Skip to content

fix(generator): escape of binding of a using declaration in for-of head - #18430

Merged
nicolo-ribaudo merged 2 commits into
babel:mainfrom
JLHwung:fix-generator-escaped-contextual-identifier
Nov 2, 2026
Merged

nicolo-ribaudo merged 2 commits into
babel:mainfrom
JLHwung:fix-generator-escaped-contextual-identifier

Conversation

@JLHwung

@JLHwung JLHwung commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel generates invalid code for input for (using \u006ff of y);, closes #15117.
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 Fable: for (using \u{6f}f of y); was printed as for (using of of y);, which does not parse. Here we set a TokenContext.usingForOfBinding flag when printing the for-of head, and let the Identifier printer emit an escaped of for a binding named of in that context. Cases like await using, for (using of = 1;;) and using of = 1; are not affected.

Assisted-by: Claude Fable 5.1

/cc @fisker I updated the PR description to close #15117: I think the contextual identifier case should be fixed in generators rather than the parser: Even if we can add the .raw data to the AST, it risks out-of-sync information when the AST is modified: because the identifier builder t.identifier now has to check the surrounding context to decide if it should escape the of or not. The generator fix ensures that ultimately, no matter how the AST is manipulated, as long as the AST is still valid, the output JS code is always valid.

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

babel-bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

commit: c21e875

@codspeed

codspeed Bot commented Oct 27, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 39.04%

2 warnings

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

❌ 26 regressed benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ babel-generator/real-case.mjs ts-parser.ts @ current 5.3 ms 10.9 ms -51.29%
❌ babel-generator/real-case.mjs babel-parser-express.ts @ current 1.3 ms 2.6 ms -50.27%
❌ babel-generator/real-case.mjs ts-parser.mjs @ current 4.2 ms 8.3 ms -49.71%
❌ babel-traverse/real-case.mjs babel-parser-express.ts @ current 5.4 ms 10.4 ms -48.14%
❌ babel-parser/real-case.mjs babel-parser-express.ts @ current 2.5 ms 4.7 ms -46.55%
❌ babel-generator/real-case.mjs jquery-3.6.js @ current 4.2 ms 7.9 ms -46.46%
❌ all/real-case-ts-mjs.mjs babel-parser-express.ts @ current 12.2 ms 21.8 ms -44.33%
❌ babel-parser/real-case.mjs ts-parser.mjs @ current 7.8 ms 13.8 ms -43.58%
❌ babel-parser/real-case.mjs jquery-3.6.js @ current 7.6 ms 13 ms -41.69%
❌ babel-parser/real-case.mjs ts-parser.ts @ current 16.3 ms 27.8 ms -41.58%
❌ babel-parser/real-case.mjs ts-checker.ts @ current 120 ms 203 ms -40.9%
❌ babel-parser/real-case.mjs typescript-5.6.2.js @ current 406.6 ms 669.9 ms -39.3%
❌ all/real-case-ts-mjs.mjs ts-parser.ts @ current 100.3 ms 160.2 ms -37.4%
❌ all/real-case-js-node6.mjs jquery-3.6.js @ current 84.2 ms 134.3 ms -37.27%
❌ all/real-case-mjs-cjs.mjs ts-parser.mjs @ current 105.4 ms 167.5 ms -37.12%
❌ babel-parser/real-case.mjs ts-checker.mjs @ current 93.7 ms 148.1 ms -36.74%
❌ babel-traverse/real-case.mjs ts-parser.ts @ current 43.7 ms 68.1 ms -35.78%
❌ all/real-case-js-node6.mjs typescript-5.6.2.js @ current 2.8 s 4.2 s -34.08%
❌ all/real-case-mjs-cjs.mjs ts-checker.mjs @ current 889.7 ms 1,324.7 ms -32.84%
❌ babel-generator/real-case.mjs typescript-5.6.2.js @ current 257.1 ms 380.5 ms -32.44%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression with the CodSpeed MCP and your agent.


Comparing JLHwung:fix-generator-escaped-contextual-identifier (07e8205) with main (70b843f)

Open in CodSpeed

@fisker

fisker commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

For this of case, I think it's acceptable to escape the first letter, but for long keywords, people may expect the original input they wrote instead of choosing a random character to escape by printer. Anyway, it's understandable to close #15117, thanks for your work!

@JLHwung

JLHwung commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

people may expect the original input they wrote instead of choosing a random character to escape by printer.

Indeed, and I think the preserveFormat mode is exactly for such purpose. I will check if we can preserve the escape format of different of.

this.word(this.tokenMap ? lastRawIdentResult : node.name);
let name = this.tokenMap ? lastRawIdentResult : node.name;
if (name === "of" && this.tokenContext & TokenContext.usingForOfBinding) {
name = "\\u006ff";

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.

Very nit-picky, but I'd prefer if we escaped the f instead of the o. I feel like if it at least starts with a normal identifier character it will be more obvious that this is just a "normal" identifier.

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.

Fixed, ptal.

@JLHwung
JLHwung force-pushed the fix-generator-escaped-contextual-identifier branch 2 times, most recently from 7fa7fe6 to db43d50 Compare October 30, 2026 13:59
@JLHwung
JLHwung requested a review from liuxingbaoyu October 31, 2026 18:24
…head

`for (using \u{6f}f of y);` was printed as `for (using of of y);`, which
does not parse: the token pair `using of` is excluded by lookahead at the
start of a for-of head so that `for (using of x)` keeps meaning "iterate
with the identifier `using`", so the binding `of` must be spelled with a
Unicode escape (the escape is not retained in the AST). Parentheses are
not an option because a binding identifier cannot be parenthesized.

Set a `TokenContext.usingForOfBinding` flag while printing the binding of
a `using` declaration that is the left of a ForOfStatement, and make the
Identifier printer emit `of` for a binding named `of` in that context.
`await using`, `for (using of = 1;;)` and `using of = 1;` are unaffected.

Assisted-by: Claude Fable 5.1
@JLHwung
JLHwung force-pushed the fix-generator-escaped-contextual-identifier branch from db43d50 to c21e875 Compare November 2, 2026 15:36
@nicolo-ribaudo

Copy link
Copy Markdown
Member

(merging this as it's a low-risk one and I want to see the test262 cache in action)

@nicolo-ribaudo
nicolo-ribaudo added this pull request to the merge queue Nov 2, 2026
Merged via the queue into babel:main with commit 0750fe6 Nov 2, 2026
60 checks passed
@nicolo-ribaudo
nicolo-ribaudo deleted the fix-generator-escaped-contextual-identifier branch November 2, 2026 21:02
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18500.

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

Labels

pkg: generator 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.

Add .raw or .extra.raw to Identifier

4 participants