Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js_parser): Correctly parse global declaration when the global token and the { token are not on the same line #1731

Merged
merged 12 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

### Parser

#### Bug fixes
- Fix [#1728](https://github.com/biomejs/biome/issues/1728). Correctly parse the global declaration when the `{` token is on the line following the `global` keyword.

Now the following code is correctly parsed:

```ts
declare global
{ }

declare module foo {
global
{ }
}
```

Contributed by @ah-yu

## 1.5.3 (2024-01-22)

### LSP
Expand Down
8 changes: 4 additions & 4 deletions crates/biome_js_parser/src/syntax/auxiliary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub(crate) fn is_nth_at_declaration_clause(p: &mut JsParser, n: usize) -> bool {
return true;
}

if is_nth_at_any_ts_namespace_declaration(p, n) {
return true;
}

if p.has_nth_preceding_line_break(n + 1) {
return false;
}
Expand All @@ -63,10 +67,6 @@ pub(crate) fn is_nth_at_declaration_clause(p: &mut JsParser, n: usize) -> bool {
return true;
}

if is_nth_at_any_ts_namespace_declaration(p, n) {
return true;
}

if p.nth_at(n, T![abstract]) && p.nth_at(n + 1, T![class]) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_parser/src/syntax/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub(crate) fn parse_statement(p: &mut JsParser, context: StatementContext) -> Pa
T![async] if is_at_async_function(p, LineBreak::DoNotCheck) => {
parse_function_declaration(p, context)
}
T![module] | T![namespace] | T![global] if is_at_any_ts_namespace_declaration(p) => {
T![module] | T![namespace] | T![global] if is_nth_at_any_ts_namespace_declaration(p, 0) => {
let name = p.cur_range();
TypeScript.parse_exclusive_syntax(
p,
Expand Down
30 changes: 10 additions & 20 deletions crates/biome_js_parser/src/syntax/typescript/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,24 +375,11 @@ fn parse_ts_extends_clause(p: &mut JsParser) -> ParsedSyntax {
}

#[inline]
pub(crate) fn is_at_any_ts_namespace_declaration(p: &mut JsParser) -> bool {
if p.has_nth_preceding_line_break(1) {
return false;
}

if matches!(p.cur(), T![namespace] | T![module]) {
return is_nth_at_identifier(p, 1) || p.nth_at(1, JS_STRING_LITERAL);
}

if p.at(T![global]) {
return p.nth_at(1, T!['{']);
pub(crate) fn is_nth_at_any_ts_namespace_declaration(p: &mut JsParser, n: usize) -> bool {
if p.nth_at(n, T![global]) {
return p.nth_at(n + 1, T!['{']);
}

false
}

#[inline]
pub(crate) fn is_nth_at_any_ts_namespace_declaration(p: &mut JsParser, n: usize) -> bool {
if p.has_nth_preceding_line_break(n + 1) {
return false;
}
Expand All @@ -401,10 +388,6 @@ pub(crate) fn is_nth_at_any_ts_namespace_declaration(p: &mut JsParser, n: usize)
return is_nth_at_identifier(p, n + 1) || p.nth_at(n + 1, JS_STRING_LITERAL);
}

if p.nth_at(n, T![global]) {
return p.nth_at(n + 1, T!['{']);
}

false
}

Expand Down Expand Up @@ -521,6 +504,13 @@ fn parse_ts_module_block(p: &mut JsParser) -> ParsedSyntax {
// let VERSION: string;
// }
// }
// declare module "foo" {
// global
// { }
// }
// declare global {}
// declare global
// { }
//
// test ts ts_global_variable
// let global;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,60 @@ JsModule {
},
},
},
TsDeclareStatement {
declare_token: [email protected] "declare" [Newline("\n")] [Whitespace(" ")],
declaration: TsExternalModuleDeclaration {
module_token: [email protected] "module" [] [Whitespace(" ")],
source: JsModuleSource {
value_token: [email protected] "\"foo\"" [] [Whitespace(" ")],
},
body: TsModuleBlock {
l_curly_token: [email protected] "{" [] [],
items: JsModuleItemList [
TsGlobalDeclaration {
global_token: [email protected] "global" [Newline("\n"), Whitespace(" ")] [],
body: TsModuleBlock {
l_curly_token: [email protected] "{" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")],
items: JsModuleItemList [],
r_curly_token: [email protected] "}" [] [],
},
},
],
r_curly_token: [email protected] "}" [Newline("\n")] [],
},
},
},
TsDeclareStatement {
declare_token: [email protected] "declare" [Newline("\n")] [Whitespace(" ")],
declaration: TsGlobalDeclaration {
global_token: [email protected] "global" [] [Whitespace(" ")],
body: TsModuleBlock {
l_curly_token: [email protected] "{" [] [],
items: JsModuleItemList [],
r_curly_token: [email protected] "}" [] [],
},
},
},
TsDeclareStatement {
declare_token: [email protected] "declare" [Newline("\n")] [Whitespace(" ")],
declaration: TsGlobalDeclaration {
global_token: [email protected] "global" [] [],
body: TsModuleBlock {
l_curly_token: [email protected] "{" [Newline("\n")] [Whitespace(" ")],
items: JsModuleItemList [],
r_curly_token: [email protected] "}" [] [],
},
},
},
],
eof_token: EOF@66..67 "" [Newline("\n")] [],
eof_token: EOF@141..142 "" [Newline("\n")] [],
}

0: JS_MODULE@0..67
0: JS_MODULE@0..142
0: (empty)
1: (empty)
2: [email protected]
3: JS_MODULE_ITEM_LIST@0..66
3: JS_MODULE_ITEM_LIST@0..141
0: [email protected]
0: [email protected] "declare" [] [Whitespace(" ")]
1: [email protected]
Expand Down Expand Up @@ -87,4 +132,36 @@ JsModule {
1: [email protected] ";" [] []
2: [email protected] "}" [Newline("\n"), Whitespace(" ")] []
2: [email protected] "}" [Newline("\n")] []
4: [email protected] "" [Newline("\n")] []
1: [email protected]
0: [email protected] "declare" [Newline("\n")] [Whitespace(" ")]
1: [email protected]
0: [email protected] "module" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "\"foo\"" [] [Whitespace(" ")]
2: [email protected]
0: [email protected] "{" [] []
1: [email protected]
0: [email protected]
0: [email protected] "global" [Newline("\n"), Whitespace(" ")] []
1: [email protected]
0: [email protected] "{" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")]
1: [email protected]
2: [email protected] "}" [] []
2: [email protected] "}" [Newline("\n")] []
2: [email protected]
0: [email protected] "declare" [Newline("\n")] [Whitespace(" ")]
1: [email protected]
0: [email protected] "global" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "{" [] []
1: [email protected]
2: [email protected] "}" [] []
3: [email protected]
0: [email protected] "declare" [Newline("\n")] [Whitespace(" ")]
1: [email protected]
0: [email protected] "global" [] []
1: [email protected]
0: [email protected] "{" [Newline("\n")] [Whitespace(" ")]
1: [email protected]
2: [email protected] "}" [] []
4: [email protected] "" [Newline("\n")] []
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ declare module "./test" {
let VERSION: string;
}
}
declare module "foo" {
global
{ }
}
declare global {}
declare global
{ }
17 changes: 17 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

### Parser

#### Bug fixes
- Fix [#1728](https://github.com/biomejs/biome/issues/1728). Correctly parse the global declaration when the `{` token is on the line following the `global` keyword.

Now the following code is correctly parsed:

```ts
declare global
{ }

declare module foo {
global
{ }
}
```

Contributed by @ah-yu

## 1.5.3 (2024-01-22)

### LSP
Expand Down
Loading