Skip to content

Commit f747e5a

Browse files
Merge pull request #2276 from Microsoft/commentEmit
Provide specialized codepath for emitting when comments aren't necessary.
2 parents f061e00 + c21fe03 commit f747e5a

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

src/compiler/emitter.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,16 +1600,16 @@ module ts {
16001600
var emitLeadingCommentsOfPosition = compilerOptions.removeComments ? (pos: number) => { } : emitLeadingCommentsOfLocalPosition;
16011601

16021602
var detachedCommentsInfo: { nodePos: number; detachedCommentEndPos: number }[];
1603+
16031604
/** Emit detached comments of the node */
16041605
var emitDetachedComments = compilerOptions.removeComments ? (node: TextRange) => { } : emitDetachedCommentsAtPosition;
16051606

1606-
/** Emits /// or pinned which is comment starting with /*! comments */
1607-
var emitPinnedOrTripleSlashComments = compilerOptions.removeComments ? (node: Node) => { } : emitPinnedOrTripleSlashCommentsOfNode;
1608-
16091607
var writeComment = writeCommentRange;
16101608

16111609
/** Emit a node */
1612-
var emit = emitNode;
1610+
var emitNodeWithoutSourceMap = compilerOptions.removeComments ? emitNodeWithoutSourceMapWithoutComments : emitNodeWithoutSourceMapWithComments;
1611+
var emit = emitNodeWithoutSourceMap;
1612+
var emitWithoutComments = emitNodeWithoutSourceMapWithoutComments;
16131613

16141614
/** Called just before starting emit of a node */
16151615
var emitStart = function (node: Node) { };
@@ -2061,22 +2061,31 @@ module ts {
20612061
sourceMapDir = getDirectoryPath(normalizePath(jsFilePath));
20622062
}
20632063

2064-
function emitNodeWithMap(node: Node) {
2064+
function emitNodeWithSourceMap(node: Node) {
20652065
if (node) {
20662066
if (node.kind != SyntaxKind.SourceFile) {
20672067
recordEmitNodeStartSpan(node);
2068-
emitNode(node);
2068+
emitNodeWithoutSourceMap(node);
20692069
recordEmitNodeEndSpan(node);
20702070
}
20712071
else {
20722072
recordNewSourceFileStart(<SourceFile>node);
2073-
emitNode(node);
2073+
emitNodeWithoutSourceMap(node);
20742074
}
20752075
}
20762076
}
20772077

2078+
function emitNodeWithSourceMapWithoutComments(node: Node) {
2079+
if (node) {
2080+
recordEmitNodeStartSpan(node);
2081+
emitNodeWithoutSourceMapWithoutComments(node);
2082+
recordEmitNodeEndSpan(node);
2083+
}
2084+
}
2085+
20782086
writeEmittedFiles = writeJavaScriptAndSourceMapFile;
2079-
emit = emitNodeWithMap;
2087+
emit = emitNodeWithSourceMap;
2088+
emitWithoutComments = emitNodeWithSourceMapWithoutComments;
20802089
emitStart = recordEmitNodeStartSpan;
20812090
emitEnd = recordEmitNodeEndSpan;
20822091
emitToken = writeTextWithSpanRecord;
@@ -3036,7 +3045,7 @@ module ts {
30363045
// Returns 'true' if the code was actually indented, false otherwise.
30373046
// If the code is not indented, an optional valueToWriteWhenNotIndenting will be
30383047
// emitted instead.
3039-
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node, valueToWriteWhenNotIndenting?: string) {
3048+
function indentIfOnDifferentLines(parent: Node, node1: Node, node2: Node, valueToWriteWhenNotIndenting?: string): boolean {
30403049
var realNodesAreOnDifferentLines = preserveNewLines && !nodeIsSynthesized(parent) && !nodeEndIsOnSameLineAsNodeStart(node1, node2);
30413050

30423051
// Always use a newline for synthesized code if the synthesizer desires it.
@@ -3631,7 +3640,7 @@ module ts {
36313640
emitContainingModuleName(node);
36323641
write(".");
36333642
}
3634-
emitNode(node.name);
3643+
emitNodeWithoutSourceMap(node.name);
36353644
emitEnd(node.name);
36363645
}
36373646

@@ -3650,10 +3659,10 @@ module ts {
36503659
emitStart(specifier.name);
36513660
emitContainingModuleName(specifier);
36523661
write(".");
3653-
emitNode(specifier.name);
3662+
emitNodeWithoutSourceMap(specifier.name);
36543663
emitEnd(specifier.name);
36553664
write(" = ");
3656-
emitNode(name);
3665+
emitNodeWithoutSourceMap(name);
36573666
write(";");
36583667
});
36593668
}
@@ -4046,14 +4055,14 @@ module ts {
40464055
writeLine();
40474056
emitStart(p);
40484057
write("if (");
4049-
emitNode(p.name);
4058+
emitNodeWithoutSourceMap(p.name);
40504059
write(" === void 0)");
40514060
emitEnd(p);
40524061
write(" { ");
40534062
emitStart(p);
4054-
emitNode(p.name);
4063+
emitNodeWithoutSourceMap(p.name);
40554064
write(" = ");
4056-
emitNode(p.initializer);
4065+
emitNodeWithoutSourceMap(p.initializer);
40574066
emitEnd(p);
40584067
write("; }");
40594068
}
@@ -4070,7 +4079,7 @@ module ts {
40704079
emitLeadingComments(restParam);
40714080
emitStart(restParam);
40724081
write("var ");
4073-
emitNode(restParam.name);
4082+
emitNodeWithoutSourceMap(restParam.name);
40744083
write(" = [];");
40754084
emitEnd(restParam);
40764085
emitTrailingComments(restParam);
@@ -4091,7 +4100,7 @@ module ts {
40914100
increaseIndent();
40924101
writeLine();
40934102
emitStart(restParam);
4094-
emitNode(restParam.name);
4103+
emitNodeWithoutSourceMap(restParam.name);
40954104
write("[" + tempName + " - " + restIndex + "] = arguments[" + tempName + "];");
40964105
emitEnd(restParam);
40974106
decreaseIndent();
@@ -4112,7 +4121,7 @@ module ts {
41124121

41134122
function emitDeclarationName(node: Declaration) {
41144123
if (node.name) {
4115-
emitNode(node.name);
4124+
emitNodeWithoutSourceMap(node.name);
41164125
}
41174126
else {
41184127
write(resolver.getGeneratedNameForNode(node));
@@ -4273,7 +4282,7 @@ module ts {
42734282

42744283
// Don't emit comments on this body. We'll have already taken care of it above
42754284
// when we called emitDetachedComments.
4276-
emitNode(body, /*disableComments:*/ true);
4285+
emitWithoutComments(body);
42774286
emitEnd(body);
42784287
write(";");
42794288
emitTempDeclarations(/*newLine*/ false);
@@ -4284,7 +4293,10 @@ module ts {
42844293
writeLine();
42854294
emitLeadingComments(node.body);
42864295
write("return ");
4287-
emit(node.body, /*disableComments:*/ true);
4296+
4297+
// Don't emit comments on this body. We'll have already taken care of it above
4298+
// when we called emitDetachedComments.
4299+
emitWithoutComments(node.body);
42884300
write(";");
42894301
emitTrailingComments(node.body);
42904302

@@ -4362,7 +4374,7 @@ module ts {
43624374
emitStart(param);
43634375
emitStart(param.name);
43644376
write("this.");
4365-
emitNode(param.name);
4377+
emitNodeWithoutSourceMap(param.name);
43664378
emitEnd(param.name);
43674379
write(" = ");
43684380
emit(param.name);
@@ -4376,15 +4388,15 @@ module ts {
43764388
// TODO: (jfreeman,drosen): comment on why this is emitNode instead of emit here.
43774389
if (memberName.kind === SyntaxKind.StringLiteral || memberName.kind === SyntaxKind.NumericLiteral) {
43784390
write("[");
4379-
emitNode(memberName);
4391+
emitNodeWithoutSourceMap(memberName);
43804392
write("]");
43814393
}
43824394
else if (memberName.kind === SyntaxKind.ComputedPropertyName) {
43834395
emitComputedPropertyName(<ComputedPropertyName>memberName);
43844396
}
43854397
else {
43864398
write(".");
4387-
emitNode(memberName);
4399+
emitNodeWithoutSourceMap(memberName);
43884400
}
43894401
}
43904402

@@ -4877,11 +4889,11 @@ module ts {
48774889
emitStart(specifier);
48784890
emitContainingModuleName(specifier);
48794891
write(".");
4880-
emitNode(specifier.name);
4892+
emitNodeWithoutSourceMap(specifier.name);
48814893
write(" = ");
48824894
write(generatedName);
48834895
write(".");
4884-
emitNode(specifier.propertyName || specifier.name);
4896+
emitNodeWithoutSourceMap(specifier.propertyName || specifier.name);
48854897
write(";");
48864898
emitEnd(specifier);
48874899
});
@@ -5146,7 +5158,7 @@ module ts {
51465158
emitLeadingComments(node.endOfFileToken);
51475159
}
51485160

5149-
function emitNode(node: Node, disableComments?:boolean): void {
5161+
function emitNodeWithoutSourceMapWithComments(node: Node): void {
51505162
if (!node) {
51515163
return;
51525164
}
@@ -5155,7 +5167,7 @@ module ts {
51555167
return emitPinnedOrTripleSlashComments(node);
51565168
}
51575169

5158-
var emitComments = !disableComments && shouldEmitLeadingAndTrailingComments(node);
5170+
var emitComments = shouldEmitLeadingAndTrailingComments(node);
51595171
if (emitComments) {
51605172
emitLeadingComments(node);
51615173
}
@@ -5167,6 +5179,18 @@ module ts {
51675179
}
51685180
}
51695181

5182+
function emitNodeWithoutSourceMapWithoutComments(node: Node): void {
5183+
if (!node) {
5184+
return;
5185+
}
5186+
5187+
if (node.flags & NodeFlags.Ambient) {
5188+
return emitPinnedOrTripleSlashComments(node);
5189+
}
5190+
5191+
emitJavaScriptWorker(node);
5192+
}
5193+
51705194
function shouldEmitLeadingAndTrailingComments(node: Node) {
51715195
switch (node.kind) {
51725196
// All of these entities are emitted in a specialized fashion. As such, we allow
@@ -5461,7 +5485,8 @@ module ts {
54615485
}
54625486
}
54635487

5464-
function emitPinnedOrTripleSlashCommentsOfNode(node: Node) {
5488+
/** Emits /// or pinned which is comment starting with /*! comments */
5489+
function emitPinnedOrTripleSlashComments(node: Node) {
54655490
var pinnedComments = ts.filter(getLeadingCommentsToEmit(node), isPinnedOrTripleSlashComment);
54665491

54675492
function isPinnedOrTripleSlashComment(comment: CommentRange) {

0 commit comments

Comments
 (0)