Skip to content

Commit e9115ca

Browse files
committed
Simplify disabling comments recursively, cleanup unused flags.
1 parent e063bb0 commit e9115ca

6 files changed

Lines changed: 70 additions & 52 deletions

File tree

src/compiler/comments.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace ts {
2626
let hasWrittenComment = false;
2727
let hasLastComment: boolean;
2828
let lastCommentEnd: number;
29+
let disabled: boolean = compilerOptions.removeComments;
2930

3031
return {
3132
reset,
@@ -36,24 +37,29 @@ namespace ts {
3637
};
3738

3839
function emitNodeWithComments(node: Node, emitCallback: (node: Node) => void) {
39-
if (compilerOptions.removeComments) {
40+
if (disabled) {
4041
emitCallback(node);
4142
return;
4243
}
4344

4445
if (node) {
4546
const { pos, end } = node.commentRange || node;
47+
const emitFlags = node.emitFlags;
4648
if ((pos < 0 && end < 0) || (pos === end)) {
4749
// Both pos and end are synthesized, so just emit the node without comments.
48-
emitCallback(node);
50+
if (emitFlags & NodeEmitFlags.NoNestedComments) {
51+
disableCommentsAndEmit(node, emitCallback);
52+
}
53+
else {
54+
emitCallback(node);
55+
}
4956
}
5057
else {
5158
let commentStart: number;
5259
if (extendedDiagnostics) {
5360
commentStart = performance.mark();
5461
}
5562

56-
const emitFlags = node.emitFlags;
5763
const isEmittedNode = node.kind !== SyntaxKind.NotEmittedStatement;
5864
const skipLeadingComments = pos < 0 || (emitFlags & NodeEmitFlags.NoLeadingComments) !== 0;
5965
const skipTrailingComments = end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0;
@@ -85,13 +91,19 @@ namespace ts {
8591

8692
if (extendedDiagnostics) {
8793
performance.measure("commentTime", commentStart);
88-
emitCallback(node);
89-
commentStart = performance.mark();
94+
}
95+
96+
if (emitFlags & NodeEmitFlags.NoNestedComments) {
97+
disableCommentsAndEmit(node, emitCallback);
9098
}
9199
else {
92100
emitCallback(node);
93101
}
94102

103+
if (extendedDiagnostics) {
104+
commentStart = performance.mark();
105+
}
106+
95107
// Restore previous container state.
96108
containerPos = savedContainerPos;
97109
containerEnd = savedContainerEnd;
@@ -119,21 +131,27 @@ namespace ts {
119131
const { pos, end } = detachedRange;
120132
const emitFlags = node.emitFlags;
121133
const skipLeadingComments = pos < 0 || (emitFlags & NodeEmitFlags.NoLeadingComments) !== 0;
122-
const skipTrailingComments = end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0 || compilerOptions.removeComments;
134+
const skipTrailingComments = disabled || end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0;
123135

124136
if (!skipLeadingComments) {
125137
emitDetachedCommentsAndUpdateCommentsInfo(detachedRange);
126138
}
127139

128140
if (extendedDiagnostics) {
129141
performance.measure("commentTime", commentStart);
130-
emitCallback(node);
131-
commentStart = performance.mark();
142+
}
143+
144+
if (emitFlags & NodeEmitFlags.NoNestedComments) {
145+
disableCommentsAndEmit(node, emitCallback);
132146
}
133147
else {
134148
emitCallback(node);
135149
}
136150

151+
if (extendedDiagnostics) {
152+
commentStart = performance.mark();
153+
}
154+
137155
if (!skipTrailingComments) {
138156
emitLeadingComments(detachedRange.end, /*isEmittedNode*/ true);
139157
}
@@ -207,7 +225,7 @@ namespace ts {
207225
}
208226

209227
function emitTrailingCommentsOfPosition(pos: number) {
210-
if (compilerOptions.removeComments) {
228+
if (disabled) {
211229
return;
212230
}
213231

@@ -269,6 +287,18 @@ namespace ts {
269287
currentText = currentSourceFile.text;
270288
currentLineMap = getLineStarts(currentSourceFile);
271289
detachedCommentsInfo = undefined;
290+
disabled = false;
291+
}
292+
293+
function disableCommentsAndEmit(node: Node, emitCallback: (node: Node) => void): void {
294+
if (disabled) {
295+
emitCallback(node);
296+
}
297+
else {
298+
disabled = true;
299+
emitCallback(node);
300+
disabled = false;
301+
}
272302
}
273303

274304
function hasDetachedComments(pos: number) {
@@ -289,7 +319,7 @@ namespace ts {
289319
}
290320

291321
function emitDetachedCommentsAndUpdateCommentsInfo(range: TextRange) {
292-
const currentDetachedCommentInfo = emitDetachedComments(currentText, currentLineMap, writer, writeComment, range, newLine, compilerOptions.removeComments);
322+
const currentDetachedCommentInfo = emitDetachedComments(currentText, currentLineMap, writer, writeComment, range, newLine, disabled);
293323
if (currentDetachedCommentInfo) {
294324
if (detachedCommentsInfo) {
295325
detachedCommentsInfo.push(currentDetachedCommentInfo);

src/compiler/factory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,11 +1882,11 @@ namespace ts {
18821882

18831883
export function setOriginalNode<T extends Node>(node: T, original: Node): T {
18841884
node.original = original;
1885-
if (original && original.transformId && !node.transformId) {
1886-
node.transformId = original.transformId;
1887-
node.emitFlags = original.emitFlags;
1888-
node.commentRange = original.commentRange;
1889-
node.sourceMapRange = original.sourceMapRange;
1885+
if (original) {
1886+
const { emitFlags, commentRange, sourceMapRange } = original;
1887+
if (emitFlags) node.emitFlags = emitFlags;
1888+
if (commentRange) node.commentRange = commentRange;
1889+
if (sourceMapRange) node.sourceMapRange = sourceMapRange;
18901890
}
18911891
return node;
18921892
}

src/compiler/printer.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,17 +2065,11 @@ const _super = (function (geti, seti) {
20652065
function emitSourceFileWorker(node: SourceFile) {
20662066
const statements = node.statements;
20672067
const statementOffset = emitPrologueDirectives(statements);
2068-
if (getNodeEmitFlags(node) & NodeEmitFlags.NoLexicalEnvironment) {
2069-
emitHelpers(node);
2070-
emitList(node, statements, ListFormat.MultiLine, statementOffset);
2071-
}
2072-
else {
2073-
const savedTempFlags = tempFlags;
2074-
tempFlags = 0;
2075-
emitHelpers(node);
2076-
emitList(node, statements, ListFormat.MultiLine, statementOffset);
2077-
tempFlags = savedTempFlags;
2078-
}
2068+
const savedTempFlags = tempFlags;
2069+
tempFlags = 0;
2070+
emitHelpers(node);
2071+
emitList(node, statements, ListFormat.MultiLine, statementOffset);
2072+
tempFlags = savedTempFlags;
20792073
}
20802074

20812075
// Transformation nodes

src/compiler/transformer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,12 @@ namespace ts {
300300
* @param node The node.
301301
*/
302302
function beforeSetAnnotation(node: Node) {
303-
node.transformId = transformId;
304-
if ((node.flags & NodeFlags.Synthesized) === 0) {
303+
if ((node.flags & NodeFlags.Synthesized) === 0 && node.transformId !== transformId) {
305304
// To avoid holding onto transformation artifacts, we keep track of any
306305
// source tree node we are annotating. This allows us to clean them up after
307306
// all transformations have completed.
308307
sourceTreeNodesWithAnnotations.push(node);
308+
node.transformId = transformId;
309309
}
310310
}
311311

@@ -318,7 +318,7 @@ namespace ts {
318318
* @param node The node.
319319
*/
320320
function getNodeEmitFlags(node: Node) {
321-
return node.emitFlags & ~NodeEmitFlags.HasNodeEmitFlags;
321+
return node.emitFlags;
322322
}
323323

324324
/**
@@ -329,7 +329,7 @@ namespace ts {
329329
*/
330330
function setNodeEmitFlags<T extends Node>(node: T, emitFlags: NodeEmitFlags) {
331331
beforeSetAnnotation(node);
332-
node.emitFlags = emitFlags | NodeEmitFlags.HasNodeEmitFlags;
332+
node.emitFlags = emitFlags;
333333
return node;
334334
}
335335

src/compiler/transformers/ts.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,11 +2768,6 @@ namespace ts {
27682768
&& resolver.isTopLevelValueImportEqualsWithEntityName(node));
27692769
}
27702770

2771-
function disableCommentsRecursive(node: Node) {
2772-
setNodeEmitFlags(node, NodeEmitFlags.NoComments | getNodeEmitFlags(node));
2773-
forEachChild(node, disableCommentsRecursive);
2774-
}
2775-
27762771
/**
27772772
* Visits an import equals declaration.
27782773
*
@@ -2788,7 +2783,8 @@ namespace ts {
27882783
}
27892784

27902785
const moduleReference = createExpressionFromEntityName(<EntityName>node.moduleReference);
2791-
disableCommentsRecursive(moduleReference);
2786+
setNodeEmitFlags(moduleReference, NodeEmitFlags.NoComments | NodeEmitFlags.NoNestedComments);
2787+
27922788
if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) {
27932789
// export var ${name} = ${moduleReference};
27942790
// var ${name} = ${moduleReference};

src/compiler/types.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,21 +2984,21 @@ namespace ts {
29842984
EmitSuperHelper = 1 << 2, // Emit the basic _super helper for async methods.
29852985
EmitAdvancedSuperHelper = 1 << 3, // Emit the advanced _super helper for async methods.
29862986
UMDDefine = 1 << 4, // This node should be replaced with the UMD define helper.
2987-
NoLexicalEnvironment = 1 << 5, // A new LexicalEnvironment should *not* be introduced when emitting this node, this is primarily used when printing a SystemJS module.
2988-
SingleLine = 1 << 6, // The contents of this node should be emitted on a single line.
2989-
AdviseOnEmitNode = 1 << 7, // The printer should invoke the onEmitNode callback when printing this node.
2990-
NoSubstitution = 1 << 8, // Disables further substitution of an expression.
2991-
CapturesThis = 1 << 9, // The function captures a lexical `this`
2992-
NoLeadingSourceMap = 1 << 10, // Do not emit a leading source map location for this node.
2993-
NoTrailingSourceMap = 1 << 11, // Do not emit a trailing source map location for this node.
2987+
SingleLine = 1 << 5, // The contents of this node should be emitted on a single line.
2988+
AdviseOnEmitNode = 1 << 6, // The printer should invoke the onEmitNode callback when printing this node.
2989+
NoSubstitution = 1 << 7, // Disables further substitution of an expression.
2990+
CapturesThis = 1 << 8, // The function captures a lexical `this`
2991+
NoLeadingSourceMap = 1 << 9, // Do not emit a leading source map location for this node.
2992+
NoTrailingSourceMap = 1 << 10, // Do not emit a trailing source map location for this node.
29942993
NoSourceMap = NoLeadingSourceMap | NoTrailingSourceMap, // Do not emit a source map location for this node.
2995-
NoNestedSourceMaps = 1 << 12, // Do not emit source map locations for children of this node.
2996-
NoTokenLeadingSourceMaps = 1 << 13, // Do not emit leading source map location for token nodes.
2997-
NoTokenTrailingSourceMaps = 1 << 14, // Do not emit trailing source map location for token nodes.
2994+
NoNestedSourceMaps = 1 << 11, // Do not emit source map locations for children of this node.
2995+
NoTokenLeadingSourceMaps = 1 << 12, // Do not emit leading source map location for token nodes.
2996+
NoTokenTrailingSourceMaps = 1 << 13, // Do not emit trailing source map location for token nodes.
29982997
NoTokenSourceMaps = NoTokenLeadingSourceMaps | NoTokenTrailingSourceMaps, // Do not emit source map locations for tokens of this node.
2999-
NoLeadingComments = 1 << 15, // Do not emit leading comments for this node.
3000-
NoTrailingComments = 1 << 16, // Do not emit trailing comments for this node.
2998+
NoLeadingComments = 1 << 14, // Do not emit leading comments for this node.
2999+
NoTrailingComments = 1 << 15, // Do not emit trailing comments for this node.
30013000
NoComments = NoLeadingComments | NoTrailingComments, // Do not emit comments for this node.
3001+
NoNestedComments = 1 << 16,
30023002
ExportName = 1 << 17, // Ensure an export prefix is added for an identifier that points to an exported declaration with a local name (see SymbolFlags.ExportHasLocal).
30033003
LocalName = 1 << 18, // Ensure an export prefix is not added for an identifier that points to an exported declaration.
30043004
Indented = 1 << 19, // Adds an explicit extra indentation level for class and function bodies when printing (used to match old emitter).
@@ -3007,10 +3007,8 @@ namespace ts {
30073007
// TODO(rbuckton): These should be removed once source maps are aligned with the old
30083008
// emitter and new baselines are taken. This exists solely to
30093009
// align with the old emitter.
3010-
SourceMapEmitOpenBraceAsToken = 1 << 21, // Emits the open brace of a block function body as a source mapped token.
3011-
SourceMapAdjustRestParameterLoop = 1 << 22, // Emits adjusted source map positions for a ForStatement generated when transforming a rest parameter for ES5/3.
3012-
3013-
HasNodeEmitFlags = 1 << 31, // Indicates the node has emit flags set.
3010+
SourceMapEmitOpenBraceAsToken = 1 << 20, // Emits the open brace of a block function body as a source mapped token.
3011+
SourceMapAdjustRestParameterLoop = 1 << 21, // Emits adjusted source map positions for a ForStatement generated when transforming a rest parameter for ES5/3.
30143012
}
30153013

30163014
/** Additional context provided to `visitEachChild` */

0 commit comments

Comments
 (0)