Repository navigation
fix(generator): avoid newlines and parens after no-LineTerminator tokens - #18728
Merged
Merged
Conversation
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63841 |
|
commit: |
Base automatically changed from
jlhwung/fix-generator-preserve-format-type-arguments-gt
to
main
November 25, 2026 21:12
JLHwung
force-pushed
the
jlhwung/fix-generator-comments-after-keywords
branch
2 times, most recently
from
November 25, 2026 21:28
c5ca2b1 to
e6e2e28
Compare
Some tokens cannot be followed by a line terminator, but the generator
could still print one after them, or wrap the next node in parentheses
where parentheses are not allowed:
- `await` in `await using` was not marked as such a token. When the
declaration spans multiple lines, a comment after `await` was
followed by a newline:
await /* c */ using a = b(
c
);
printed `using a = b(c);` on its own line, which does not parse.
- With `retainLines`, a comment on a later line could be printed right
after `async` or `accessor`, on its original line:
async *a(
// c
) {}
accessor [b]
// c
= 1;
The line break after the keyword turned it into a field named `async`
or `accessor` in a class, and into a syntax error in an object. The
printer no longer catches up to a later line right after such a
token.
- A node printed after such a token is wrapped in parentheses when it
has a leading comment containing a line terminator or, with
`retainLines`, when it starts on a later line. This was done even for
nodes that are neither expressions nor types, which cannot be
parenthesized. With Flow,
class A implements B<T> // c
{}
(x: mixed): boolean // c
%checks => !!x;
printed `({})` as the class body and `(%checks)` as the predicate.
Such comments are now printed after the node instead. No line
terminator is allowed between the predicate and `=>` either, so
there they are printed as block comments, and those spanning
multiple lines are dropped.
Assisted-by: Claude Opus 5.5
Assisted-by: Claude Fable 5.2
JLHwung
force-pushed
the
jlhwung/fix-generator-comments-after-keywords
branch
from
November 25, 2026 21:31
e6e2e28 to
b6a6e06
Compare
nicolo-ribaudo
approved these changes
Nov 26, 2026
Collaborator
|
Backported to 7.x in #18749. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In this PR we fix a few issues caught by the round trip tests:
awaitinawait usingwas not marked as an NLTH token. When the declaration spans multiple lines, a comment afterawaitwas followed by a line break:was printed as
which is invalid.
With
retainLines, a comment on a later line could be printed right after theasyncoraccessortoken on its original line:The line break after the keyword turned it into a field named
asyncoraccessorin a class, and into a syntax error in an object. The printer no longer catches up to a later line right after such a token.A node printed after such a token is wrapped in parentheses when it has a leading comment containing a line terminator or, with
retainLines, when it starts on a later line. This was done even for nodes that are neither expressions nor types, which cannot be parenthesized. With theFlowplugin,printed
({})as the class body and(%checks)as the predicate. WithretainLines, the class body was wrapped even without a comment. They are now printed asclass A implements B<T> /* c*/ {}andboolean /* c*/ %checks => !!x.A comment spanning multiple lines cannot stay before such a node. It is printed after the class body. Before
%checksit is dropped, since no line terminator is allowed between the predicate and=>either.