Skip to content

Commit

Permalink
fix(parser): avoid crashing on invalid const modifier (#4267)
Browse files Browse the repository at this point in the history
Followup on #3977, fixing one of the crashes at #3977.
  • Loading branch information
lucab committed Jul 15, 2024
1 parent f144082 commit 9a87e41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion crates/oxc_parser/src/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ impl<'a> ParserImpl<'a> {
let span = self.start_span();
let kind = self.cur_kind();

if matches!(self.cur_kind(), Kind::Const) && permit_const_as_modifier {
if matches!(self.cur_kind(), Kind::Const) {
if !permit_const_as_modifier {
return None;
}

// We need to ensure that any subsequent modifiers appear on the same line
// so that when 'const' is a standalone declaration, we don't issue
// an error.
Expand Down
1 change: 1 addition & 0 deletions tasks/coverage/misc/fail/oxc-4212-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class a { const enum b(); }
9 changes: 8 additions & 1 deletion tasks/coverage/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parser_misc Summary:
AST Parsed : 24/24 (100.00%)
Positive Passed: 24/24 (100.00%)
Negative Passed: 12/12 (100.00%)
Negative Passed: 13/13 (100.00%)

× Unexpected token
╭─[fail/oxc-169.js:2:1]
Expand Down Expand Up @@ -166,6 +166,13 @@ Negative Passed: 12/12 (100.00%)
╰────
help: Try insert a semicolon here

× Expected a semicolon or an implicit semicolon after a statement, but found none
╭─[fail/oxc-4212-1.ts:1:16]
1class a { const enum b(); }
· ▲
╰────
help: Try insert a semicolon here

× The keyword 'let' is reserved
╭─[fail/oxc.js:1:1]
1let.a = 1;
Expand Down

0 comments on commit 9a87e41

Please sign in to comment.