Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

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

未關閉 適合新手
#64,453 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
84/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
go, typescript
領域
compilers

研究方向

從 internal/printer/printer.go 中的 emitLeadingSyntheticCommentsOfNode 和 emitTrailingSyntheticCommentsOfNode 開始,然後執行 issue 中的 TestSyntheticLeadingCommentWithNoLeadingComments 範例。將輸出與 Strada 的 printer 行為進行比較,並為使用 EFNoLeadingComments、EFNoTrailingComments 和 EFNoComments 時的前置與尾隨合成註解新增針對性的覆蓋。完成的標準是可以抑制原始碼註解,而不會抑制合成的替代內容。

由索引模型根據 Issue 內容生成。

描述

🔎 Search Terms

NoLeadingComments, EFNoLeadingComments, synthesized comments, setSyntheticLeadingComments, emitLeadingSyntheticCommentsOfNode, printer, comment emit

🕗 Version & Regression Information
  • This changed between TypeScript 6.0.3 and tsgo built from main at df1a31e6d5.
  • The tsgo behavior has been there since synthesized comment emit was first ported, in microsoft/typescript-go#1599 (commit 809b2c09, "Const enum inlining and synthetic comment emit support").
⏯ Playground Link

None. This is only reachable through the transformer/printer API, so the repros below use the API directly.

💻 Code

Strada, run with Node against [email protected]:

const ts = require('typescript');
const f = ts.factory;
const sf = ts.createSourceFile('a.ts', '', ts.ScriptTarget.Latest);
const printer = ts.createPrinter();
const comment = {kind: ts.SyntaxKind.MultiLineCommentTrivia, text: '* synthesized ', hasTrailingNewLine: true, pos: -1, end: -1};

for (const [label, flags] of [['no flags', 0], ['NoLeadingComments', ts.EmitFlags.NoLeadingComments], ['NoComments', ts.EmitFlags.NoComments]]) {
  const stmt = f.createExpressionStatement(f.createIdentifier('x'));
  ts.setSyntheticLeadingComments(stmt, [comment]);
  if (flags) ts.setEmitFlags(stmt, flags);
  console.log(`--- ${label}\n` + printer.printNode(ts.EmitHint.Unspecified, stmt, sf));
}

tsgo, the same thing as a test in internal/printer:

func TestSyntheticLeadingCommentWithNoLeadingComments(t *testing.T) {
  for _, c := range []struct {
    name  string
    flags EmitFlags
  }{{"no flags", 0}, {"EFNoLeadingComments", EFNoLeadingComments}, {"EFNoComments", EFNoComments}} {
    sf := parser.ParseSourceFile(ast.SourceFileParseOptions{FileName: "/a.ts", Path: "/a.ts"}, "", core.ScriptKindTS)
    ec := NewEmitContext()
    stmt := ec.Factory.NewExpressionStatement(ec.Factory.NewIdentifier("x"))
    ec.SetSyntheticLeadingComments(stmt, []SynthesizedComment{{Kind: ast.KindMultiLineCommentTrivia, Text: "* synthesized ", HasTrailingNewLine: true, Loc: core.UndefinedTextRange()}})
    ec.SetEmitFlags(stmt, c.flags)
    t.Logf("--- %s\n%s", c.name, NewPrinter(PrinterOptions{}, PrintHandlers{}, ec).Emit(stmt, sf))
  }
}
🙁 Actual behavior

tsgo drops the synthesized comment as soon as EFNoLeadingComments is set:

--- no flags
/** synthesized */
x;
--- EFNoLeadingComments
x;
--- EFNoComments
x;
🙂 Expected behavior

Same as TypeScript 6.0.3, which keeps the synthesized comment in all three cases:

--- no flags
/** synthesized */
x;
--- NoLeadingComments
/** synthesized */
x;
--- NoComments
/** synthesized */
x;
Additional information about the issue

Where the two differ. In Strada, emitLeadingCommentsOfNode in src/compiler/emitter.ts uses NoLeadingComments (and a negative pos) only to decide whether to emit the comments found in the source text for the node's range. The synthesized comments are emitted after that check, unconditionally:

const skipLeadingComments = pos < 0 || (emitFlags & EmitFlags.NoLeadingComments) !== 0 || node.kind === SyntaxKind.JsxText;
// ...
if (!skipLeadingComments) {
    emitLeadingComments(pos, /*isEmittedNode*/ node.kind !== SyntaxKind.NotEmittedStatement);
}
// ...
forEach(getSyntheticLeadingComments(node), emitLeadingSynthesizedComment);

In tsgo, the synthesized comments go through their own function, emitLeadingSyntheticCommentsOfNode in internal/printer/printer.go, which returns early on the flag:

func (p *Printer) emitLeadingSyntheticCommentsOfNode(node *ast.Node, emitFlags EmitFlags) {
  if emitFlags&EFNoLeadingComments != 0 {
    return
  }
  synth := p.emitContext.GetSyntheticLeadingComments(node)
  // ...

Trailing comments have the same shape: Strada's emitTrailingCommentsOfNode emits getSyntheticTrailingComments(node) unconditionally, and tsgo's emitTrailingSyntheticCommentsOfNode returns early on EFNoTrailingComments. I measured only the leading case; the trailing one is from reading the code.

These lines came in with microsoft/typescript-go#1599, the PR that first added synthesized comment emit. I couldn't find any discussion of the flag in that PR, so this looks like it was introduced in the original port rather than chosen deliberately.

Why it matters. A transformer that rewrites a comment needs to hide the node's original source comment and print its replacement. In Strada, that means setting NoLeadingComments and attaching the replacement with setSyntheticLeadingComments. tsickle, which rewrites TypeScript JSDoc into Closure Compiler annotations, relies on this. In tsgo, EFNoLeadingComments also suppresses the synthesized replacement, so the pattern breaks: the transformer can hide both comments or neither. For our Go port of tsickle, this means decorated class members get printed with both the original JSDoc and the rewritten JSDoc. Closure Compiler rejects that output, so builds that pass with TypeScript 6.0.3 fail with tsgo.

Possible fix. Emit synthesized comments regardless of EFNoLeadingComments / EFNoTrailingComments, as Strada does, so the flags only control comments taken from the source text:

--- a/internal/printer/printer.go
+++ b/internal/printer/printer.go
 func (p *Printer) emitLeadingSyntheticCommentsOfNode(node *ast.Node, emitFlags EmitFlags) {
- if emitFlags&EFNoLeadingComments != 0 {
-   return
- }
  synth := p.emitContext.GetSyntheticLeadingComments(node)
  for _, c := range synth {
    p.emitLeadingSynthesizedComment(c)
  }
 }
@@
 func (p *Printer) emitTrailingSyntheticCommentsOfNode(node *ast.Node, emitFlags EmitFlags) {
- if emitFlags&EFNoTrailingComments != 0 {
-   return
- }
  synth := p.emitContext.GetSyntheticTrailingComments(node)
  for _, c := range synth {
    p.emitTrailingSynthesizedComment(c)
  }
 }

This leaves emitFlags unused in both functions. I kept the signatures unchanged to keep the diff small. I haven't run tsgo's test suite with this change, so I don't know whether anything in tsgo itself now depends on the current behavior.

主要語言
Go
星號
111k
分支
14.4k
平均合併
2 天 2 小時
30 天內合併 PR
110

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。