diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index dbbb4a1b65f..c136359ccea 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -5136,7 +5136,12 @@ namespace ts { hasWrittenComment = false; if (isEmittedNode) { - forEachLeadingCommentToEmit(pos, emitLeadingComment); + if (pos === 0 && currentSourceFile?.isDeclarationFile) { + forEachLeadingCommentToEmit(pos, emitNonTripleSlashLeadingComment); + } + else { + forEachLeadingCommentToEmit(pos, emitLeadingComment); + } } else if (pos === 0) { // 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 { } } + function emitNonTripleSlashLeadingComment(commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) { + if (!isTripleSlashComment(commentPos, commentEnd)) { + emitLeadingComment(commentPos, commentEnd, kind, hasTrailingNewLine, rangePos); + } + } + function shouldWriteComment(text: string, pos: number) { if (printerOptions.onlyPrintJsDocStyle) { return (isJSDocLikeText(text, pos) || isPinnedComment(text, pos)); diff --git a/src/testRunner/unittests/printer.ts b/src/testRunner/unittests/printer.ts index 59ffa8f0ed2..50c776c0f9f 100644 --- a/src/testRunner/unittests/printer.ts +++ b/src/testRunner/unittests/printer.ts @@ -93,6 +93,33 @@ namespace ts { }); }); + describe("No duplicate ref directives when emiting .d.ts->.d.ts", () => { + it("without statements", () => { + const host = new fakes.CompilerHost(new vfs.FileSystem(true, { + files: { + "/test.d.ts": `/// \n/// { + const host = new fakes.CompilerHost(new vfs.FileSystem(true, { + files: { + "/test.d.ts": `/// \n/// { const printsCorrectly = makePrintsCorrectly("printsBundleCorrectly"); let bundle: Bundle;