Repository navigation
Add support for setting emit flags and synthetic comments - #65447
Andrew Branch (andrewbranch) merged 9 commits into
Conversation
…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>
There was a problem hiding this comment.
🟢 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
EmitContextfor 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
EmitFlagsexports.
| 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
Wesley Wigham (weswigham)
left a comment
There was a problem hiding this comment.
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 || |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Wesley Wigham (weswigham)
left a comment
There was a problem hiding this comment.
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?
|
They're not mutually exclusive, and they're not new; they're inherited from Strada's |
Continuation of #64500
Fixes #64429