From d4eaf3ef537f5c125abd319892aa6da18ab0433f Mon Sep 17 00:00:00 2001 From: z0rimo <46391780+z0rimo@users.noreply.github.com> Date: Sun, 22 Nov 2026 23:20:32 +0900 Subject: [PATCH] Fix synthesized comment emission with comment flags --- packages/typescript/test/async/api.test.ts | 53 ++++++++++++++++++++++ packages/typescript/test/sync/api.test.ts | 53 ++++++++++++++++++++++ tsc/internal/printer/printer.go | 9 +--- 3 files changed, 107 insertions(+), 8 deletions(-) diff --git a/packages/typescript/test/async/api.test.ts b/packages/typescript/test/async/api.test.ts index 582672974ff4f..d3fd69de5b456 100644 --- a/packages/typescript/test/async/api.test.ts +++ b/packages/typescript/test/async/api.test.ts @@ -9563,6 +9563,59 @@ export const obj = { m: 1, s: "hi", b: true }; ); }); + test("printNode with emitContext comment emit flags only suppress source comments", async () => { + await using api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": `/* source leading */ leading;\ntrailing; /* source trailing */\n`, + }); + const snapshot = await api.createSnapshot({ openProjects: ["/tsconfig.json"] }); + const sourceFile = await snapshot.getConfiguredProject("/tsconfig.json")!.program.getSourceFile("/src/main.ts"); + assert.ok(sourceFile); + const [leading, trailing] = sourceFile.statements; + + // Source comments are only emitted when printing the whole source file, so print that + // and attach the emit context state to the statement of interest. + const print = async (statement: typeof leading, flags: EmitFlags, side: "leading" | "trailing") => { + const emitContext = api.printer.createEmitContext(); + emitContext.setEmitFlags(statement, flags); + if (side === "leading") { + emitContext.addSyntheticLeadingComment(statement, SyntaxKind.MultiLineCommentTrivia, " synthesized ", false, false); + } + else { + emitContext.addSyntheticTrailingComment(statement, SyntaxKind.MultiLineCommentTrivia, " synthesized ", false, false); + } + return (await api.printer.printNode(sourceFile, { emitContext })).trimEnd(); + }; + + assert.strictEqual(await print(leading, EmitFlags.None, "leading"), "/* source leading */ /* synthesized */ leading;\ntrailing; /* source trailing */"); + assert.strictEqual(await print(leading, EmitFlags.NoLeadingComments, "leading"), "/* synthesized */ leading;\ntrailing; /* source trailing */"); + assert.strictEqual(await print(leading, EmitFlags.NoComments, "leading"), "/* synthesized */ leading;\ntrailing; /* source trailing */"); + + assert.strictEqual(await print(trailing, EmitFlags.None, "trailing"), "/* source leading */ leading;\ntrailing; /* synthesized */ /* source trailing */"); + assert.strictEqual(await print(trailing, EmitFlags.NoTrailingComments, "trailing"), "/* source leading */ leading;\ntrailing; /* synthesized */"); + assert.strictEqual(await print(trailing, EmitFlags.NoComments, "trailing"), "/* source leading */ leading;\ntrailing; /* synthesized */"); + }); + + test("printNode parenthesizes an operand whose synthesized leading comment introduces a newline even with NoLeadingComments", async () => { + await using api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": `function f() {\n /* source */ return a.b;\n}\n`, + }); + const snapshot = await api.createSnapshot({ openProjects: ["/tsconfig.json"] }); + const sourceFile = await snapshot.getConfiguredProject("/tsconfig.json")!.program.getSourceFile("/src/main.ts"); + assert.ok(sourceFile); + // `return` cannot be followed by a line break, so an operand whose leading comment + // starts a new line has to be parenthesized. + const declaration = sourceFile.statements[0] as import("@typescript/typescript/ast").FunctionDeclaration; + const returnStatement = declaration.body!.statements[0] as import("@typescript/typescript/ast").ReturnStatement; + const operand = (returnStatement.expression as import("@typescript/typescript/ast").PropertyAccessExpression).expression; + + const emitContext = api.printer.createEmitContext(); + emitContext.setEmitFlags(operand, EmitFlags.NoLeadingComments); + emitContext.addSyntheticLeadingComment(operand, SyntaxKind.SingleLineCommentTrivia, " synthesized", true, false); + assert.strictEqual((await api.printer.printNode(sourceFile, { emitContext })).trimEnd(), "function f() {\n /* source */ return (\n // synthesized\n a).b;\n}"); + }); + test("printNode honors newline flags on synthetic trailing comments", async () => { await using api = spawnAPI(emitterFiles); diff --git a/packages/typescript/test/sync/api.test.ts b/packages/typescript/test/sync/api.test.ts index caeabc14aa17e..75614071aa95e 100644 --- a/packages/typescript/test/sync/api.test.ts +++ b/packages/typescript/test/sync/api.test.ts @@ -9321,6 +9321,59 @@ export const obj = { m: 1, s: "hi", b: true }; ); }); + test("printNode with emitContext comment emit flags only suppress source comments", () => { + using api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": `/* source leading */ leading;\ntrailing; /* source trailing */\n`, + }); + const snapshot = api.createSnapshot({ openProjects: ["/tsconfig.json"] }); + const sourceFile = snapshot.getConfiguredProject("/tsconfig.json")!.program.getSourceFile("/src/main.ts"); + assert.ok(sourceFile); + const [leading, trailing] = sourceFile.statements; + + // Source comments are only emitted when printing the whole source file, so print that + // and attach the emit context state to the statement of interest. + const print = (statement: typeof leading, flags: EmitFlags, side: "leading" | "trailing") => { + const emitContext = api.printer.createEmitContext(); + emitContext.setEmitFlags(statement, flags); + if (side === "leading") { + emitContext.addSyntheticLeadingComment(statement, SyntaxKind.MultiLineCommentTrivia, " synthesized ", false, false); + } + else { + emitContext.addSyntheticTrailingComment(statement, SyntaxKind.MultiLineCommentTrivia, " synthesized ", false, false); + } + return (api.printer.printNode(sourceFile, { emitContext })).trimEnd(); + }; + + assert.strictEqual(print(leading, EmitFlags.None, "leading"), "/* source leading */ /* synthesized */ leading;\ntrailing; /* source trailing */"); + assert.strictEqual(print(leading, EmitFlags.NoLeadingComments, "leading"), "/* synthesized */ leading;\ntrailing; /* source trailing */"); + assert.strictEqual(print(leading, EmitFlags.NoComments, "leading"), "/* synthesized */ leading;\ntrailing; /* source trailing */"); + + assert.strictEqual(print(trailing, EmitFlags.None, "trailing"), "/* source leading */ leading;\ntrailing; /* synthesized */ /* source trailing */"); + assert.strictEqual(print(trailing, EmitFlags.NoTrailingComments, "trailing"), "/* source leading */ leading;\ntrailing; /* synthesized */"); + assert.strictEqual(print(trailing, EmitFlags.NoComments, "trailing"), "/* source leading */ leading;\ntrailing; /* synthesized */"); + }); + + test("printNode parenthesizes an operand whose synthesized leading comment introduces a newline even with NoLeadingComments", () => { + using api = spawnAPI({ + "/tsconfig.json": JSON.stringify({ compilerOptions: { strict: true } }), + "/src/main.ts": `function f() {\n /* source */ return a.b;\n}\n`, + }); + const snapshot = api.createSnapshot({ openProjects: ["/tsconfig.json"] }); + const sourceFile = snapshot.getConfiguredProject("/tsconfig.json")!.program.getSourceFile("/src/main.ts"); + assert.ok(sourceFile); + // `return` cannot be followed by a line break, so an operand whose leading comment + // starts a new line has to be parenthesized. + const declaration = sourceFile.statements[0] as import("@typescript/typescript/ast").FunctionDeclaration; + const returnStatement = declaration.body!.statements[0] as import("@typescript/typescript/ast").ReturnStatement; + const operand = (returnStatement.expression as import("@typescript/typescript/ast").PropertyAccessExpression).expression; + + const emitContext = api.printer.createEmitContext(); + emitContext.setEmitFlags(operand, EmitFlags.NoLeadingComments); + emitContext.addSyntheticLeadingComment(operand, SyntaxKind.SingleLineCommentTrivia, " synthesized", true, false); + assert.strictEqual((api.printer.printNode(sourceFile, { emitContext })).trimEnd(), "function f() {\n /* source */ return (\n // synthesized\n a).b;\n}"); + }); + test("printNode honors newline flags on synthetic trailing comments", () => { using api = spawnAPI(emitterFiles); diff --git a/tsc/internal/printer/printer.go b/tsc/internal/printer/printer.go index d5674adb47b78..e1f4135e0d7ec 100644 --- a/tsc/internal/printer/printer.go +++ b/tsc/internal/printer/printer.go @@ -3065,8 +3065,7 @@ func (p *Printer) syntheticLeadingCommentWillEmitNewLine(comment SynthesizedComm } func (p *Printer) hasSyntheticLeadingCommentIntroducingNewLine(node *ast.Expression) bool { - return p.emitContext.EmitFlags(node)&EFNoLeadingComments == 0 && - slices.ContainsFunc(p.emitContext.GetSyntheticLeadingComments(node), p.syntheticLeadingCommentWillEmitNewLine) + return slices.ContainsFunc(p.emitContext.GetSyntheticLeadingComments(node), p.syntheticLeadingCommentWillEmitNewLine) } func (p *Printer) willEmitLeadingNewLine(node *ast.Expression) bool { @@ -5479,9 +5478,6 @@ func (p *Printer) emitTrailingCommentsOfNode(node *ast.Node, emitFlags EmitFlags } 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) @@ -5501,9 +5497,6 @@ func (p *Printer) emitLeadingSynthesizedComment(comment SynthesizedComment) { } 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)