Skip to content

fix(parser): allow lone yield before in in an Annex B for-in init - #18444

Merged
JLHwung merged 2 commits into
babel:mainfrom
JLHwung:fix-parser-yield-before-in-for-in-initializer
Oct 31, 2026
Merged

JLHwung merged 2 commits into
babel:mainfrom
JLHwung:fix-parser-yield-before-in-for-in-initializer

Conversation

@JLHwung

@JLHwung JLHwung commented Oct 29, 2026

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel rejects valid code function* g() { for (var x = yield in z); } when sourceType is script and annexB parser option is enabled.
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 fixed a spec compliance issue reported by Claude Fable: In Annex B.3.5, the following production is allowed in sloppy mode:

ForInOfStatement[Yield, Await, Return] :
for ( var BindingIdentifier[?Yield, ?Await] Initializer[~In, ?Yield, ?Await] in Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] 

The Initializer is AssignmentExpression[~In], so in after yield belongs to the for-in head. However, parseYield only treated the tokens that can follow an AssignmentExpression[+In] as terminators, so it tried to parse in as the operand and then failed.

Assisted-by: Claude Fable 5.1

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser 7.x: needs backport labels Oct 29, 2026
@babel-bot

babel-bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 84394a7

@JLHwung
JLHwung force-pushed the fix-parser-yield-before-in-for-in-initializer branch from 2d26c14 to 9203205 Compare October 29, 2026 14:47

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a test case where it still errors, e.g.

function* fn() {
  for (var a = (yield in x) in y);
}

@JLHwung
JLHwung force-pushed the fix-parser-yield-before-in-for-in-initializer branch from aba6933 to 938bdfb Compare October 29, 2026 15:25
In sloppy mode, the generator code `for (var x = yield in z)` is valid (B.3.5):
the initializer is an `AssignmentExpression[~In]`, `in` cannot start an
AssignmentExpression, so the `yield` has no operand and the `in` belongs
to the for-in head. `parseYield` only treated the tokens that can follow
an `AssignmentExpression[+In]` as terminators, so it tried to parse `in`
as the operand and failed. Every other `yield in` form still errors
because `in` is not accepted after a YieldExpression anywhere else.

Assisted-by: Claude Fable 5.1 <[email protected]>
@JLHwung
JLHwung force-pushed the fix-parser-yield-before-in-for-in-initializer branch from 938bdfb to 84394a7 Compare October 31, 2026 17:48
@JLHwung
JLHwung requested a review from liuxingbaoyu October 31, 2026 18:24
@JLHwung
JLHwung added this pull request to the merge queue Oct 31, 2026
Merged via the queue into babel:main with commit 52f8e9b Oct 31, 2026
59 of 60 checks passed
@JLHwung
JLHwung deleted the fix-parser-yield-before-in-for-in-initializer branch October 31, 2026 18:54
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18485.

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

Labels

pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants