Skip to content

Fix synthesized comment emission with comment flags - #64488

Merged
Andrew Branch (andrewbranch) merged 1 commit into
microsoft:mainfrom
z0rimo:fix/64453-synthetic-comments
Nov 23, 2026
Merged

Andrew Branch (andrewbranch) merged 1 commit into
microsoft:mainfrom
z0rimo:fix/64453-synthetic-comments

Conversation

@z0rimo

@z0rimo S.H Jeong (z0rimo) commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #64453

This restores Strada-compatible synthesized comment emission behavior.

EFNoLeadingComments and EFNoTrailingComments now suppress only comments from the source text, while synthesized replacement comments continue to be emitted.

Regression coverage now runs through the public JS API in both the sync and async suites. The tests cover leading and trailing comments, including EFNoComments, and verify that source comments are suppressed while synthesized comments are preserved.

The change also updates the newline-sensitive synthesized-leading-comment path so that EFNoLeadingComments does not prevent a synthesized comment from affecting printer formatting. The regression test covers a return expression where missing this handling could change the emitted program's semantics.

AI assistance was used during investigation and implementation. I reviewed the patch, traced the printer behavior, and verified the change with focused API tests and the repository's validation commands locally.

@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 28, 2026
Comment thread tsc/internal/printer/printer_test.go Outdated
}
}

func TestSynthesizedCommentsIgnoreNoCommentsEmitFlags(t *testing.T) {

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.

We only add unit tests when we really really have to. You should test this with a compiler test if possible.

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.

Though, it was an API based report, so...

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.

Yeah, that was my concern as well. Since the behavior under test requires attaching synthesized comments through the emit/printer API, I couldn't find a source-only compiler test that exercises the same path.

I used the printer unit test here so it can explicitly combine a real source comment, the emit flag, and a synthesized replacement comment.

Happy to move this to a compiler test if there's an existing harness/path that can exercise that API behavior.

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.

No, there isn't something like that.

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.

We don't even have a Go API at the moment. So it's a bit of an odd ask, sort of

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.

Now that #65447 is in, this bug is observable with the public JS API, and therefore can be tested with an end to end API test. That PR also added hasSyntheticLeadingCommentIntroducingNewLine which needs the same treatment.

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.

Now that #65447 is in, this bug is observable with the public JS API, and therefore can be tested with an end to end API test. That PR also added hasSyntheticLeadingCommentIntroducingNewLine which needs the same treatment.

Updated based on this feedback.

I moved the regression coverage from the Go printer unit test to the public JS API tests added by #65447, covering both sync and async APIs. The tests verify that NoLeadingComments / NoTrailingComments suppress source comments while synthesized replacements are preserved.

I also updated hasSyntheticLeadingCommentIntroducingNewLine so NoLeadingComments no longer suppresses newline handling for synthesized comments. The added regression catches the formatting case where failing to do so can change a return statement's semantics.

npx hereby test, npx hereby lint, npx hereby check:format, and git diff --check all pass.

Trevor Florence (trevorade) added a commit to trevorade/TypeScript that referenced this pull request Oct 11, 2026
…is added

Strada clears them on the enum and namespace statements when it emits a
leading `var` (ts.ts, `visitEnumDeclaration` and `visitModuleDeclaration`),
so the comments are emitted once, on the `var` statement. tsgo relied on
`EFNoLeadingComments` suppressing synthesized comments instead, which microsoft#64453
reports as a divergence from Strada and microsoft#64488 would remove.
Trevor Florence (trevorade) added a commit to trevorade/TypeScript that referenced this pull request Oct 11, 2026
When a static property initializer becomes `static { this.x = ...; }`,
the property's comments are emitted on the static block. Strada clears
them on the initializer statement (classFields.ts,
`transformPropertyOrClassStaticBlock` caller in `visitPropertyDeclaration`).
tsgo only set `EFNoComments`, which stops suppressing synthetic comments
once microsoft#64488 lands, so the comments would be printed twice.
@z0rimo
S.H Jeong (z0rimo) force-pushed the fix/64453-synthetic-comments branch from 2f32369 to d4eaf3e Compare November 22, 2026 14:21
@z0rimo
S.H Jeong (z0rimo) marked this pull request as ready for review November 22, 2026 14:21
Copilot AI balanced review requested due to automatic review settings November 22, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change preserving synthesized comments when source comments are suppressed.

No open issues 🎉

What changed in this PR

Fixes #64453 by restoring synthesized comment emission when source-comment suppression flags are set, preserving replacement comments added through the printer API.

Changes:

  • Keeps synthesized leading and trailing comments when NoLeadingComments, NoTrailingComments, or NoComments is set.
  • Updates newline detection so synthesized comments still trigger required parenthesization.
  • Adds synchronous and asynchronous API regression tests for comment suppression and newline handling.
File Description
tsc/​internal/​printer/​printer.go Removes flag checks from synthesized comment emission and newline detection.
packages/​typescript/​test/​sync/​api.test.ts Adds synchronous tests for replacement comments and newline-safe parenthesization.
packages/​typescript/​test/​async/​api.test.ts Adds matching asynchronous API regression tests.

Review effort: Balanced

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 for the update!

@andrewbranch
Andrew Branch (andrewbranch) added this pull request to the merge queue Nov 23, 2026
Merged via the queue into microsoft:main with commit 6a35a39 Nov 23, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EFNoLeadingComments suppresses synthesized leading comments in tsgo; Strada only suppresses source comments

5 participants