Skip to content

Commit

Permalink
feat(parser): recover from const used as a BindingIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Oct 12, 2024
1 parent 0784e74 commit 7abfbd5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
8 changes: 8 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ pub fn identifier_generator(x0: &str, span1: Span) -> OxcDiagnostic {
.with_label(span1)
}

#[cold]
pub fn unexpected_const(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(
"Identifier expected. 'const' is a reserved word that cannot be used here.",
)
.with_label(span)
}

#[cold]
pub fn constructor_generator(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Constructor can't be a generator").with_label(span)
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ impl<'a> ParserImpl<'a> {
/// `BindingIdentifier` : Identifier
pub(crate) fn parse_binding_identifier(&mut self) -> Result<BindingIdentifier<'a>> {
if !self.cur_kind().is_binding_identifier() {
return Err(self.unexpected());
if matches!(self.cur_kind(), Kind::Const) {
self.error(diagnostics::unexpected_const(self.cur_token().span()));
} else {
return Err(self.unexpected());
}
}
let (span, name) = self.parse_identifier_kind(Kind::Ident);
self.check_identifier(span, &name);
Expand Down
9 changes: 8 additions & 1 deletion tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2669,13 +2669,20 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
╰────
help: In non-strict mode code, functions can only be declared at top level, inside a block, or as the body of an if statement

× Unexpected token
× Identifier expected. 'const' is a reserved word that cannot be used here.
╭─[babel/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-const/input.js:1:5]
1 │ var co\u{6e}st = 123;
· ──────────
2 │
╰────

× Keywords cannot contain escape characters
╭─[babel/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-const/input.js:3:1]
2 │
3 │ co\u{6e}st x = 2;
· ──────────
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/es2015/identifiers/invalid-escape-seq-export/input.js:1:5]
1 │ var expor\u{74} = 123;
Expand Down
8 changes: 4 additions & 4 deletions tasks/coverage/snapshots/parser_test262.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18317,7 +18317,7 @@ Expect to Parse: tasks/coverage/test262/test/built-ins/String/prototype/split/se
· ─────
╰────

× Unexpected token
× Identifier expected. 'const' is a reserved word that cannot be used here.
╭─[test262/test/language/future-reserved-words/const.js:21:5]
20 │
21 │ var const = 1;
Expand Down Expand Up @@ -18715,21 +18715,21 @@ Expect to Parse: tasks/coverage/test262/test/built-ins/String/prototype/split/se
· ─────
╰────

× Unexpected token
× Identifier expected. 'const' is a reserved word that cannot be used here.
╭─[test262/test/language/identifiers/val-const-via-escape-hex.js:17:5]
16 │
17 │ var co\u{6e}st = 123;
· ──────────
╰────

× Unexpected token
× Identifier expected. 'const' is a reserved word that cannot be used here.
╭─[test262/test/language/identifiers/val-const-via-escape-hex4.js:17:5]
16 │
17 │ var co\u006est = 123;
· ──────────
╰────

× Unexpected token
× Identifier expected. 'const' is a reserved word that cannot be used here.
╭─[test262/test/language/identifiers/val-const.js:16:5]
15 │
16 │ var const = 123;
Expand Down

0 comments on commit 7abfbd5

Please sign in to comment.