Skip to content

fix(generator): add roundtrip test and fix reported bugs - #18546

Merged
JLHwung merged 8 commits into
mainfrom
jlhwung/fix-generator-roundtrip
Nov 18, 2026
Merged

JLHwung merged 8 commits into
mainfrom
jlhwung/fix-generator-roundtrip

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Added a roundtrip generator test and fixed reported bugs
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 add a roundtrip test for babel-generator. The test generate code from every generator and parser fixture, parse it again and compare the two ASTs, ignoring locations, comments, tokens and extra keys.

Then we fix all reported bugs from the roundtrip test one-by-one. This PR does not fix the ts arrow ambiguity parsing, which is already tracked in #18268.

The roundtrip test has been extracted as a test helper so that we can add the roundtrip test for other generator options, such as minified, retainLines and compact. These roundtrip tests ensures that such generator options do not change the AST in an unexpected way.

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

babel-bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

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

declare: {
validate: assertValueType("boolean"),
optional: true,
},

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.

declare is added to the ClassPrivateProperty node since both TS and flow have supported this feature.

@JLHwung
JLHwung added this pull request to stack #18548 November 7, 2026 04:11
@pkg-pr-new

pkg-pr-new Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 7cdc24c

@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-roundtrip branch from 9a64c89 to 6c87abd Compare November 16, 2026 16:00
Generate code from every generator and parser fixture, with the
fixture's own generator options, parse it again and compare the two
ASTs, ignoring locations, comments, tokens and `extra` keys that are
expected to change. The fixture loading and comparison live in
`test/helpers` so that other format options can reuse them.

Assisted-by: Claude Opus 5.5
Add fixtures for the computed, assignment, `!!` and
instantiation-expression forms of `(a?.b!).c`, whose parentheses #18570
keeps, and for the parentheses that can be dropped before `?.`.

Assisted-by: Claude Opus 5.5
- `(throw a);`: a throw expression at the start of an expression
  statement, otherwise it becomes a throw statement.
- `export default (from);`, which `exportDefaultFrom` rejects without
  parentheses.
- `(<a />) < b` and `(a as B) < c`: `<` after a JSX element starts
  another JSX element, and an operator starting with `<` after a TS
  type starts type arguments.

The argument-less F# pipeline `await` (`x |> await`) is never
parenthesized, since `x |> (await)` is a syntax error, and is printed
without a trailing space.

Assisted-by: Claude Opus 5.5
`type A = | B` is a TSUnionType with one member; printing it as `B`
re-parses as a plain type reference. A single-member union nested in
a union (or intersection in an intersection) is parenthesized so that
its leading operator does not merge with the parent: `A | (| B)`.

Assisted-by: Claude Opus 5.5
`declare #a` and `+#b` lost their modifiers. Add the `declare` field
to ClassPrivateProperty in @babel/types, since the Flow parser sets it.

Print the variance through its Variance node, so that
`experimental_preserveFormat` finds the `+`/`-` token: `static +a`
was printed as `static+ a`.

With `createParenthesizedExpressions`, `(a: T)` is a ParenthesizedExpression
around a TypeCastExpression sharing one pair of parentheses; do not print
a second pair.

Assisted-by: Claude Opus 5.5
`import %%FILE%% with { type: "json" }` and
`import %%A%% from "x" with { type: "json" }` threw "Missing
semicolon", and the placeholder imports had no `attributes` array.
Placeholder imports also did not mark the file as a module under
`sourceType: "unambiguous"`.

Assisted-by: Claude Opus 5.5
`class A { +[x]: T }` was printed as `class A { [x]: T; }`: the variance
was only printed for non-computed keys.

Assisted-by: Claude Opus 5.5
ClassPrivateProperty now has the `declare` field, so Prettier's
`@ts-expect-error` before `!nextNode.declare` in class-body.js is
unused and `yarn lint:typecheck` fails. Remove it until Prettier
updates to a Babel release with this change.

Assisted-by: Claude Opus 5.5
@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-roundtrip branch from 6c87abd to 7cdc24c Compare November 17, 2026 14:46
// Parser fixtures whose AST is expected to change shape, relative to
// `babel-parser/test/fixtures`
const PARSER_FAILURES = [
// `({ a: a = 1 } = {})` is printed as `({ 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.

Is this by design?

@JLHwung JLHwung Nov 18, 2026 •

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, this is generator's behaviour since Babel 6.0.0, so the roundtrip is not possible on this case since it changed the shorthand property:

// print `({ foo: foo = 5 } = {})` as `({ foo = 5 } = {});`
if (
isAssignmentPattern(node.value) &&
isIdentifier(node.key) &&
// @ts-expect-error todo(flow->ts) `.name` does not exist on some types in union
node.key.name === node.value.left.name
) {
this.print(node.value);
return;
}

@JLHwung

JLHwung commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

https://babeljs.io/repl/#?config_lz=N4IgZglgNgpgdgQwLYxALhAJxgBygOgCsBnEAGhDwFcBzCOUtAbSZAAEAjBDmKAemp04AWmIBPOABcEAD2GSxOGMQDGmCDknlgAXwC6ZVp268BUWvVETpckjO36DIYgHsqmFTAAqi1BiQuACZUsCA6QA&code_lz=BQMwPAKgfAlABGAvHAHgbgLAChSVgsVTHAQzgGo5xp5D1sGsg&lineWrap=true&version=8.1.2

AI reported this issue, they were unable to perform secondary parsing. :)

Thank you for the report. Since this is already failing on main and this PR does not make it worse, I prefer to fix it on a new PR stacked on the roundtrip test series so as to keep this PR focused and the fix PR would then have to pass more roundtrip test as well. This PR only introduce the basic roundtrip test and make the new test pass. If there is regression introduced in this PR, I would like to fix them in this PR.

@JLHwung
JLHwung added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit 4517028 Nov 18, 2026
47 checks passed
@JLHwung
JLHwung deleted the jlhwung/fix-generator-roundtrip branch November 18, 2026 18:53
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18657.

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.

3 participants