Skip to content

fix(parser): record start location in startNodeAt - #18407

Merged
JLHwung merged 4 commits into
babel:mainfrom
JLHwung:fix-parser-start-node-at-set-loc
Oct 26, 2026
Merged

JLHwung merged 4 commits into
babel:mainfrom
JLHwung:fix-parser-start-node-at-set-loc

Conversation

@JLHwung

@JLHwung JLHwung commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel parser crashes when parsing export { x as z }; export * as z from "y" with locations: false
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

Previously we only invoke setLoc in startNode, in this PR we move the call to startNodeAt such that it works for other parser routines using startNodeAt as well.

We also fixed a parser crash detected when adding new tests: plugins: ["estree"] relies on the loc property, so Babel parser will crash when locations: false is set.

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels Oct 23, 2026
@babel-bot

babel-bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

commit: 7f30598

@JLHwung
JLHwung force-pushed the fix-parser-start-node-at-set-loc branch from 5464fb2 to c7fe966 Compare October 23, 2026 18:08
@JLHwung
JLHwung requested a lite review from Copilot October 24, 2026 03:44

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

The reviewed changes and regression fixtures address the reported crashes, with no unresolved blocking issues.

No open issues 🎉

What changed in this PR

This pull request fixes Babel parser crashes for duplicate namespace exports when locations: false, including with the ESTree plugin, by recording locations consistently for nodes created through startNodeAt.

Changes:

  • Moves location bookkeeping into startNodeAt.
  • Guards ESTree location conversion when node.loc is unavailable.
  • Adds regression fixtures for duplicate namespace and default exports with and without locations.
File Description
packages/​babel-parser/​test/​fixtures/​estree/​export/​duplicate-ns-locations-false/​output.json Expected ESTree output and duplicate-export diagnostic.
packages/​babel-parser/​test/​fixtures/​estree/​export/​duplicate-ns-locations-false/​options.json Configures the ESTree case with locations disabled.
packages/​babel-parser/​test/​fixtures/​estree/​export/​duplicate-ns-locations-false/​input.js ESTree duplicate namespace export regression case.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-ns/​output.json Expected output with locations and duplicate-export diagnostics.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-ns/​input.js Duplicate namespace export test case.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-ns-locations-false/​output.json Expected output without location data.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-ns-locations-false/​options.json Configures the namespace export case with locations disabled.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-ns-locations-false/​input.js Location-disabled duplicate namespace export regression case.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-default-ns/​output.json Expected output with locations and duplicate-default diagnostics.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-default-ns/​input.js Duplicate default namespace export test case.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-default-ns-locations-false/​output.json Expected output without location data.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-default-ns-locations-false/​options.json Configures the default export case with locations disabled.
packages/​babel-parser/​test/​fixtures/​es2020/​export-ns-from/​duplicate-default-ns-locations-false/​input.js Location-disabled duplicate default export regression case.
packages/​babel-parser/​src/​plugins/​estree.ts Safely handles nodes without location data.
packages/​babel-parser/​src/​parser/​node.ts Records source locations for all startNodeAt nodes.

Review effort: Lite

@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.

Searching for this.setLoc there are a couple calls that now can be removed (e.g. in parseAwait).

Btw, is .setLoc needed when locations are enabled? If no, can we move it to inside the if statement?

`export { x as z }; export * as z from "y"` with `locations: false`
threw `Error: Attempted to get location data for an index that has not
been set` (in a published build: a SyntaxError at line/column
4294967295) instead of the `DuplicateExport` error. The same happened
for `export default 1; export * as default from "y"`.

When `locations` is off, `raise(node)` recovers the line/column of the
node start through `getLoc(node.start)`, which reads the cache filled by
`setLoc`. Only `startNode()` called `setLoc`; `startNodeAt(loc)` did not,
so a node started at an arbitrary position (here
`maybeParseExportNamespaceSpecifier` starting the
`ExportNamespaceSpecifier` at `lastTokStartLoc`, the `*` token, where no
other node begins) had no cached location and raising at it failed.

Move the `setLoc` call from `startNode` into `startNodeAt`, so that every
node start position is recorded. This also covers the other direct
`startNodeAt(...)` callers whose start position is not necessarily that
of a `startNode()` node: `JSXEmptyExpression` (started at
`lastTokEndLoc`), the TemplateElement start
(`createPositionWithColumnOffset(startLoc, ...)`), the private name
identifier (`createPositionWithColumnOffset(startLoc, 1)`), the
TypeScript named tuple member label, and the module block Program node
started right after the `{` (`startNodeAt(state.endLoc)`).

The new fixtures are exercised with `locations: false` by
test/locations-false.js; without the fix exactly those two tests fail.

Assisted-by: Claude Fable 5.1 <[email protected]>
They are now called in startNodeAt.
@JLHwung
JLHwung force-pushed the fix-parser-start-node-at-set-loc branch from a4be510 to 7f30598 Compare October 25, 2026 19:43
@JLHwung

JLHwung commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Searching for this.setLoc there are a couple calls that now can be removed (e.g. in parseAwait).

Good catch, they are now removed.

Btw, is .setLoc needed when locations are enabled? If no, can we move it to inside the if statement?

Yes it is still required: When locations are true, raise(at: number) will call getLoc(at) and raise the errors in the location. If setLoc is not invoked, .raise will likely throw because getLoc(at) can not find the location corresponding to the index.

During the review session, Claude finds a new bug related to the v8intrinsic plugin, it is now fixed in this PR as well.

@JLHwung
JLHwung requested review from nicolo-ribaudo and a balanced review from Copilot October 25, 2026 19:48

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

The focused location-handling changes have regression coverage for the reported failures, and no blocking issues were identified.

No open issues 🎉

Review effort: Balanced

@liuxingbaoyu liuxingbaoyu 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.

Amazing!
I initially thought this would impact performance, but after running some performance tests, I found that the number of setLoc calls actually decreased.

@JLHwung
JLHwung merged commit 368821f into babel:main Oct 26, 2026
56 checks passed
@JLHwung
JLHwung deleted the fix-parser-start-node-at-set-loc branch October 26, 2026 11:44
@babel-bot

Copy link
Copy Markdown
Collaborator

⚠️ The automatic backport to 7.x failed.

The fix doesn't apply to 7.x. There, Node always builds loc from the start position, and there is no setLoc/getLoc cache or locations: false handling in the parser. The crash being fixed therefore can't occur on 7.x.

Conflicting files:

  • packages/babel-parser/src/parser/node.ts: startNode and startNodeAt are structured differently on 7.x. The setLoc call and the OptionFlags.Locations branch don't exist there.
  • packages/babel-parser/src/parser/expression.ts: the surrounding code differs on 7.x (potentialArrowAt, no soloAwait), and the hunks only remove setLoc calls that don't exist on 7.x.
  • packages/babel-parser/src/plugins/estree.ts: 7.x's toESTreeLocation is a different implementation, using toUnenumerable, and always has node.loc.

This PR needs to be backported manually, if it is needed at all. I'd expect it isn't, because 7.x isn't affected.

Generated by 🔙 Backport to 7.x for #18407 · copilot · auto · 14.5 AIC · ⌖ 0.558 AIC · ⊞ 9.1K · ◷

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

7.x: needs backport 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.

5 participants