Skip to content

fix: only separate > from = & > after TS instantiation expression - #18723

Merged
JLHwung merged 2 commits into
mainfrom
jlhwung/fix-generator-preserve-format-type-arguments-gt
Nov 25, 2026
Merged

JLHwung merged 2 commits into
mainfrom
jlhwung/fix-generator-preserve-format-type-arguments-gt

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? #18658 (review)
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? No
License MIT

As a follow-up to #18658, we fix an issue reported in the review comment #18658 (review): that x as A<B>>c does not roundtrip when experimental_preserveFormat is enabled:

The space added between a > and an operator starting with = or > is only needed when the > closes the type arguments of an instantiation expression. So we add a new last-char enum kind TS_INSTANTIATION_TYPE_ARGUMENTS.

@JLHwung
JLHwung added this pull request to stack #18548 November 25, 2026 16:38
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator 7.x: needs backport labels Nov 25, 2026
@JLHwung
JLHwung requested a review from liuxingbaoyu November 25, 2026 16:38
this.print(node.typeArguments);
// Unlike after other type arguments, a `>` or `=` printed right after these
// would be tokenized together with the closing `>`: see Printer#token.
this.setLastChar(-4 /* LAST_CHAR_KINDS.TS_INSTANTIATION_TYPE_ARGUMENTS */);

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.

Here we write the literal by hand to avoid the runtime object in this hot path. We should figure out how to make exported TS const enums works in the build pipeline.

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.

babel/Gulpfile.ts

Lines 568 to 571 in 68cfec5

["packages/babel-parser", "packages/babel-generator"].includes(
src
) &&
getBabelOutputPlugin({

It doesn't work? I remember that before it was because Babel 7 didn't have bundled generators, so I had to use numeric literals.

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.

Oh you are right. I forgot about this plugin. I will update the usage here. I guess we can just make this plugin a global rollup plugin, not limited to the parser/generator.

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.

Hmm, that plugin does not work with negative enum values like -1 because we only have the isNumericLiteral check, I will update that plugin and push a new commit.

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.

Well, it shouldn't have any side effects, I just remember it being a bit slow. 🤦‍♂️

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.

It's okay, we just improved the build performance, we will have more budget for that plugin.

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.

The plugin is fixed in 81f7769, ptal.

@babel-bot

babel-bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

commit: fc64cc1

Base automatically changed from jlhwung/fix-generator-ts-instantiation-before-lt to main November 25, 2026 16:49
@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-preserve-format-type-arguments-gt branch from 1baa49d to 3666960 Compare November 25, 2026 16:49
The bundle-time inliner for `@babel/parser` and `@babel/generator` only
accepted numeric literals, so a const enum with negative members (`-1`
parses as a unary expression) stayed an object with property reads in
the bundle.

Assisted-by: Claude Opus 5.5
…n expressions

The space added between a `>` and an operator starting with `=` or `>` is
only needed when the `>` closes the type arguments of an instantiation
expression: `f<T>==x` is tokenized as `f < T >= =x`. Any other `>` is a
token on its own, so `x as A<B>>c` and `<a />==c` are valid as written.

Applying it to every `>` made `experimental_preserveFormat` print
`x as A<B>>c` as `x as A<B> >c`, and added a redundant space to the
minified output of such expressions.

`LAST_CHAR_KINDS` moves to buffer.ts and is exported, so the generators
use it instead of hand-inlined literals. The bundle still contains the
plain values.

Assisted-by: Claude Fable 5.2
@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-preserve-format-type-arguments-gt branch from 3666960 to fc64cc1 Compare November 25, 2026 16:55

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

Thanks!

@JLHwung
JLHwung added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit f8345b3 Nov 25, 2026
41 checks passed
@JLHwung
JLHwung deleted the jlhwung/fix-generator-preserve-format-type-arguments-gt branch November 25, 2026 21:12
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18729.

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