Repository navigation
fix(generator): add roundtrip test and fix reported bugs - #18546
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63392 |
| declare: { | ||
| validate: assertValueType("boolean"), | ||
| optional: true, | ||
| }, |
There was a problem hiding this comment.
declare is added to the ClassPrivateProperty node since both TS and flow have supported this feature.
|
commit: |
9a64c89 to
6c87abd
Compare
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
6c87abd to
7cdc24c
Compare
|
AI reported this issue, they were unable to perform secondary parsing. :) |
| // 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 } = {})` |
There was a problem hiding this comment.
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:
babel/packages/babel-generator/src/generators/types.ts
Lines 87 to 96 in 7574415
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. |
|
Backported to 7.x in #18657. |
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
extrakeys.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,retainLinesandcompact. These roundtrip tests ensures that such generator options do not change the AST in an unexpected way.