Skip to content

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

Open
Titian Cernicova-Dragomir (dragomirtitian) wants to merge 5 commits into
microsoft:mainfrom
bloomberg:add-support-for-emit-nodes
Open

Titian Cernicova-Dragomir (dragomirtitian) wants to merge 5 commits into
microsoft:mainfrom
bloomberg:add-support-for-emit-nodes

Conversation

@dragomirtitian

@dragomirtitian Titian Cernicova-Dragomir (dragomirtitian) commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #64429

Copilot AI balanced review requested due to automatic review settings September 28, 2026 15:39
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 28, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 28, 2026
@typescript-automation

Copy link
Copy Markdown
Contributor

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

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.

Copilot review overview

🟡 Changes recommended

Public API compatibility gaps, omitted enum members, and a nullable protocol entry that can panic must be addressed.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Adds emit flags and synthetic-comment support to the TypeScript API printer.

Changes:

  • Introduces EmitContext and EmitFlags.
  • Transfers emit metadata alongside encoded AST nodes.
  • Adds synchronous and asynchronous API tests.
File Description
tsc/​internal/​printer/​printer.go Enables synthetic comments without a source file.
tsc/​internal/​api/​session.go Reconstructs emit metadata for printing.
tsc/​internal/​api/​proto.go Defines emit metadata protocol types.
tsc/​internal/​api/​enum_values_generated.go Adds generated emit flag values.
tsc/​internal/​api/​encoder/​decoder.go Adds per-node decode callbacks.
tools/​scripts/​tsc/​generate-enums.ts Configures EmitFlags generation.
packages/​typescript/​test/​sync/​api.test.ts Adds generated synchronous tests.
packages/​typescript/​test/​async/​api.test.ts Tests comments and emit flags.
packages/​typescript/​src/​enums/​emitFlags.ts Adds runtime enum output.
packages/​typescript/​src/​enums/​emitFlags.enum.ts Adds typed enum output.
packages/​typescript/​src/​api/​sync/​api.ts Adds generated synchronous APIs.
packages/​typescript/​src/​api/​proto.generated.ts Adds generated protocol declarations.
packages/​typescript/​src/​api/​node/​encoder.ts Reports encoded node indices.
packages/​typescript/​src/​api/​async/​api.ts Implements the public emit context API.
Files not reviewed (1)
  • tsc/internal/api/enum_values_generated.go: Generated file

Comment thread tsc/internal/api/session.go Resolved
Comment thread tools/scripts/tsc/generate-enums.ts Resolved
…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>

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.

🟡 Changes recommended

Synthetic comment handling can change program semantics or produce invalid syntax, and deferred prints retain mutable metadata.

3 open findings
3 resolved since last review
Files not reviewed (1)
  • tsc/internal/api/enum_values_generated.go: Generated file

🧠 Review effort: Balanced

Comment thread tsc/internal/api/session.go Resolved
Comment thread tsc/internal/printer/printer.go Resolved

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.

🟡 Changes recommended

Synthetic-comment handling can alter expression semantics, produce invalid syntax, and lose attached metadata.

3 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Detect line terminators in multiline comments

tsc/​internal/​printer/​printer.go:3061

A multiline comment can contain a line terminator even when both newline flags are false. A leading comment with text a\nb on the operand of return 1 bypasses this check and emits return /*a\nb*/ 1;. Automatic semicolon insertion makes that return undefined; the corresponding throw is invalid syntax. Include line terminators in comment.Text when deciding whether parentheses are needed.

Medium severity Honor HasLeadingNewLine for trailing comments

tsc/​internal/​printer/​printer.go:5519

The newly exposed hasLeadingNewLine option is ignored for trailing comments. For example, addSyntheticTrailingComment(node, MultiLineCommentTrivia, " note ", false, true) still places the comment on the node's line. Honor comment.HasLeadingNewLine before writing the trailing comment, as the leading-comment emitter already does.

3 resolved since last review
1 file not reviewed
  • tsc/internal/api/enum_values_generated.go: Generated file

Review effort: Balanced

Comment on lines +3112 to +3113
if node.Kind != ast.KindPartiallyEmittedExpression && p.willEmitLeadingSyntheticNewLine(node) {
return p.emitContext.Factory.NewParenthesizedExpression(node)

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.

Are these new bugs or inherited from Strada?

Comment on lines +5519 to 5520
if comment.HasTrailingNewLine || comment.Kind == ast.KindSingleLineCommentTrivia {
p.writer.WriteLine()
func (p *Printer) parenthesizeExpressionForNoAsi(node *ast.Expression) *ast.Expression {
if !p.commentsDisabled {
if node.Kind != ast.KindPartiallyEmittedExpression && p.willEmitLeadingSyntheticNewLine(node) {
return p.emitContext.Factory.NewParenthesizedExpression(node)
@el-j

Copy link
Copy Markdown

Heads-up from #65058 (JSDoc node emission in the printer), which also added synthetic-comment transport for printNode before I saw this PR. Having compared the two, I think this one should be the design that lands, and I plan to drop the overlapping part from #65058 once it merges.

Overlap: both send synthetic leading/trailing comments through the encoder and printNode, keyed by node index, and apply them to an EmitContext in session.go.

Where this PR is the better fit:

  • It also covers EmitFlags, which [api] Add back missing: setEmitFlags and addSynthetic*Comment #64429 asks for and ours doesn't.
  • It adds hasLeadingNewLine on comments and keeps a single-line comment from commenting out the following code.
  • It removes the currentSourceFile != nil gating in the printer generally (including the parenthesization checks) rather than through an opt-in option like ours (EmitSyntheticCommentsWithoutSourceFile).
  • It validates out-of-range emit-node indices.

What ours has that may be worth checking here: a JSDoc-shaped test (a /** ... */ multi-line comment with hasTrailingNewLine printed before a statement, then reparsed), plus trailing // comments after a statement with no source file. I can contribute those as tests here if useful.

Pointers: #65058 adds emitJSDocNode, which fixes #65057 (printing JSDoc nodes panicked), so the two are complementary rather than competing. No action needed from the author; this is only so maintainers know the two PRs touch the same code in encoder.ts, proto.go, session.go and decoder.go.

This branch has not been deployed

No deployments
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

Status: Not started

Development

Successfully merging this pull request may close these issues.

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

4 participants