Skip to content

fix(parser): allow parenthesized assignment target in arrow initializer - #18372

Merged
JLHwung merged 11 commits into
babel:mainfrom
JLHwung:fix-parser-parenthesized-target-in-param-default
Oct 29, 2026
Merged

JLHwung merged 11 commits into
babel:mainfrom
JLHwung:fix-parser-parenthesized-target-in-param-default

Conversation

@JLHwung

@JLHwung JLHwung commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel rejects valid JS code (a = (b) = 1) => {}
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

This PR includes commits from #18380, I didn't open a stacked PR because we will have to reopen a new PR. Since #18380 is relatively small, I will rebase once that PR gets merged.

In this PR we fix an issue reported by Claude Fable 5.1: In the param initializer of an arrow function, Babel rejects the parenthesized assignment target because such errors are recorded in the arrow head parsing scope and incorrectly reported later. To fix that we have to introduce a new InitializerScope to avoid recording this error.

FF, JSC and V8 all accept this case.

During the review session, LLM detects a few new bugs related to the tryParse resetting the expression scope and the fixes are attached to this PR as well.

We also cleaned up and removed a few duplicate error message because both parseMaybeAssign and toAssignable may report the same error.

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

babel-bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

commit: e233eea

@JLHwung
JLHwung force-pushed the fix-parser-parenthesized-target-in-param-default branch from 0ace46f to 2fb91fa Compare October 21, 2026 02:47
@JLHwung
JLHwung force-pushed the fix-parser-parenthesized-target-in-param-default branch from 2fb91fa to 124b036 Compare October 21, 2026 03:01
Comment on lines +190 to +193
const scopeDepth = this.scope.scopeStack.length;
const prodParamDepth = this.prodParam.stacks.length;
const classScopeDepth = this.classScope.stack.length;
const expressionScopeDepth = this.expressionScope.stack.length;

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.

I find this approach harder to read, as it adds more noise. Is it for performance? Is it measurable? What if as a "compromise" we put the lengths on an object?

const scopeStacksSnapshot = this.snapshotScopeStacks();

// ...

this.restoreScopeStacks(scopeStacksSnapshot)

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.

Yes it was for performance because there will be a closure being created in every tryParse call, but I can't measure the impact definitely, I will update this part.

* The LHS of an assignment has been converted by `parseMaybeAssign`, and it is
* converted again when the assignment is converted to a pattern, so the errors
* raised or recorded in the first conversion would be reported twice, e.g.
* `[([a]) = 1] = 2` and `((a) = 1) => {}`

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.

I wonder if we could instead, when converting from an assignment to a pattern, know already based on who our caller/node is whether the node was already checked before or not.

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.

Currently, we can not skip the second toAssignable pass when we know parseMaybeAssign already converted it, because the first pass always runs with isLHS = true, while for arrow params, the second pass runs with isLHS = false, which is stricter, i.e. It reports (a.b) in ((a.b) = 1) => {}, and is the only place that reports ((%%a%%) = 1) => {} with placeholders.

@JLHwung
JLHwung force-pushed the fix-parser-parenthesized-target-in-param-default branch from 5f8e8e1 to 077893d Compare October 24, 2026 01:03
The recorded error is thrown early in errorRecovery: false mode because the tryParse does not clear the expression scope stack.
`toAssignable`, `toAssignableParenthesizedExpression` and
`checkToRestConversion` each listed the same four type cast nodes
(`TSAsExpression`, `TSSatisfiesExpression`, `TSNonNullExpression` and
`TSTypeAssertion`). Share the list through a `tsIsTypeCast` type guard.
`isValidLVal` keeps its own cases since it treats `TSNonNullExpression`
differently. No behavior change.

Assisted-by: Claude Opus 5.5 <[email protected]>
…ializers

An Initializer of a formal parameter (or of a binding element nested in
a parameter pattern) is an ordinary AssignmentExpression, not part of
the binding pattern, so `(b)` in `function f(a = (b) = 1) {}` or
`(x = [(a)] = 1) => {}` is a valid simple assignment target. The same
holds for computed keys, e.g. `({ [(b) = 1]: a }) => {}`, and decorator
arguments, e.g. `function f(@dec((b) = 1) a) {}`. Only the targets that
end up in the parameter pattern itself must not be parenthesized.

Add an Initializer expression scope, entered by an AssignmentExpression
that is not parsed as part of a cover grammar when the current scope is
a (possible) ParameterDeclaration. Parenthesized binding errors are not
recorded in it, while await/yield parameter initializer errors are
still propagated to the enclosing (possible) parameter scopes.

Also restore the scope stacks when the speculative parse in the
typescript plugin's `tsSkipParameterStart` throws, sharing the logic
with `tryParse` through `snapshotScopeStacks`, so that a scope entered
by the failed parse is not left on the stack.

Assisted-by: Claude Opus 5.5 <[email protected]>
When a tryParse attempt threw, the declaration errors recorded at or
after its start position were cleared from the arrow head scopes. This
also discarded the errors recorded by an earlier attempt that started
at the same position and failed without throwing, whose result is
adopted through `failState`, e.g. the JSX attempt in
`async (x = <a>{await}</b>) => {}` with typescript and jsx, so the
AwaitBindingIdentifier error was lost. Only restore the scope stacks.

Assisted-by: Claude Opus 5.5 <[email protected]>
…losure

Rename the shared Expression scope to `sharedExpressionScope`, so that it
is not confused with the `this.expressionScope` handler, e.g. in
`this.expressionScope.enter(sharedExpressionScope)`.

Replace `snapshotScopeStacks`, which allocated a closure on every
`tryParse` call although it is only invoked when the attempt throws,
with `restoreScopeStacks`, which takes the scope stack depths that the
caller records in local variables. Merge the object and array pattern
branches of `tsSkipParameterStart` so that the depths are recorded once.

Assisted-by: Claude Opus 5.5 <[email protected]>
The LHS of an assignment is converted by `parseMaybeAssign`, and it is
converted again when the assignment is converted to a pattern, e.g. as
an arrow parameter in `((a) = 1) => {}` or as a nested assignment target
in `[([a]) = 1] = 2`. The errors raised or recorded in the first
conversion were reported again by the second one.

Add `raiseOnce`, which drops the new error when the same error has been
reported at the same position, and use it for the
InvalidParenthesizedAssignment and UnexpectedTypeCastInParameter errors
raised in `toAssignable`. The second conversion is kept because it also
reports errors that the first one allows, e.g. the parenthesized member
expression in `((a.b) = 1) => {}`.

Assisted-by: Claude Opus 5.5 <[email protected]>
…in-param-initializer/input.ts test in ts-eslint integration test
@JLHwung
JLHwung force-pushed the fix-parser-parenthesized-target-in-param-default branch from 077893d to e233eea Compare October 27, 2026 19:51
@JLHwung
JLHwung merged commit 4dd004a into babel:main Oct 29, 2026
57 of 58 checks passed
@JLHwung
JLHwung deleted the fix-parser-parenthesized-target-in-param-default branch October 29, 2026 13:16
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18442.

brandhaug added a commit to brandhaug/b2b-saas-starter that referenced this pull request Nov 2, 2026
## 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>
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.

3 participants