From 14714bbace7a810192fb4a1e983ac012e42adc01 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 6 Oct 2020 15:02:41 -0700 Subject: [PATCH] Don't emit duplicate triple-slash directives when using API to print a .d.ts --- src/compiler/emitter.ts | 13 ++++++++++++- src/testRunner/unittests/printer.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 237d3fee9cd..edd966270b7 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -5135,7 +5135,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, @@ -5156,6 +5161,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;