From 817b8e7d5f5234e4de4c3189a9568bb99acf4008 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 9 Nov 2018 14:55:33 -0800 Subject: [PATCH] Fix duplicated amd module comments in declaration bundle --- src/compiler/emitter.ts | 47 ++++++++++--------- ...undleNoDuplicateDeclarationEmitComments.js | 45 ++++++++++++++++++ ...NoDuplicateDeclarationEmitComments.symbols | 10 ++++ ...leNoDuplicateDeclarationEmitComments.types | 10 ++++ ...undleNoDuplicateDeclarationEmitComments.ts | 9 ++++ 5 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.js create mode 100644 tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.symbols create mode 100644 tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.types create mode 100644 tests/cases/compiler/amdModuleBundleNoDuplicateDeclarationEmitComments.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 2443f72d59116..7a8f6f300a4bc 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -338,7 +338,7 @@ namespace ts { emitLeadingCommentsOfPosition, } = comments; - let currentSourceFile!: SourceFile; + let currentSourceFile: SourceFile | undefined; let nodeIdToGeneratedName: string[]; // Map of generated names for specific nodes. let autoGeneratedIdToGeneratedName: string[]; // Map of generated names for temp and loop variables. let generatedNames: Map; // Set of names generated by the NameGenerator. @@ -505,11 +505,13 @@ namespace ts { pipelinePhase(hint, node); } - function setSourceFile(sourceFile: SourceFile) { + function setSourceFile(sourceFile: SourceFile | undefined) { currentSourceFile = sourceFile; - comments.setSourceFile(sourceFile); - if (onSetSourceFile) { - onSetSourceFile(sourceFile); + if (sourceFile) { + comments.setSourceFile(sourceFile); + if (onSetSourceFile) { + onSetSourceFile(sourceFile); + } } } @@ -1023,7 +1025,7 @@ namespace ts { const numNodes = bundle ? bundle.sourceFiles.length : 1; for (let i = 0; i < numNodes; i++) { const currentNode = bundle ? bundle.sourceFiles[i] : node; - const sourceFile = isSourceFile(currentNode) ? currentNode : currentSourceFile; + const sourceFile = isSourceFile(currentNode) ? currentNode : currentSourceFile!; const shouldSkip = printerOptions.noEmitHelpers || getExternalHelpersModuleName(sourceFile) !== undefined; const shouldBundle = isSourceFile(currentNode) && !isOwnFileEmit; const helpers = getEmitHelpers(currentNode); @@ -1531,7 +1533,7 @@ namespace ts { } const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None; - const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 && !isJsonSourceFile(currentSourceFile) ? ListFormat.AllowTrailingComma : ListFormat.None; + const allowTrailingComma = currentSourceFile!.languageVersion >= ScriptTarget.ES5 && !isJsonSourceFile(currentSourceFile!) ? ListFormat.AllowTrailingComma : ListFormat.None; emitList(node, node.properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine); if (indentedFlag) { @@ -1544,7 +1546,7 @@ namespace ts { let indentAfterDot = false; if (!(getEmitFlags(node) & EmitFlags.NoIndentation)) { const dotRangeStart = node.expression.end; - const dotRangeEnd = skipTrivia(currentSourceFile.text, node.expression.end) + 1; + const dotRangeEnd = skipTrivia(currentSourceFile!.text, node.expression.end) + 1; const dotToken = createToken(SyntaxKind.DotToken); dotToken.pos = dotRangeStart; dotToken.end = dotRangeEnd; @@ -1822,7 +1824,7 @@ namespace ts { emitExpression(node.expression); // Emit semicolon in non json files // or if json file that created synthesized expression(eg.define expression statement when --out and amd code generation) - if (!isJsonSourceFile(currentSourceFile) || nodeIsSynthesized(node.expression)) { + if (!isJsonSourceFile(currentSourceFile!) || nodeIsSynthesized(node.expression)) { writeSemicolon(); } } @@ -1942,10 +1944,10 @@ namespace ts { const isSimilarNode = node && node.kind === contextNode.kind; const startPos = pos; if (isSimilarNode) { - pos = skipTrivia(currentSourceFile.text, pos); + pos = skipTrivia(currentSourceFile!.text, pos); } if (emitLeadingCommentsOfPosition && isSimilarNode && contextNode.pos !== startPos) { - const needsIndent = indentLeading && !positionsAreOnSameLine(startPos, pos, currentSourceFile); + const needsIndent = indentLeading && !positionsAreOnSameLine(startPos, pos, currentSourceFile!); if (needsIndent) { increaseIndent(); } @@ -2116,7 +2118,7 @@ namespace ts { return false; } - if (!nodeIsSynthesized(body) && !rangeIsOnSingleLine(body, currentSourceFile)) { + if (!nodeIsSynthesized(body) && !rangeIsOnSingleLine(body, currentSourceFile!)) { return false; } @@ -2530,7 +2532,7 @@ namespace ts { // treat synthesized nodes as located on the same line for emit purposes nodeIsSynthesized(parentNode) || nodeIsSynthesized(statements[0]) || - rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile) + rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile!) ); let format = ListFormat.CaseOrDefaultClauseStatements; @@ -2884,6 +2886,7 @@ namespace ts { setSourceFile(sourceFile); emitPrologueDirectives(sourceFile.statements, /*startWithNewLine*/ true, seenPrologueDirectives); } + setSourceFile(undefined); } } @@ -3386,13 +3389,13 @@ namespace ts { const firstChild = children[0]; if (firstChild === undefined) { - return !rangeIsOnSingleLine(parentNode, currentSourceFile); + return !rangeIsOnSingleLine(parentNode, currentSourceFile!); } else if (positionIsSynthesized(parentNode.pos) || nodeIsSynthesized(firstChild)) { return synthesizedNodeStartsOnNewLine(firstChild, format); } else { - return !rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); + return !rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile!); } } else { @@ -3412,7 +3415,7 @@ namespace ts { return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); } else { - return !rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); + return !rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile!); } } else { @@ -3431,13 +3434,13 @@ namespace ts { const lastChild = lastOrUndefined(children); if (lastChild === undefined) { - return !rangeIsOnSingleLine(parentNode, currentSourceFile); + return !rangeIsOnSingleLine(parentNode, currentSourceFile!); } else if (positionIsSynthesized(parentNode.pos) || nodeIsSynthesized(lastChild)) { return synthesizedNodeStartsOnNewLine(lastChild, format); } else { - return !rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); + return !rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile!); } } else { @@ -3471,12 +3474,12 @@ namespace ts { return !nodeIsSynthesized(parent) && !nodeIsSynthesized(node1) && !nodeIsSynthesized(node2) - && !rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); + && !rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile!); } function isEmptyBlock(block: BlockLike) { return block.statements.length === 0 - && rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); + && rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile!); } function skipSynthesizedParentheses(node: Node) { @@ -3501,7 +3504,7 @@ namespace ts { return node.text; } - return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); + return getSourceTextOfNodeFromSourceFile(currentSourceFile!, node, includeTrivia); } function getLiteralTextOfNode(node: LiteralLikeNode, neverAsciiEscape: boolean | undefined): string { @@ -3517,7 +3520,7 @@ namespace ts { } } - return getLiteralText(node, currentSourceFile, neverAsciiEscape); + return getLiteralText(node, currentSourceFile!, neverAsciiEscape); } /** diff --git a/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.js b/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.js new file mode 100644 index 0000000000000..f2e5bc636b498 --- /dev/null +++ b/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.js @@ -0,0 +1,45 @@ +//// [tests/cases/compiler/amdModuleBundleNoDuplicateDeclarationEmitComments.ts] //// + +//// [file1.ts] +/// +export class Foo {} +//// [file2.ts] +/// +export class Bar {} + +//// [out.js] +define("mynamespace::SomeModuleA", ["require", "exports"], function (require, exports) { + "use strict"; + exports.__esModule = true; + /// + var Foo = /** @class */ (function () { + function Foo() { + } + return Foo; + }()); + exports.Foo = Foo; +}); +define("mynamespace::SomeModuleB", ["require", "exports"], function (require, exports) { + "use strict"; + exports.__esModule = true; + /// + var Bar = /** @class */ (function () { + function Bar() { + } + return Bar; + }()); + exports.Bar = Bar; +}); + + +//// [out.d.ts] +/// +declare module "mynamespace::SomeModuleA" { + export class Foo { + } +} +/// +declare module "mynamespace::SomeModuleB" { + export class Bar { + } +} diff --git a/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.symbols b/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.symbols new file mode 100644 index 0000000000000..69eaee002f24a --- /dev/null +++ b/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.symbols @@ -0,0 +1,10 @@ +=== tests/cases/compiler/file1.ts === +/// +export class Foo {} +>Foo : Symbol(Foo, Decl(file1.ts, 0, 0)) + +=== tests/cases/compiler/file2.ts === +/// +export class Bar {} +>Bar : Symbol(Bar, Decl(file2.ts, 0, 0)) + diff --git a/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.types b/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.types new file mode 100644 index 0000000000000..d90ff85e5fbed --- /dev/null +++ b/tests/baselines/reference/amdModuleBundleNoDuplicateDeclarationEmitComments.types @@ -0,0 +1,10 @@ +=== tests/cases/compiler/file1.ts === +/// +export class Foo {} +>Foo : Foo + +=== tests/cases/compiler/file2.ts === +/// +export class Bar {} +>Bar : Bar + diff --git a/tests/cases/compiler/amdModuleBundleNoDuplicateDeclarationEmitComments.ts b/tests/cases/compiler/amdModuleBundleNoDuplicateDeclarationEmitComments.ts new file mode 100644 index 0000000000000..ac185a891177c --- /dev/null +++ b/tests/cases/compiler/amdModuleBundleNoDuplicateDeclarationEmitComments.ts @@ -0,0 +1,9 @@ +//@module: amd +// @declaration: true +// @outFile: ./out.js +// @filename: file1.ts +/// +export class Foo {} +// @filename: file2.ts +/// +export class Bar {} \ No newline at end of file