Skip to content

fix(generator): avoid newlines and parens after no-LineTerminator tokens - #18728

Merged
JLHwung merged 1 commit into
mainfrom
jlhwung/fix-generator-comments-after-keywords
Nov 26, 2026
Merged

JLHwung merged 1 commit into
mainfrom
jlhwung/fix-generator-comments-after-keywords

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? avoid new lines and parentheses after NLTH [no LineTerminator here] tokens
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 a few issues caught by the round trip tests:

  • await in await using was not marked as an NLTH token. When the declaration spans multiple lines, a comment after await was followed by a line break:

    await /* c */ using a = b(
      c
    );

    was printed as

    await /* c */
    using a = b(c);

    which is invalid.

  • With retainLines, a comment on a later line could be printed right after the async or accessor token 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 the Flow plugin,

    class A implements B<T> // c
    {}
    
    (x: mixed): boolean // c
    %checks => !!x;
    

    printed ({}) as the class body and (%checks) as the predicate. With retainLines, the class body was wrapped even without a comment. They are now printed as class A implements B<T> /* c*/ {} and boolean /* c*/ %checks => !!x.

    A comment spanning multiple lines cannot stay before such a node. It is printed after the class body. Before %checks it is dropped, since no line terminator is allowed between the predicate and => either.

@JLHwung
JLHwung added this pull request to stack #18548 November 25, 2026 21:05
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator labels Nov 25, 2026
@JLHwung
JLHwung requested a review from liuxingbaoyu November 25, 2026 21:06
@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/63841

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

commit: b6a6e06

Base automatically changed from jlhwung/fix-generator-preserve-format-type-arguments-gt to main November 25, 2026 21:12
@JLHwung
JLHwung force-pushed the jlhwung/fix-generator-comments-after-keywords branch 2 times, most recently from c5ca2b1 to e6e2e28 Compare November 25, 2026 21:28
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
JLHwung force-pushed the jlhwung/fix-generator-comments-after-keywords branch from e6e2e28 to b6a6e06 Compare November 25, 2026 21:31
@JLHwung
JLHwung added this pull request to the merge queue Nov 26, 2026
Merged via the queue into main with commit c1a6cbd Nov 26, 2026
48 checks passed
@JLHwung
JLHwung deleted the jlhwung/fix-generator-comments-after-keywords branch November 26, 2026 18:08
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18749.

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