Skip to content

fix: handle TSInstantiation before < - #18658

Merged
JLHwung merged 2 commits into
mainfrom
jlhwung/fix-generator-ts-instantiation-before-lt
Nov 25, 2026
Merged

JLHwung merged 2 commits into
mainfrom
jlhwung/fix-generator-ts-instantiation-before-lt

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 18, 2026

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? #18546 (comment)
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 issues reported during the review session in #18546.

In the first commit we handle ambiguous operators after f<T>: When the left hand side of a binary operator ends with f<T>, the generator now adds parentheses before these operators: < (and <=, <<) with JSX, since < after > starts a JSX element.

To pass the minified roundtrip, we also add a space after f<T> when printing f<T> == x since f<T>==x does not parse (tsc also rejects this case)

In the second commit we handle type arguments after f<T>: For example (f<T>)<U>x` now keeps its parentheses.

f<T> within call expression arguments are no longer parenthesized, for example g<U>(f<T>) used to print g<U>((f<T>)) and now prints g<U>(f<T>).

@JLHwung
JLHwung added this pull request to stack #18548 November 18, 2026 19:51
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator 7.x: needs backport labels Nov 18, 2026
@JLHwung
JLHwung requested a review from liuxingbaoyu November 18, 2026 19:51
@babel-bot

babel-bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

commit: c1c1bec

@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-ts-instantiation-before-lt branch 2 times, most recently from 3adac16 to 42dde89 Compare November 21, 2026 00:26
@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-ts-instantiation-before-lt branch 2 times, most recently from 9b81e0c to 21fa83e Compare November 21, 2026 14:21
Base automatically changed from jlhwung/test-generator-compact-roundtrip to main November 21, 2026 16:03
@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-ts-instantiation-before-lt branch from 21fa83e to f32a1fb Compare November 21, 2026 16:03
…us operators

`(f<T>) <= x` was printed as `f<T> <= x`, which fails to parse with JSX
enabled because `<` after `>` starts a JSX element. Likewise `>`, `>>`
and `>>>` after type arguments are not valid instantiation expressions,
and `+`, `-` and `%` can start an expression, so `f<T> + x` reparses as
`(f < T) > +x`. Parenthesize the left operand when it ends with an
instantiation expression, also inside a type assertion such as
`(<X>f<T>) - x`, and the operator is one of these.

When minified, also separate `>` from a following `=` or `>`, so that
`f<T> == x` is not printed as `f<T>==x` (`f < T >= =x`), and keep the
`()` of `new (f<T>)()`, so that it does not end with type arguments
(`new f<T>-x`).

Assisted-by: Claude Opus 5.5
…guments

`(f<T>)<U>`x`` was printed as `f<T><U>`x``, which is not parsed as a
tagged template of an instantiation expression. Keep the parentheses when
the tagged template has its own type arguments, as already done for
calls and `new`, and stop adding them around arguments of such calls:
`g<U>(f<T>)` was printed as `g<U>((f<T>))`.

Without type arguments on the parent, `(f<T>)()`, `new (f<T>)()` and
`(f<T>)`x`` are still printed without parentheses: the type arguments
move to the call or tagged template, which has the same runtime
semantics. Add a fixture for them, allowed to change AST shape in the
roundtrip tests; experimental_preserveFormat keeps their parentheses.

Assisted-by: Claude Opus 5.5
@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-ts-instantiation-before-lt branch from f32a1fb to c1c1bec Compare November 22, 2026 04:11

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

When experimental_preserveFormat is enabled, x as A<B>>c is not currently fully consistent with the source code.
We can include this in a separate PR. :)
repl

@JLHwung

JLHwung commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

When experimental_preserveFormat is enabled, x as A<B>>c is not currently fully consistent with the source code. We can include this in a separate PR. :) repl

Thank you, this will be fixed in #18723.

@JLHwung
JLHwung added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 0a06c30 Nov 25, 2026
40 checks passed
@JLHwung
JLHwung deleted the jlhwung/fix-generator-ts-instantiation-before-lt branch November 25, 2026 16:49
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18724.

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