mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-29 16:29:19 -05:00
Simplify disabling comments recursively, cleanup unused flags.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -1882,11 +1882,11 @@ namespace ts {
|
||||
|
||||
export function setOriginalNode<T extends Node>(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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<T extends Node>(node: T, emitFlags: NodeEmitFlags) {
|
||||
beforeSetAnnotation(node);
|
||||
node.emitFlags = emitFlags | NodeEmitFlags.HasNodeEmitFlags;
|
||||
node.emitFlags = emitFlags;
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@@ -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(<EntityName>node.moduleReference);
|
||||
disableCommentsRecursive(moduleReference);
|
||||
setNodeEmitFlags(moduleReference, NodeEmitFlags.NoComments | NodeEmitFlags.NoNestedComments);
|
||||
|
||||
if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) {
|
||||
// export var ${name} = ${moduleReference};
|
||||
// var ${name} = ${moduleReference};
|
||||
|
||||
@@ -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` */
|
||||
|
||||
Reference in New Issue
Block a user