Repository navigation
Fix synthesized comment emission with comment flags - #64488
Andrew Branch (andrewbranch) merged 1 commit into
Conversation
| } | ||
| } | ||
|
|
||
| func TestSynthesizedCommentsIgnoreNoCommentsEmitFlags(t *testing.T) { |
There was a problem hiding this comment.
We only add unit tests when we really really have to. You should test this with a compiler test if possible.
There was a problem hiding this comment.
Though, it was an API based report, so...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
No, there isn't something like that.
There was a problem hiding this comment.
We don't even have a Go API at the moment. So it's a bit of an odd ask, sort of
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
hasSyntheticLeadingCommentIntroducingNewLinewhich 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.
…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.
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.
2f32369 to
d4eaf3e
Compare
There was a problem hiding this comment.
🟢 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, orNoCommentsis 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
Andrew Branch (andrewbranch)
left a comment
There was a problem hiding this comment.
Thanks for the update!
Fixes #64453
This restores Strada-compatible synthesized comment emission behavior.
EFNoLeadingCommentsandEFNoTrailingCommentsnow 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
EFNoLeadingCommentsdoes not prevent a synthesized comment from affecting printer formatting. The regression test covers areturnexpression 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.