Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ namespace ts {
const moduleKind = getEmitModuleKind(printerOptions);
const bundledHelpers = createMap<boolean>();

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<true>; // Set of names generated by the NameGenerator.
Expand Down Expand Up @@ -604,11 +604,13 @@ namespace ts {
pipelinePhase(hint, node);
}

function setSourceFile(sourceFile: SourceFile) {
function setSourceFile(sourceFile: SourceFile | undefined) {
currentSourceFile = sourceFile;
currentLineMap = undefined;
detachedCommentsInfo = undefined;
setSourceMapSource(sourceFile);
if (sourceFile) {
setSourceMapSource(sourceFile);
Comment thread
sheetalkamat marked this conversation as resolved.
}
}

function setWriter(_writer: EmitTextWriter | undefined, _sourceMapGenerator: SourceMapGenerator | undefined) {
Expand All @@ -635,7 +637,7 @@ namespace ts {
}

function getCurrentLineMap() {
return currentLineMap || (currentLineMap = getLineStarts(currentSourceFile));
return currentLineMap || (currentLineMap = getLineStarts(currentSourceFile!));
}

function emit(node: Node | undefined) {
Expand Down Expand Up @@ -1130,7 +1132,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);
Expand Down Expand Up @@ -1638,7 +1640,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) {
Expand All @@ -1651,7 +1653,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;
Expand Down Expand Up @@ -1937,7 +1939,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)) {
writeTrailingSemicolon();
}
}
Expand Down Expand Up @@ -2057,10 +2059,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();
}
Expand Down Expand Up @@ -2231,7 +2233,7 @@ namespace ts {
return false;
}

if (!nodeIsSynthesized(body) && !rangeIsOnSingleLine(body, currentSourceFile)) {
if (!nodeIsSynthesized(body) && !rangeIsOnSingleLine(body, currentSourceFile!)) {
return false;
}

Expand Down Expand Up @@ -2644,7 +2646,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;
Expand Down Expand Up @@ -2998,6 +3000,7 @@ namespace ts {
setSourceFile(sourceFile);
emitPrologueDirectives(sourceFile.statements, /*startWithNewLine*/ true, seenPrologueDirectives);
}
setSourceFile(undefined);
}
}

Expand Down Expand Up @@ -3488,13 +3491,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 {
Expand All @@ -3514,7 +3517,7 @@ namespace ts {
return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format);
}
else {
return !rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile);
return !rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile!);
}
}
else {
Expand All @@ -3533,13 +3536,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 {
Expand Down Expand Up @@ -3573,12 +3576,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) {
Expand All @@ -3603,7 +3606,7 @@ namespace ts {
return node.text;
}

return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia);
return getSourceTextOfNodeFromSourceFile(currentSourceFile!, node, includeTrivia);
}

function getLiteralTextOfNode(node: LiteralLikeNode, neverAsciiEscape: boolean | undefined): string {
Expand All @@ -3619,7 +3622,7 @@ namespace ts {
}
}

return getLiteralText(node, currentSourceFile, neverAsciiEscape);
return getLiteralText(node, currentSourceFile!, neverAsciiEscape);
}

/**
Expand Down Expand Up @@ -4181,15 +4184,15 @@ namespace ts {
}

function emitLeadingComment(commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) {
if (!shouldWriteComment(currentSourceFile.text, commentPos)) return;
if (!shouldWriteComment(currentSourceFile!.text, commentPos)) return;
if (!hasWrittenComment) {
emitNewLineBeforeLeadingCommentOfPosition(getCurrentLineMap(), writer, rangePos, commentPos);
hasWrittenComment = true;
}

// Leading comments are emitted at /*leading comment1 */space/*leading comment*/space
emitPos(commentPos);
writeCommentRange(currentSourceFile.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
writeCommentRange(currentSourceFile!.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
emitPos(commentEnd);

if (hasTrailingNewLine) {
Expand All @@ -4213,14 +4216,14 @@ namespace ts {
}

function emitTrailingComment(commentPos: number, commentEnd: number, _kind: SyntaxKind, hasTrailingNewLine: boolean) {
if (!shouldWriteComment(currentSourceFile.text, commentPos)) return;
if (!shouldWriteComment(currentSourceFile!.text, commentPos)) return;
// trailing comments are emitted at space/*trailing comment1 */space/*trailing comment2*/
if (!writer.isAtStartOfLine()) {
writer.writeSpace(" ");
}

emitPos(commentPos);
writeCommentRange(currentSourceFile.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
writeCommentRange(currentSourceFile!.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
emitPos(commentEnd);

if (hasTrailingNewLine) {
Expand All @@ -4241,7 +4244,7 @@ namespace ts {
// trailing comments of a position are emitted at /*trailing comment1 */space/*trailing comment*/space

emitPos(commentPos);
writeCommentRange(currentSourceFile.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
writeCommentRange(currentSourceFile!.text, getCurrentLineMap(), writer, commentPos, commentEnd, newLine);
emitPos(commentEnd);

if (hasTrailingNewLine) {
Expand Down Expand Up @@ -4285,11 +4288,11 @@ namespace ts {
detachedCommentsInfo = undefined;
}

forEachLeadingCommentRange(currentSourceFile.text, pos, cb, /*state*/ pos);
forEachLeadingCommentRange(currentSourceFile!.text, pos, cb, /*state*/ pos);
}

function emitDetachedCommentsAndUpdateCommentsInfo(range: TextRange) {
const currentDetachedCommentInfo = emitDetachedComments(currentSourceFile.text, getCurrentLineMap(), writer, emitComment, range, newLine, commentsDisabled);
const currentDetachedCommentInfo = emitDetachedComments(currentSourceFile!.text, getCurrentLineMap(), writer, emitComment, range, newLine, commentsDisabled);
if (currentDetachedCommentInfo) {
if (detachedCommentsInfo) {
detachedCommentsInfo.push(currentDetachedCommentInfo);
Expand All @@ -4301,7 +4304,7 @@ namespace ts {
}

function emitComment(text: string, lineMap: number[], writer: EmitTextWriter, commentPos: number, commentEnd: number, newLine: string) {
if (!shouldWriteComment(currentSourceFile.text, commentPos)) return;
if (!shouldWriteComment(currentSourceFile!.text, commentPos)) return;
emitPos(commentPos);
writeCommentRange(text, lineMap, writer, commentPos, commentEnd, newLine);
emitPos(commentEnd);
Expand All @@ -4313,7 +4316,7 @@ namespace ts {
* @return true if the comment is a triple-slash comment else false
*/
function isTripleSlashComment(commentPos: number, commentEnd: number) {
return isRecognizedTripleSlashComment(currentSourceFile.text, commentPos, commentEnd);
return isRecognizedTripleSlashComment(currentSourceFile!.text, commentPos, commentEnd);
}

// Source Maps
Expand Down Expand Up @@ -4377,7 +4380,7 @@ namespace ts {
return;
}

const { line: sourceLine, character: sourceCharacter } = getLineAndCharacterOfPosition(currentSourceFile, pos);
const { line: sourceLine, character: sourceCharacter } = getLineAndCharacterOfPosition(currentSourceFile!, pos);
sourceMapGenerator!.addMapping(
writer.getLine(),
writer.getColumn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/compiler/amdModuleBundleNoDuplicateDeclarationEmitComments.ts] ////

//// [file1.ts]
/// <amd-module name="mynamespace::SomeModuleA" />
export class Foo {}
//// [file2.ts]
/// <amd-module name="mynamespace::SomeModuleB" />
export class Bar {}

//// [out.js]
define("mynamespace::SomeModuleA", ["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
/// <amd-module name="mynamespace::SomeModuleA" />
var Foo = /** @class */ (function () {
function Foo() {
}
return Foo;
}());
exports.Foo = Foo;
});
define("mynamespace::SomeModuleB", ["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
/// <amd-module name="mynamespace::SomeModuleB" />
var Bar = /** @class */ (function () {
function Bar() {
}
return Bar;
}());
exports.Bar = Bar;
});


//// [out.d.ts]
/// <amd-module name="mynamespace::SomeModuleA" />
declare module "mynamespace::SomeModuleA" {
export class Foo {
}
}
/// <amd-module name="mynamespace::SomeModuleB" />
declare module "mynamespace::SomeModuleB" {
export class Bar {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/file1.ts ===
/// <amd-module name="mynamespace::SomeModuleA" />
export class Foo {}
>Foo : Symbol(Foo, Decl(file1.ts, 0, 0))

=== tests/cases/compiler/file2.ts ===
/// <amd-module name="mynamespace::SomeModuleB" />
export class Bar {}
>Bar : Symbol(Bar, Decl(file2.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/file1.ts ===
/// <amd-module name="mynamespace::SomeModuleA" />
export class Foo {}
>Foo : Foo

=== tests/cases/compiler/file2.ts ===
/// <amd-module name="mynamespace::SomeModuleB" />
export class Bar {}
>Bar : Bar

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@module: amd
// @declaration: true
// @outFile: ./out.js
// @filename: file1.ts
/// <amd-module name="mynamespace::SomeModuleA" />
export class Foo {}
// @filename: file2.ts
/// <amd-module name="mynamespace::SomeModuleB" />
export class Bar {}