Repository navigation
fix(parser): handle ASI after let followed by a reserved word on the next line - #18409
Conversation
In sloppy mode, `let` followed by a line break and a reserved word
(e.g. `let \n if (x) {}`) is not a lexical declaration: a ReservedWord
can never match `BindingIdentifier : Identifier` (this is a grammar
restriction, not an early error), so the reserved word is an offending
token preceded by a LineTerminator and ASI turns `let` into an
identifier expression statement. `yield`, `await`, escaped keywords and
other identifiers are still parsed as declarations, since they are
grammatically valid BindingIdentifiers.
Assisted-by: Claude Opus 5.5 <[email protected]>
Assisted-by: Claude Opus 5.5 <[email protected]>
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62566 |
|
commit: |
| keywordLikeWord.lastIndex = pos; | ||
| if (!keywordLikeWord.test(this.input)) { | ||
| return false; | ||
| } | ||
| const end = keywordLikeWord.lastIndex; | ||
| const endCh = this.codePointAtPos(end); | ||
| if (isIdentifierChar(endCh) || endCh === charCodes.backslash) { | ||
| return false; | ||
| } | ||
| const word = this.input.slice(pos, end); | ||
| // Pass `inModule: false` so that only `enum` is checked, even in modules: | ||
| // `await` is a valid BindingIdentifier in the grammar and is only | ||
| // disallowed in modules by an early error. | ||
| return isKeyword(word) || isReservedWord(word, false); |
There was a problem hiding this comment.
We could simplify this part with a full lookahead then containsEsc check and the token type check. But the parser will slow down up to 20% for cases like 20k of line let\n a = 1, b = 2, the prettier does not generate such format, though.
There was a problem hiding this comment.
🟢 Approval recommended
The parser fix has comprehensive regression coverage, and no unresolved blocking issues were identified.
No open issues 🎉
What changed in this PR
This pull request fixes Babel parser handling of automatic semicolon insertion when contextual let is followed by a line break and a reserved word, allowing valid cases such as let\nif (0) {}.
Changes:
- Detect reserved words after
letacross line terminators and parse the input using ASI. - Preserve declaration, export, module, Flow, TypeScript, and contextual-identifier behavior.
- Add broad regression coverage for comments, Unicode line separators, strict mode, generators, async functions, Annex B, and
forstatements. - Align TypeScript-ESTree comparisons with fixture-specific source type and JSX settings.
| File | Description |
|---|---|
packages/babel-parser/test/fixtures/typescript/enum/let-with-linebreak/output.json |
Expected TypeScript enum AST. |
packages/babel-parser/test/fixtures/typescript/enum/let-with-linebreak/options.json |
TypeScript enum fixture options. |
packages/babel-parser/test/fixtures/typescript/enum/let-with-linebreak/input.js |
TypeScript enum ASI regression input. |
packages/babel-parser/test/fixtures/flow/enum-declaration/let-with-linebreak/output.json |
Expected Flow enum AST. |
packages/babel-parser/test/fixtures/flow/enum-declaration/let-with-linebreak/input.js |
Flow enum ASI regression input. |
packages/babel-parser/test/fixtures/flow/declare-export/export-let-with-linebreak-and-keyword/options.json |
Expected Flow diagnostic. |
packages/babel-parser/test/fixtures/flow/declare-export/export-let-with-linebreak-and-keyword/input.js |
Flow declare-export regression input. |
packages/babel-parser/test/fixtures/experimental/discard-binding/let-with-linebreak-and-void/output.json |
Expected discard-binding AST. |
packages/babel-parser/test/fixtures/experimental/discard-binding/let-with-linebreak-and-void/input.js |
Discard-binding ASI regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-multiline-comment-and-keyword/output.json |
Expected AST and comment attachment. |
packages/babel-parser/test/fixtures/es2015/let/let-with-multiline-comment-and-keyword/input.js |
Multiline-comment regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-yield/output.json |
Expected non-generator yield output. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-yield/input.js |
Non-generator yield regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-yield-generator/output.json |
Expected generator diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-yield-generator/input.js |
Generator yield regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-void/output.json |
Expected void expression AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-void/input.js |
void expression regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-non-reserved-word/output.json |
Expected declaration AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-non-reserved-word/input.js |
Non-reserved binding regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword/output.json |
Expected separate let and keyword statements. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword/input.js |
Primary reserved-keyword ASI regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-strict/output.json |
Expected strict-mode diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-strict/input.js |
Strict-mode regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-paragraph-separator/output.json |
Expected paragraph-separator AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-paragraph-separator/input.js |
Paragraph-separator regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-nested/output.json |
Expected nested statement AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-nested/input.js |
Nested statement regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-module/output.json |
Expected module diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-module/options.json |
Module fixture options. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-module/input.js |
Module-mode regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-line-separator/output.json |
Expected Unicode line-separator AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-line-separator/input.js |
Unicode line-separator regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-expression/output.json |
Expected expression and comment AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-expression/input.js |
Keyword-led expression regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-declaration/output.json |
Expected declaration AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-declaration/input.js |
Following-declaration regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-cr/output.json |
Expected CR-separated AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-keyword-cr/input.js |
Carriage-return regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-escaped-keyword/output.json |
Expected escaped-keyword diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-escaped-keyword/input.js |
Escaped-keyword regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-enum/output.json |
Expected enum diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-enum/input.js |
Reserved enum regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-await-module/output.json |
Expected module await diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-await-module/options.json |
Module await fixture options. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-await-module/input.js |
Module await regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-await-async/output.json |
Expected async-function diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/let-with-linebreak-and-await-async/input.js |
Async-function await regression input. |
packages/babel-parser/test/fixtures/es2015/let/let-with-keyword-same-line/output.json |
Expected same-line behavior. |
packages/babel-parser/test/fixtures/es2015/let/let-with-keyword-same-line/input.js |
Same-line keyword regression input. |
packages/babel-parser/test/fixtures/es2015/let/for-let-with-linebreak-and-keyword/output.json |
Expected for initializer AST. |
packages/babel-parser/test/fixtures/es2015/let/for-let-with-linebreak-and-keyword/input.js |
for initializer regression input. |
packages/babel-parser/test/fixtures/es2015/let/export-let-with-linebreak-and-keyword/output.json |
Expected named-export diagnostics and AST. |
packages/babel-parser/test/fixtures/es2015/let/export-let-with-linebreak-and-keyword/options.json |
Named-export fixture options. |
packages/babel-parser/test/fixtures/es2015/let/export-let-with-linebreak-and-keyword/input.js |
Named-export regression input. |
packages/babel-parser/test/fixtures/es2015/let/export-let-with-linebreak-and-keyword-script/output.json |
Expected script-mode export AST. |
packages/babel-parser/test/fixtures/es2015/let/export-let-with-linebreak-and-keyword-script/options.json |
Script export fixture options. |
packages/babel-parser/test/fixtures/es2015/let/export-let-with-linebreak-and-keyword-script/input.js |
Script-mode export regression input. |
packages/babel-parser/test/fixtures/es2015/let/export-default-let-with-linebreak-and-keyword/output.json |
Expected default-export AST. |
packages/babel-parser/test/fixtures/es2015/let/export-default-let-with-linebreak-and-keyword/options.json |
Default-export fixture options. |
packages/babel-parser/test/fixtures/es2015/let/export-default-let-with-linebreak-and-keyword/input.js |
Default-export regression input. |
packages/babel-parser/test/fixtures/annex-b/enabled/1.1-html-comments-open-let-keyword/output.json |
Expected Annex B opening-comment AST. |
packages/babel-parser/test/fixtures/annex-b/enabled/1.1-html-comments-open-let-keyword/input.js |
Annex B opening-comment regression input. |
packages/babel-parser/test/fixtures/annex-b/enabled/1.1-html-comments-close-let-keyword/output.json |
Expected Annex B closing-comment AST. |
packages/babel-parser/test/fixtures/annex-b/enabled/1.1-html-comments-close-let-keyword/input.js |
Annex B closing-comment regression input. |
packages/babel-parser/src/parser/statement.ts |
Implements reserved-word lookahead and ASI handling after let. |
eslint/babel-eslint-parser/test/typescript-estree.test.js |
Aligns TypeScript-ESTree parsing with fixture source type and JSX settings. |
Review effort: Lite
| const keywordRelationalOperator = /in(?:stanceof)?/y; | ||
|
|
||
| // Every keyword is 2 (`do`) to 10 (`instanceof`) lowercase ASCII letters. | ||
| const keywordLikeWord = /[a-z]{2,10}/y; |
There was a problem hiding this comment.
Is it possible to write a unit test that ensures that all the keywords our parser defines pass this regexp? In case the language gets a new longer keyword.
Also, should this regexp start/end with \b? Can you add a test for let \n instanceofhello?
There was a problem hiding this comment.
The keywordLikeWord regex is guarded behind the hasNewLine call, so \b is not required. The assumption here is that let \na is less popular than let a so most let variable declaration only pays the cost of hasNewLine check.
Just added new tests for the in/instanceof prefixed identifier and a new unit test.
|
Backported to 7.x in #18432. |
…next line (#18432) * fix(parser): handle ASI after let followed by a reserved word on the next line (#18409) backport of b8a65cf * Fix enum fixture output after backporting #18409 Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Huáng Jùnliàng <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
## pnpm-workspace.yaml (default) ## Dependency Updates | Package | From | To | Type | | --- | --- | --- | --- | | `@babel/core` | 8.0.9 | 8.0.10 | patch | | `@inlang/plugin-message-format` | 4.5.5 | 4.5.6 | patch | | `@tanstack/devtools-vite` | 0.8.5 | 0.8.6 | patch | | `ai` | 7.0.156 | 7.0.157 | patch | | `cn` | 0.5.0 | 0.5.1 | patch | | `seroval` | 1.7.4 | 1.7.5 | patch | ## Release Notes <details> <summary><b>@<!---->babel/core</b> (8.0.9 → 8.0.10)</summary> ## v8.0.10 (2026-10-29) Thanks @<!---->dagemawinegash, @<!---->nyan-left, @<!---->saiyam-jain-dev, and @<!---->yulman2207 for your first PRs! #### 🐛 Bug Fix * `babel-parser` * [#18420](babel/babel#18420) fix(parser): improve radix literal rounding ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18372](babel/babel#18372) fix(parser): allow parenthesized assignment target in arrow initializer ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18410](babel/babel#18410) fix(parser): do not record strict mode errors during lookahead ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18433](babel/babel#18433) fix(parser): invalid expectOnePlugin usage ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18409](babel/babel#18409) fix(parser): handle ASI after let followed by a reserved word on the next line ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18406](babel/babel#18406) fix(parser): parser should not crash for `import defer {}` ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18407](babel/babel#18407) fix(parser): record start location in `startNodeAt` ([@<!---->JLHwung](https://github.com/JLHwung)) * `babel-plugin-transform-block-scoping` * [#18371](babel/babel#18371) fix: closures in for-loop test and update capture wrong binding ([@<!---->joellemd](https://github.com/joellemd)) * `babel-generator` * [#18418](babel/babel#18418) fix(generator): keep aliases of string-literal module specifiers ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18427](babel/babel#18427) Fix missing parentheses around TypeScript assertion targets ([@<!---->dagemawinegash](https://github.com/dagemawinegash)) * [#18324](babel/babel#18324) fix: invalid `minified: true` output for new expression ([@<!---->JLHwung](https://github.com/JLHwung)) * [#18437](https://github. …[full notes](https://github.com/babel/babel/releases/tag/v8.0.10) </details> <details> <summary><b>@<!---->tanstack/devtools-vite</b> (0.8.5 → 0.8.6)</summary> ### Patch Changes - [#539](TanStack/devtools#539) [`ed8b314`](TanStack/devtools@ed8b314) - Source injection no longer stops after the dev server parses a regex literal. Before this fix, a regex literal cached `value` as a child key of `Literal` nodes. Every later file with a string literal then threw during the walk and got zero `data-tsd-source` attributes. - [#540](TanStack/devtools#540) [`012421f`](TanStack/devtools@012421f) - Stop the console pipe feedback loop with Vite 8 `server.forwardConsole`. The server side of the pipe no longer sends browser logs that Vite printed in the terminal back to the browser. - [#548](TanStack/devtools#548) [`40f32e4`](TanStack/devtools@40f32e4) - Run source injection before the transforms of other plugins (unless they also use `order: 'pre'`), also when `devtools()` is not first in the plugins array. Before this fix, a framework plugin that changed the code of only one environment shifted the line numbers, so `data-tsd-source` differed between SSR and client and caused hydration warnings. - [#541](TanStack/devtools#541) [`df1464c`](TanStack/devtools@df1464c) - Fix "Go to Source" for files outside the current working directory, for example a package in a monorepo or Vite run from another directory. The editor got a path with the working directory added twice. Also answer a missing or malformed `source` with `400` instead of leaving the request pending. - [#542](TanStack/devtools#542) [`9c58288`](TanStack/devtools@9c58288) - Remove the `intent` bin from the published packages. It took over `node_modules/.bin/i …[full notes](https://github.com/TanStack/devtools/releases/tag/%40tanstack/devtools-vite%400.8.6) </details> <details> <summary><b>cn</b> (0.5.0 → 0.5.1)</summary> ### Patch Changes - [#165](shadcn-ui/cn#165) [`5e65675`](shadcn-ui/cn@5e65675) Thanks [@<!---->shadcn](https://github.com/shadcn)! - Group the legacy `bg-gradient-to-*` utilities as `bg-color`, the same as tailwind-merge. `cn("bg-background", "bg-gradient-to-r")` now returns `bg-gradient-to-r`, and `bg-gradient-to-r` no longer replaces `bg-linear-to-r` or an arbitrary `bg-[...]` image. </details> <details> <summary><b>seroval</b> (1.7.4 → 1.7.5)</summary> ### Patch Changes - d85ec3f: Keep two round-trip cases intact. An object whose own `constructor` key holds a falsy value, such as `{ constructor: undefined }`, now keeps `Object.prototype` instead of coming back with a null prototype. A null-prototype object with an own `__proto__` key, or any object serialized with `Feature.ObjectAssign` disabled, no longer turns equal-looking values such as two `new Set()` into one shared object in `serialize`, `crossSerialize` and `compileJSON` output. </details> --- *This PR was auto-generated by [catalog-update-action](https://github.com/brandhaug/catalog-update-action).* Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
let\nif (0) {}In this PR we fix an issue reported by Claude Opus 5.5:
let\nif (0) {}is valid because a ReservedWordcan never match
BindingIdentifier : Identifier, so per ASI rules,letwill become single identifier statement.Both FF and V8 support this case. They throw a runtime Error "let is not defined" rather than a syntax error.