Skip to content

Commit 8ed645a

Browse files
authored
Don't emit duplicate triple-slash directives when using API to print a .d.ts (#40968)
2 parents 6092ecf + 14714bb commit 8ed645a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/compiler/emitter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5136,7 +5136,12 @@ namespace ts {
51365136
hasWrittenComment = false;
51375137

51385138
if (isEmittedNode) {
5139-
forEachLeadingCommentToEmit(pos, emitLeadingComment);
5139+
if (pos === 0 && currentSourceFile?.isDeclarationFile) {
5140+
forEachLeadingCommentToEmit(pos, emitNonTripleSlashLeadingComment);
5141+
}
5142+
else {
5143+
forEachLeadingCommentToEmit(pos, emitLeadingComment);
5144+
}
51405145
}
51415146
else if (pos === 0) {
51425147
// If the node will not be emitted in JS, remove all the comments(normal, pinned and ///) associated with the node,
@@ -5157,6 +5162,12 @@ namespace ts {
51575162
}
51585163
}
51595164

5165+
function emitNonTripleSlashLeadingComment(commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) {
5166+
if (!isTripleSlashComment(commentPos, commentEnd)) {
5167+
emitLeadingComment(commentPos, commentEnd, kind, hasTrailingNewLine, rangePos);
5168+
}
5169+
}
5170+
51605171
function shouldWriteComment(text: string, pos: number) {
51615172
if (printerOptions.onlyPrintJsDocStyle) {
51625173
return (isJSDocLikeText(text, pos) || isPinnedComment(text, pos));

src/testRunner/unittests/printer.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ namespace ts {
9393
});
9494
});
9595

96+
describe("No duplicate ref directives when emiting .d.ts->.d.ts", () => {
97+
it("without statements", () => {
98+
const host = new fakes.CompilerHost(new vfs.FileSystem(true, {
99+
files: {
100+
"/test.d.ts": `/// <reference types="node" />\n/// <reference path="./src/test.d.ts />\n`
101+
}
102+
}));
103+
const program = createProgram(["/test.d.ts"], { }, host);
104+
const file = program.getSourceFile("/test.d.ts")!;
105+
const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed });
106+
const output = printer.printFile(file);
107+
assert.equal(output.split(/\r?\n/g).length, 3);
108+
});
109+
it("with statements", () => {
110+
const host = new fakes.CompilerHost(new vfs.FileSystem(true, {
111+
files: {
112+
"/test.d.ts": `/// <reference types="node" />\n/// <reference path="./src/test.d.ts />\nvar a: number;\n`
113+
}
114+
}));
115+
const program = createProgram(["/test.d.ts"], { }, host);
116+
const file = program.getSourceFile("/test.d.ts")!;
117+
const printer = createPrinter({ newLine: NewLineKind.CarriageReturnLineFeed });
118+
const output = printer.printFile(file);
119+
assert.equal(output.split(/\r?\n/g).length, 4);
120+
});
121+
});
122+
96123
describe("printBundle", () => {
97124
const printsCorrectly = makePrintsCorrectly("printsBundleCorrectly");
98125
let bundle: Bundle;

0 commit comments

Comments
 (0)