Skip to content

fix(parser): reject arrow functions after an arrow in an earlier operand - #18533

Merged
JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-parser-arrow-binary-operand
Nov 9, 2026
Merged

JLHwung merged 1 commit into
babel:mainfrom
JLHwung:fix-parser-arrow-binary-operand

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 5, 2026

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel parser accepts invalid a + (() => {}) + b => c
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? No, this is a regression introduced in Babel 8
License MIT

In this PR we fix a regression introduced in #17838: The canStartArrow was reset too late: it should have been reset when we have started parsing the right operand of the binary expression. This issue is reported by Claude Fable.

`state.canStartArrow` was only cleared after the first operand of a binary
expression, so an arrow function in a later operand left it set for the next
one, and `a + (() => {}) + b => c` was accepted.

Assisted-by: Claude Opus 5.5
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels Nov 5, 2026
@babel-bot

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: 8cff475

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the parser regression fix.

No open issues 🎉

What changed in this PR

Fixes a Babel 8 parser regression introduced in #17838 that allowed unparenthesized arrow functions in later binary operands after parsing an earlier nested arrow.

Changes:

  • Resets canStartArrow immediately before parsing each binary right operand.
  • Adds rejection fixtures for binary, logical, exponentiation, async-arrow, and F# pipeline cases.
  • Adds snapshots covering valid expressions and error recovery.
File Description
packages/​babel-parser/​test/​fixtures/​experimental/​pipeline-operator-fsharp/​fsharp-proposal-ban-arrow-in-binary-operand/​options.json Specifies the expected pipeline syntax error.
packages/​babel-parser/​test/​fixtures/​experimental/​pipeline-operator-fsharp/​fsharp-proposal-ban-arrow-in-binary-operand/​input.js Covers an invalid arrow in a pipeline binary operand.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-logical-argument-after-arrow-in-call/​options.json Specifies the expected logical-operand error.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-logical-argument-after-arrow-in-call/​input.js Covers an invalid arrow after a call containing an arrow.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-exponent-argument-after-parenthesized-arrow/​options.json Specifies the expected exponentiation error.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-exponent-argument-after-parenthesized-arrow/​input.js Covers an invalid arrow in an exponentiation chain.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-parens-after-parenthesized-arrow/​options.json Specifies the expected parenthesized-parameter error.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-parens-after-parenthesized-arrow/​input.js Covers an invalid later arrow with parenthesized parameters.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-async-parens-after-parenthesized-arrow/​options.json Specifies the expected async-arrow error.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-async-parens-after-parenthesized-arrow/​input.js Covers an invalid async arrow with parenthesized parameters.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-async-after-parenthesized-arrow/​output.json Records the missing-semicolon error and recovered syntax tree.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-async-after-parenthesized-arrow/​input.js Covers an invalid later async arrow without parameter parentheses.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-after-parenthesized-arrow/​options.json Specifies the original regression’s expected error.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-after-parenthesized-arrow/​input.js Reproduces the reported binary-operand regression.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-after-arrow-in-template/​options.json Specifies the expected error after a template operand.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-after-arrow-in-template/​input.js Covers an earlier arrow inside template interpolation.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-after-arrow-in-array/​options.json Specifies the expected error after an array operand.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​invalid-binary-argument-after-arrow-in-array/​input.js Covers an earlier arrow inside an array.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​binary-argument-after-parenthesized-arrow/​output.json Records syntax trees for valid neighboring cases.
packages/​babel-parser/​test/​fixtures/​es2015/​arrow-functions/​binary-argument-after-parenthesized-arrow/​input.js Covers valid operands and parenthesized arrow sequences.
packages/​babel-parser/​src/​parser/​expression.ts Moves the arrow-start reset to right-operand parsing.

Review effort: Balanced

@JLHwung
JLHwung requested a review from liuxingbaoyu November 6, 2026 18:35
@JLHwung
JLHwung added this pull request to the merge queue Nov 9, 2026
Merged via the queue into babel:main with commit 5a38c34 Nov 9, 2026
58 of 60 checks passed
@JLHwung
JLHwung deleted the fix-parser-arrow-binary-operand branch November 9, 2026 19:20
JLHwung added a commit to JLHwung/babel that referenced this pull request Nov 10, 2026
…and (babel#18533)

`state.canStartArrow` was only cleared after the first operand of a binary
expression, so an arrow function in a later operand left it set for the next
one, and `a + (() => {}) + b => c` was accepted.

Assisted-by: Claude Opus 5.5
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