Skip to content

Commit 31cf443

Browse files
Add specialized codepath for emitting without comments.
1 parent bfbc18d commit 31cf443

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/compiler/emitter.ts

Lines changed: 35 additions & 10 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 */
1610+
var emitNodeWithoutSourceMap = compilerOptions.removeComments ? emitNodeWithoutSourceMapWithoutComments : emitNodeWithoutSourceMapWithComments;
16121611
var emit = emitNodeWithoutSourceMap;
1612+
var emitWithoutComments = emitNodeWithoutSourceMapWithoutComments;
16131613

16141614
/** Called just before starting emit of a node */
16151615
var emitStart = function (node: Node) { };
@@ -2061,7 +2061,7 @@ 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);
@@ -2075,8 +2075,17 @@ module ts {
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;
@@ -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-
emitNodeWithoutSourceMap(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

@@ -5146,7 +5158,7 @@ module ts {
51465158
emitLeadingComments(node.endOfFileToken);
51475159
}
51485160

5149-
function emitNodeWithoutSourceMap(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)