Skip to content

fix(parser): disallow const x followed by linebreak and of - #18336

Open
JLHwung wants to merge 3 commits into
babel:mainfrom
JLHwung:fix-parser-missing-initializer-before-of
Open

JLHwung wants to merge 3 commits into
babel:mainfrom
JLHwung:fix-parser-missing-initializer-before-of

Conversation

@JLHwung

@JLHwung JLHwung commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel parser should reject invalid ASI case: "const x\nof"
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

In this PR we fix an issue reported by Claude Code, see comments below for an detailed explanation.

I prompted the LLM to add more tests and have reviewed the add new tests.

… by `of`

Per LexicalBinding early errors, a binding without an Initializer is only
allowed in the head of a `for-in`/`for-of` statement. `parseVar` skipped
the error whenever the next token was `in` or `of`, even outside a for
head, so e.g. `const x\nof` was parsed as `const x; of;`.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
esprima/statement-iteration/const_forin duplicates packages/babel-parser/test/fixtures/es2015/for-in/const-no-initializer

uncategorised/315 duplicate packages/babel-parser/test/fixtures/es2015/for-of/const-no-initializer
@JLHwung JLHwung added PR: Spec Compliance 👓 A type of pull request used for our changelog categories pkg: parser labels Oct 12, 2026
@babel-bot

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62373

);
} else if (
(kind === "const" || kind === "using" || kind === "await using") &&
!(this.match(tt._in) || this.isContextual(tt._of))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input

const x
of

was incorrectly allowed because here we perform a lookahead without checking the isFor context. This PR fixes this issue by moving the isFor + in/of check upfront to the !allowMissingInitializer and simplify the logic flow.

@JLHwung JLHwung changed the title Fix parser missing initializer before of fix(parser): disallow const x followed by linebreak and in Oct 12, 2026
@JLHwung JLHwung changed the title fix(parser): disallow const x followed by linebreak and in fix(parser): disallow const x followed by linebreak and of Oct 12, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: 42585dd

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: parser PR: Spec Compliance 👓 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants