From e9115cad19b178336a96370a5bf35ea3ea49642b Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 31 May 2016 16:24:20 -0700 Subject: [PATCH] Simplify disabling comments recursively, cleanup unused flags. --- src/compiler/comments.ts | 50 ++++++++++++++++++++++++++------- src/compiler/factory.ts | 10 +++---- src/compiler/printer.ts | 16 ++++------- src/compiler/transformer.ts | 8 +++--- src/compiler/transformers/ts.ts | 8 ++---- src/compiler/types.ts | 30 +++++++++----------- 6 files changed, 70 insertions(+), 52 deletions(-) diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index 3c2324d352c..57611d1498a 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -26,6 +26,7 @@ namespace ts { let hasWrittenComment = false; let hasLastComment: boolean; let lastCommentEnd: number; + let disabled: boolean = compilerOptions.removeComments; return { reset, @@ -36,16 +37,22 @@ namespace ts { }; function emitNodeWithComments(node: Node, emitCallback: (node: Node) => void) { - if (compilerOptions.removeComments) { + if (disabled) { emitCallback(node); return; } if (node) { const { pos, end } = node.commentRange || node; + const emitFlags = node.emitFlags; if ((pos < 0 && end < 0) || (pos === end)) { // Both pos and end are synthesized, so just emit the node without comments. - emitCallback(node); + if (emitFlags & NodeEmitFlags.NoNestedComments) { + disableCommentsAndEmit(node, emitCallback); + } + else { + emitCallback(node); + } } else { let commentStart: number; @@ -53,7 +60,6 @@ namespace ts { commentStart = performance.mark(); } - const emitFlags = node.emitFlags; const isEmittedNode = node.kind !== SyntaxKind.NotEmittedStatement; const skipLeadingComments = pos < 0 || (emitFlags & NodeEmitFlags.NoLeadingComments) !== 0; const skipTrailingComments = end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0; @@ -85,13 +91,19 @@ namespace ts { if (extendedDiagnostics) { performance.measure("commentTime", commentStart); - emitCallback(node); - commentStart = performance.mark(); + } + + if (emitFlags & NodeEmitFlags.NoNestedComments) { + disableCommentsAndEmit(node, emitCallback); } else { emitCallback(node); } + if (extendedDiagnostics) { + commentStart = performance.mark(); + } + // Restore previous container state. containerPos = savedContainerPos; containerEnd = savedContainerEnd; @@ -119,7 +131,7 @@ namespace ts { const { pos, end } = detachedRange; const emitFlags = node.emitFlags; const skipLeadingComments = pos < 0 || (emitFlags & NodeEmitFlags.NoLeadingComments) !== 0; - const skipTrailingComments = end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0 || compilerOptions.removeComments; + const skipTrailingComments = disabled || end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0; if (!skipLeadingComments) { emitDetachedCommentsAndUpdateCommentsInfo(detachedRange); @@ -127,13 +139,19 @@ namespace ts { if (extendedDiagnostics) { performance.measure("commentTime", commentStart); - emitCallback(node); - commentStart = performance.mark(); + } + + if (emitFlags & NodeEmitFlags.NoNestedComments) { + disableCommentsAndEmit(node, emitCallback); } else { emitCallback(node); } + if (extendedDiagnostics) { + commentStart = performance.mark(); + } + if (!skipTrailingComments) { emitLeadingComments(detachedRange.end, /*isEmittedNode*/ true); } @@ -207,7 +225,7 @@ namespace ts { } function emitTrailingCommentsOfPosition(pos: number) { - if (compilerOptions.removeComments) { + if (disabled) { return; } @@ -269,6 +287,18 @@ namespace ts { currentText = currentSourceFile.text; currentLineMap = getLineStarts(currentSourceFile); detachedCommentsInfo = undefined; + disabled = false; + } + + function disableCommentsAndEmit(node: Node, emitCallback: (node: Node) => void): void { + if (disabled) { + emitCallback(node); + } + else { + disabled = true; + emitCallback(node); + disabled = false; + } } function hasDetachedComments(pos: number) { @@ -289,7 +319,7 @@ namespace ts { } function emitDetachedCommentsAndUpdateCommentsInfo(range: TextRange) { - const currentDetachedCommentInfo = emitDetachedComments(currentText, currentLineMap, writer, writeComment, range, newLine, compilerOptions.removeComments); + const currentDetachedCommentInfo = emitDetachedComments(currentText, currentLineMap, writer, writeComment, range, newLine, disabled); if (currentDetachedCommentInfo) { if (detachedCommentsInfo) { detachedCommentsInfo.push(currentDetachedCommentInfo); diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index d4a03468ca0..2ea5de24901 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -1882,11 +1882,11 @@ namespace ts { export function setOriginalNode(node: T, original: Node): T { node.original = original; - if (original && original.transformId && !node.transformId) { - node.transformId = original.transformId; - node.emitFlags = original.emitFlags; - node.commentRange = original.commentRange; - node.sourceMapRange = original.sourceMapRange; + if (original) { + const { emitFlags, commentRange, sourceMapRange } = original; + if (emitFlags) node.emitFlags = emitFlags; + if (commentRange) node.commentRange = commentRange; + if (sourceMapRange) node.sourceMapRange = sourceMapRange; } return node; } diff --git a/src/compiler/printer.ts b/src/compiler/printer.ts index d58c580972b..6a72181373a 100644 --- a/src/compiler/printer.ts +++ b/src/compiler/printer.ts @@ -2065,17 +2065,11 @@ const _super = (function (geti, seti) { function emitSourceFileWorker(node: SourceFile) { const statements = node.statements; const statementOffset = emitPrologueDirectives(statements); - if (getNodeEmitFlags(node) & NodeEmitFlags.NoLexicalEnvironment) { - emitHelpers(node); - emitList(node, statements, ListFormat.MultiLine, statementOffset); - } - else { - const savedTempFlags = tempFlags; - tempFlags = 0; - emitHelpers(node); - emitList(node, statements, ListFormat.MultiLine, statementOffset); - tempFlags = savedTempFlags; - } + const savedTempFlags = tempFlags; + tempFlags = 0; + emitHelpers(node); + emitList(node, statements, ListFormat.MultiLine, statementOffset); + tempFlags = savedTempFlags; } // Transformation nodes diff --git a/src/compiler/transformer.ts b/src/compiler/transformer.ts index 70a73ea90e0..60240372f0f 100644 --- a/src/compiler/transformer.ts +++ b/src/compiler/transformer.ts @@ -300,12 +300,12 @@ namespace ts { * @param node The node. */ function beforeSetAnnotation(node: Node) { - node.transformId = transformId; - if ((node.flags & NodeFlags.Synthesized) === 0) { + if ((node.flags & NodeFlags.Synthesized) === 0 && node.transformId !== transformId) { // To avoid holding onto transformation artifacts, we keep track of any // source tree node we are annotating. This allows us to clean them up after // all transformations have completed. sourceTreeNodesWithAnnotations.push(node); + node.transformId = transformId; } } @@ -318,7 +318,7 @@ namespace ts { * @param node The node. */ function getNodeEmitFlags(node: Node) { - return node.emitFlags & ~NodeEmitFlags.HasNodeEmitFlags; + return node.emitFlags; } /** @@ -329,7 +329,7 @@ namespace ts { */ function setNodeEmitFlags(node: T, emitFlags: NodeEmitFlags) { beforeSetAnnotation(node); - node.emitFlags = emitFlags | NodeEmitFlags.HasNodeEmitFlags; + node.emitFlags = emitFlags; return node; } diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 0b590801b91..d11b2f5663d 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2768,11 +2768,6 @@ namespace ts { && resolver.isTopLevelValueImportEqualsWithEntityName(node)); } - function disableCommentsRecursive(node: Node) { - setNodeEmitFlags(node, NodeEmitFlags.NoComments | getNodeEmitFlags(node)); - forEachChild(node, disableCommentsRecursive); - } - /** * Visits an import equals declaration. * @@ -2788,7 +2783,8 @@ namespace ts { } const moduleReference = createExpressionFromEntityName(node.moduleReference); - disableCommentsRecursive(moduleReference); + setNodeEmitFlags(moduleReference, NodeEmitFlags.NoComments | NodeEmitFlags.NoNestedComments); + if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) { // export var ${name} = ${moduleReference}; // var ${name} = ${moduleReference}; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 528093e8050..30899f7aa2a 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2984,21 +2984,21 @@ namespace ts { EmitSuperHelper = 1 << 2, // Emit the basic _super helper for async methods. EmitAdvancedSuperHelper = 1 << 3, // Emit the advanced _super helper for async methods. UMDDefine = 1 << 4, // This node should be replaced with the UMD define helper. - NoLexicalEnvironment = 1 << 5, // A new LexicalEnvironment should *not* be introduced when emitting this node, this is primarily used when printing a SystemJS module. - SingleLine = 1 << 6, // The contents of this node should be emitted on a single line. - AdviseOnEmitNode = 1 << 7, // The printer should invoke the onEmitNode callback when printing this node. - NoSubstitution = 1 << 8, // Disables further substitution of an expression. - CapturesThis = 1 << 9, // The function captures a lexical `this` - NoLeadingSourceMap = 1 << 10, // Do not emit a leading source map location for this node. - NoTrailingSourceMap = 1 << 11, // Do not emit a trailing source map location for this node. + SingleLine = 1 << 5, // The contents of this node should be emitted on a single line. + AdviseOnEmitNode = 1 << 6, // The printer should invoke the onEmitNode callback when printing this node. + NoSubstitution = 1 << 7, // Disables further substitution of an expression. + CapturesThis = 1 << 8, // The function captures a lexical `this` + NoLeadingSourceMap = 1 << 9, // Do not emit a leading source map location for this node. + NoTrailingSourceMap = 1 << 10, // Do not emit a trailing source map location for this node. NoSourceMap = NoLeadingSourceMap | NoTrailingSourceMap, // Do not emit a source map location for this node. - NoNestedSourceMaps = 1 << 12, // Do not emit source map locations for children of this node. - NoTokenLeadingSourceMaps = 1 << 13, // Do not emit leading source map location for token nodes. - NoTokenTrailingSourceMaps = 1 << 14, // Do not emit trailing source map location for token nodes. + NoNestedSourceMaps = 1 << 11, // Do not emit source map locations for children of this node. + NoTokenLeadingSourceMaps = 1 << 12, // Do not emit leading source map location for token nodes. + NoTokenTrailingSourceMaps = 1 << 13, // Do not emit trailing source map location for token nodes. NoTokenSourceMaps = NoTokenLeadingSourceMaps | NoTokenTrailingSourceMaps, // Do not emit source map locations for tokens of this node. - NoLeadingComments = 1 << 15, // Do not emit leading comments for this node. - NoTrailingComments = 1 << 16, // Do not emit trailing comments for this node. + NoLeadingComments = 1 << 14, // Do not emit leading comments for this node. + NoTrailingComments = 1 << 15, // Do not emit trailing comments for this node. NoComments = NoLeadingComments | NoTrailingComments, // Do not emit comments for this node. + NoNestedComments = 1 << 16, 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). LocalName = 1 << 18, // Ensure an export prefix is not added for an identifier that points to an exported declaration. 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 { // TODO(rbuckton): These should be removed once source maps are aligned with the old // emitter and new baselines are taken. This exists solely to // align with the old emitter. - SourceMapEmitOpenBraceAsToken = 1 << 21, // Emits the open brace of a block function body as a source mapped token. - SourceMapAdjustRestParameterLoop = 1 << 22, // Emits adjusted source map positions for a ForStatement generated when transforming a rest parameter for ES5/3. - - HasNodeEmitFlags = 1 << 31, // Indicates the node has emit flags set. + SourceMapEmitOpenBraceAsToken = 1 << 20, // Emits the open brace of a block function body as a source mapped token. + SourceMapAdjustRestParameterLoop = 1 << 21, // Emits adjusted source map positions for a ForStatement generated when transforming a rest parameter for ES5/3. } /** Additional context provided to `visitEachChild` */