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): don't panic on declare@ #4601

Merged
merged 1 commit into from
Nov 20, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- Don't panic when a multi-byte character is found in a unicode escape sequence ([#4564](https://github.com/biomejs/biome/issues/4564)). Contributed by @Conaclos

- Don't panic when a declare statement is followed by an unexpected token.([#4562](https://github.com/biomejs/biome/issues/4562)). Contributed by @fireairforce
## v1.9.4 (2024-10-17)

### Analyzer
Expand Down
4 changes: 4 additions & 0 deletions crates/biome_js_parser/src/syntax/js_parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ pub(crate) fn expected_namespace_import(p: &JsParser, range: TextRange) -> Parse
expected_any(&["namespace imports"], range, p)
}

pub(crate) fn expected_declare_statement(p: &JsParser, range: TextRange) -> ParseDiagnostic {
expected_node("declare statement", range, p)
}

pub(crate) fn expected_namespace_or_named_import(
p: &JsParser,
range: TextRange,
Expand Down
7 changes: 4 additions & 3 deletions crates/biome_js_parser/src/syntax/typescript/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::syntax::expr::{is_nth_at_identifier, parse_name, ExpressionContext};
use super::ts_parse_error::expected_ts_enum_member;
use crate::state::EnterAmbientContext;
use crate::syntax::auxiliary::{is_nth_at_declaration_clause, parse_declaration_clause};
use crate::syntax::js_parse_error::{expected_identifier, expected_module_source};
use crate::syntax::js_parse_error::{
expected_declare_statement, expected_identifier, expected_module_source,
};
use crate::syntax::module::{parse_module_item_list, parse_module_source, ModuleItemListParent};
use crate::syntax::stmt::{semi, STMT_RECOVERY_SET};
use crate::syntax::typescript::ts_parse_error::expected_ts_type;
Expand Down Expand Up @@ -241,8 +243,7 @@ pub(crate) fn parse_ts_declare_statement(p: &mut JsParser) -> ParsedSyntax {
// test_err ts ts_declare_const_initializer
// declare @decorator class D {}
// declare @decorator abstract class D {}
parse_declaration_clause(p, stmt_start_pos)
.expect("Expected a declaration as guaranteed by is_at_ts_declare_statement")
parse_declaration_clause(p, stmt_start_pos).or_add_diagnostic(p, expected_declare_statement)
});

Present(m.complete(p, TS_DECLARE_STATEMENT))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export declare @decorator class D {}
export declare @decorator abstract class D {}

declare@
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ expression: snapshot
export declare @decorator class D {}
export declare @decorator abstract class D {}

declare@
```


Expand Down Expand Up @@ -93,19 +94,32 @@ JsModule {
},
],
},
JsBogusStatement {
items: [
[email protected] "declare" [Newline("\n"), Newline("\n")] [],
JsBogusStatement {
items: [
JsDecorator {
at_token: [email protected] "@" [] [],
expression: missing (required),
},
],
},
],
},
],
eof_token: EOF@82..83 "" [Newline("\n")] [],
eof_token: EOF@92..92 "" [] [],
}
```

## CST

```
0: JS_MODULE@0..83
0: JS_MODULE@0..92
0: (empty)
1: (empty)
2: [email protected]
3: JS_MODULE_ITEM_LIST@0..82
3: JS_MODULE_ITEM_LIST@0..92
0: [email protected]
0: [email protected]
1: [email protected] "export" [] [Whitespace(" ")]
Expand Down Expand Up @@ -152,7 +166,13 @@ JsModule {
7: [email protected] "{" [] []
8: [email protected]
9: [email protected] "}" [] []
4: [email protected] "" [Newline("\n")] []
2: [email protected]
0: [email protected] "declare" [Newline("\n"), Newline("\n")] []
1: [email protected]
0: [email protected]
0: [email protected] "@" [] []
1: (empty)
4: [email protected] "" [] []

```

Expand All @@ -178,7 +198,51 @@ ts_export_declare.ts:2:16 parse ━━━━━━━━━━━━━━━━
> 2 │ export declare @decorator abstract class D {}
│ ^^^^^^^^^^
3 │
4 │ declare@

i Decorators are only valid on class declarations, class expressions, and class methods.

ts_export_declare.ts:4:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Expected an expression but instead found the end of the file.

2 │ export declare @decorator abstract class D {}
3 │
> 4 │ declare@

i Expected an expression here.

2 │ export declare @decorator abstract class D {}
3 │
> 4 │ declare@
ematipico marked this conversation as resolved.
Show resolved Hide resolved

ts_export_declare.ts:4:8 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Decorators are not valid here.

2 │ export declare @decorator abstract class D {}
3 │
> 4 │ declare@
│ ^

i Decorators are only valid on class declarations, class expressions, and class methods.

ts_export_declare.ts:4:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× Expected a declare statement but instead found the end of the file.

2 │ export declare @decorator abstract class D {}
3 │
> 4 │ declare@

i Expected a declare statement here.

2 │ export declare @decorator abstract class D {}
3 │
> 4 │ declare@

```
Loading