Skip to content

Add support for setting emit flags and synthetic comments - #65447

Merged
Andrew Branch (andrewbranch) merged 9 commits into
microsoft:mainfrom
andrewbranch:emit-flags-continuation
Nov 19, 2026
Merged

Andrew Branch (andrewbranch) merged 9 commits into
microsoft:mainfrom
andrewbranch:emit-flags-continuation

Conversation

@andrewbranch

Copy link
Copy Markdown
Member

Continuation of #64500
Fixes #64429

  • Addresses Wesley Wigham (@weswigham)'s review comment
  • Extracts non-async-specific code out of async files for deduplication
  • Fixes merge conflicts
  • The Copilot review comments were pre-existing flaws in the emitter. I'll open a follow-up PR for them.

…nodes

# Conflicts:
#	packages/typescript/src/api/async/api.ts
#	packages/typescript/src/api/proto.generated.ts
#	packages/typescript/src/api/sync/api.ts
#	packages/typescript/test/async/api.test.ts
#	packages/typescript/test/sync/api.test.ts
- Null and unknown emitNodes entries are rejected as client errors
  instead of panicking.
- EmitFlags exposes None and the composed flags, and None resets a node.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 23:30
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 11, 2026
@typescript-automation typescript-automation Bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 11, 2026

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 were identified for the emit-flag and synthetic-comment API changes.

No open issues 🎉

1 file not reviewed
  • tsc/internal/api/enum_values_generated.go: Generated file
What changed in this PR

Adds emit flags and synthetic-comment support to the native TypeScript compiler’s JavaScript printer API, addressing #64429 and continuing #64500.

Changes:

  • Introduces a shared EmitContext for attaching flags and comments to nodes through both async and sync APIs.
  • Transfers node metadata to the native printer, validates incoming metadata, and handles comment-induced line breaks.
  • Adds API and printer tests, plus generated EmitFlags exports.
File Description
tsc/​internal/​printer/​printer.go Enables source-less synthetic comments and handles line breaks safely.
tsc/​internal/​printer/​printer_test.go Tests comment-induced line breaks after return and throw.
tsc/​internal/​printer/​emitflags.go Groups flag constants for enum generation.
tsc/​internal/​api/​session.go Validates and applies client metadata before printing.
tsc/​internal/​api/​session_printnode_test.go Tests metadata decoding, validation, and comment printing.
tsc/​internal/​api/​proto.go Defines protocol fields for emit flags and comments.
tsc/​internal/​api/​enum_values_generated.go Includes native emit-flag values in generated enum data.
tsc/​internal/​api/​encoder/​decoder.go Adds a callback for associating metadata with decoded nodes.
tools/​scripts/​tsc/​generate-enums.ts Registers EmitFlags for generation.
packages/​typescript/​test/​sync/​api.test.ts Adds generated sync tests for flags and comments.
packages/​typescript/​test/​async/​api.test.ts Tests emit contexts, formatting flags, and synthetic comments.
packages/​typescript/​src/​api/​sync/​api.ts Exposes emit contexts through the generated sync printer API.
packages/​typescript/​src/​api/​proto.generated.ts Adds generated protocol types for printing metadata.
packages/​typescript/​src/​api/​printer.ts Centralizes emit-context storage and print-request encoding.
packages/​typescript/​src/​api/​node/​encoder.ts Exposes encoded node indices through a callback.
packages/​typescript/​src/​api/​async/​api.ts Integrates shared emit contexts into the async printer API.
packages/​typescript/​enums/​emitFlags.js Provides the generated runtime EmitFlags enum.
packages/​typescript/​enums/​emitFlags.d.ts Provides generated EmitFlags type declarations.

Review effort: Balanced

@weswigham Wesley Wigham (weswigham) 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.

Unsure if the new field is just misnamed or under/mis-used, tbh.

func (p *Printer) syntheticCommentWillEmitNewLine(comment SynthesizedComment) bool {
return comment.Kind == ast.KindSingleLineCommentTrivia || comment.HasTrailingNewLine
return comment.Kind == ast.KindSingleLineCommentTrivia ||
comment.HasLeadingNewLine || comment.HasTrailingNewLine ||

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.

comment.HasLeadingNewLine is used to override the result of syntheticCommentWillEmitNewLine, but as far as I can tell, not to actually emit a leading newline for a comment with it set. Seems like a mistake?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's respected in emitLeadingSynthesizedComment, and this predicate is only called for leading synthesized comments, so maybe this method should be renamed for clarity.

Separately, emission of trailing synthesized comments doesn't check HasLeadingNewLine, and it arguably should, but it doesn't mean disagreement with this predicate, because it doesn't get used in that path.

I'll push a commit with the rename and with the trailing emission fix.

@weswigham Wesley Wigham (weswigham) 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.

Maybe it's a bit weird to have both HasLeadingNewLine and HasTrailingNewLine when they're mutually exclusive? (A leading comment can have a leading newline, a trailing comment can have a trailing one, as you said.) This works but I guess just seems a bit redundant - renaming the old HasTrailingNewLine to AddsNewLine and using it for both cases seems like it'd have been a bit cleaner?

@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Nov 19, 2026
@andrewbranch

Copy link
Copy Markdown
Member Author

They're not mutually exclusive, and they're not new; they're inherited from Strada's CommentRange and SyntheticCommentRange types. This is the newline's position relative to the comment, and there can be both, on a synthetic comment.

@andrewbranch
Andrew Branch (andrewbranch) added this pull request to the merge queue Nov 19, 2026
Merged via the queue into microsoft:main with commit 5440a4c Nov 19, 2026
34 checks passed
@andrewbranch
Andrew Branch (andrewbranch) deleted the emit-flags-continuation branch November 19, 2026 20:12
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[api] Add back missing: setEmitFlags and addSynthetic*Comment

4 participants