From 3bf4f2a6f54d12aadf26a04449d41e280baaf340 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 26 May 2016 16:48:13 -0700 Subject: [PATCH 1/7] PR feedback, removed now-redundant getUniqueClone --- src/compiler/comments.ts | 13 ++++--------- src/compiler/factory.ts | 15 +++------------ src/compiler/transformers/es6.ts | 4 ++-- src/compiler/transformers/ts.ts | 6 +++--- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index 7ad380f06af..79bb795efe5 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -82,12 +82,9 @@ namespace ts { function getLeadingComments(range: TextRange): CommentRange[]; function getLeadingComments(range: TextRange, contextNode: Node, ignoreNodeCallback: (contextNode: Node) => boolean, getTextRangeCallback: (contextNode: Node) => TextRange): CommentRange[]; function getLeadingComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean, getTextRangeCallback?: (contextNode: Node) => TextRange) { - let comments: CommentRange[] = []; - let ignored = false; if (contextNode) { range = getTextRangeCallback(contextNode) || range; if (ignoreNodeCallback(contextNode)) { - ignored = true; // If the node will not be emitted in JS, remove all the comments (normal, // pinned and `///`) associated with the node, unless it is a triple slash // comment at the top of the file. @@ -101,16 +98,14 @@ namespace ts { // The first `///` will NOT be removed while the second one will be removed // even though both nodes will not be emitted. if (range.pos === 0) { - comments = filter(getLeadingCommentsOfPosition(0), isTripleSlashComment); + return filter(getLeadingCommentsOfPosition(0), isTripleSlashComment); } + + return; } } - if (!ignored) { - comments = getLeadingCommentsOfPosition(range.pos); - } - - return comments; + return getLeadingCommentsOfPosition(range.pos); } /** diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 2fc5a437565..d04d7f0fd0d 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -91,16 +91,6 @@ namespace ts { return clone; } - /** - * Gets a clone of a node with a unique node ID. - */ - export function getUniqueClone(node: T): T { - const clone = getMutableClone(node); - clone.id = 0; - getNodeId(clone); - return clone; - } - // Literals export function createLiteral(textSource: StringLiteral | Identifier, location?: TextRange): StringLiteral; @@ -821,8 +811,9 @@ namespace ts { } function createReactNamespace(reactNamespace: string, parent: JsxOpeningLikeElement) { - // Create an identifier and give it a parent. This allows us to resolve the react - // namespace during emit. + // To ensure the emit resolver can properly resolve the namespace, we need to + // treat this identifier as if it were a source tree node by clearing the `Synthesized` + // flag and setting a parent node. const react = createIdentifier(reactNamespace || "React"); react.flags &= ~NodeFlags.Synthesized; react.parent = parent; diff --git a/src/compiler/transformers/es6.ts b/src/compiler/transformers/es6.ts index e2d41783982..1c412f98af7 100644 --- a/src/compiler/transformers/es6.ts +++ b/src/compiler/transformers/es6.ts @@ -1003,7 +1003,7 @@ namespace ts { } // `declarationName` is the name of the local declaration for the parameter. - const declarationName = getUniqueClone(parameter.name); + const declarationName = getMutableClone(parameter.name); setNodeEmitFlags(declarationName, NodeEmitFlags.NoSourceMap); // `expressionName` is the name of the parameter used in expressions. @@ -2916,7 +2916,7 @@ namespace ts { */ function getDeclarationName(node: DeclarationStatement | ClassExpression, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags?: NodeEmitFlags) { if (node.name && !isGeneratedIdentifier(node.name)) { - const name = getUniqueClone(node.name); + const name = getMutableClone(node.name); emitFlags |= getNodeEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= NodeEmitFlags.NoSourceMap; diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 488b76cc469..d4f7ea020cb 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -928,10 +928,10 @@ namespace ts { function transformParameterWithPropertyAssignment(node: ParameterDeclaration) { Debug.assert(isIdentifier(node.name)); const name = node.name as Identifier; - const propertyName = getUniqueClone(name); + const propertyName = getMutableClone(name); setNodeEmitFlags(propertyName, NodeEmitFlags.NoComments | NodeEmitFlags.NoSourceMap); - const localName = getUniqueClone(name); + const localName = getMutableClone(name); setNodeEmitFlags(localName, NodeEmitFlags.NoComments); return startOnNewLine( @@ -2922,7 +2922,7 @@ namespace ts { */ function getDeclarationName(node: DeclarationStatement | ClassExpression, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags?: NodeEmitFlags) { if (node.name) { - const name = getUniqueClone(node.name); + const name = getMutableClone(node.name); emitFlags |= getNodeEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= NodeEmitFlags.NoSourceMap; From 3c6ceaf85e4516db1b06ad59da71a5d55624d957 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 26 May 2016 18:25:11 -0700 Subject: [PATCH 2/7] Simplified performance timers --- src/compiler/binder.ts | 4 +- src/compiler/checker.ts | 4 +- src/compiler/comments.ts | 28 ++++---- src/compiler/core.ts | 132 ++++++-------------------------------- src/compiler/printer.ts | 4 +- src/compiler/program.ts | 16 ++--- src/compiler/sourcemap.ts | 20 +++--- src/compiler/tsc.ts | 27 ++------ 8 files changed, 63 insertions(+), 172 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 707a7567d72..fe225d92254 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -94,9 +94,9 @@ namespace ts { const binder = createBinder(); export function bindSourceFile(file: SourceFile, options: CompilerOptions) { - performance.mark("bindStart"); + const bindStart = performance.mark(); binder(file, options); - performance.measure("bindTime", "bindStart"); + performance.measure("bindTime", bindStart); } function createBinder(): (file: SourceFile, options: CompilerOptions) => void { diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 71e099bbf83..c03c5a163cf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16064,11 +16064,11 @@ namespace ts { } function checkSourceFile(node: SourceFile) { - performance.mark("checkStart"); + const checkStart = performance.mark(); checkSourceFileWorker(node); - performance.measure("checkTime", "checkStart"); + performance.measure("checkTime", checkStart); } // Fully type check a source file and collect the relevant diagnostics. diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index 79bb795efe5..a7c7c34ab40 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -272,42 +272,42 @@ namespace ts { reset, setSourceFile, getLeadingComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean, getTextRangeCallback?: (contextNode: Node) => TextRange): CommentRange[] { - performance.mark("commentStart"); + const commentStart = performance.mark(); const comments = getLeadingComments(range, contextNode, ignoreNodeCallback, getTextRangeCallback); - performance.measure("commentTime", "commentStart"); + performance.measure("commentTime", commentStart); return comments; }, getTrailingComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean, getTextRangeCallback?: (contextNode: Node) => TextRange): CommentRange[] { - performance.mark("commentStart"); + const commentStart = performance.mark(); const comments = getTrailingComments(range, contextNode, ignoreNodeCallback, getTextRangeCallback); - performance.measure("commentTime", "commentStart"); + performance.measure("commentTime", commentStart); return comments; }, getTrailingCommentsOfPosition(pos: number): CommentRange[] { - performance.mark("commentStart"); + const commentStart = performance.mark(); const comments = getTrailingCommentsOfPosition(pos); - performance.measure("commentTime", "commentStart"); + performance.measure("commentTime", commentStart); return comments; }, emitLeadingComments(range: TextRange, comments: CommentRange[], contextNode?: Node, getTextRangeCallback?: (contextNode: Node) => TextRange): void { - performance.mark("commentStart"); + const commentStart = performance.mark(); emitLeadingComments(range, comments, contextNode, getTextRangeCallback); - performance.measure("commentTime", "commentStart"); + performance.measure("commentTime", commentStart); }, emitTrailingComments(range: TextRange, comments: CommentRange[]): void { - performance.mark("commentStart"); + const commentStart = performance.mark(); emitLeadingComments(range, comments); - performance.measure("commentTime", "commentStart"); + performance.measure("commentTime", commentStart); }, emitLeadingDetachedComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean): void { - performance.mark("commentStart"); + const commentStart = performance.mark(); emitLeadingDetachedComments(range, contextNode, ignoreNodeCallback); - performance.measure("commentTime", "commentStart"); + performance.measure("commentTime", commentStart); }, emitTrailingDetachedComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean): void { - performance.mark("commentStart"); + const commentStart = performance.mark(); emitTrailingDetachedComments(range, contextNode, ignoreNodeCallback); - performance.measure("commentTime", "commentStart"); + performance.measure("commentTime", commentStart); } }; } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index aada521c2e4..1980cf9ad9e 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1139,158 +1139,64 @@ namespace ts { /** Performance measurements for the compiler. */ /*@internal*/ export namespace performance { - interface MarkData { - markName: string; - timestamp: number; - } - - interface MeasureData { - measureName: string; - startMarkName: string; - endMarkName: string; - timestamp: number; - marksOffset: number; - } - - export interface Measure { - name: string; - startTime: number; - duration: number; - } - - const markTimestamps: Map = {}; - const markCounts: Map = {}; - const measureDurations: Map = {}; - - let start = now(); + let counters: Map = {}; + let measures: Map = {}; let enabled = false; - /** Gets the current timer for performance measurements. */ - export function now() { - return Date.now(); - } - /** - * Adds a performance mark with the specified name. + * Increments a counter with the specified name. * - * @param markName The name of the performance mark. + * @param counterName The name of the counter. */ - export function mark(markName: string) { + export function increment(counterName: string) { if (enabled) { - markTimestamps[markName] = now(); - markCounts[markName] = getCount(markName) + 1; + counters[counterName] = (getProperty(counters, counterName) || 0) + 1; } } /** - * Gets the names of all marks. - */ - export function getMarkNames() { - return getKeys(markCounts); - } - - /** - * Gets the number of marks with the specified name. + * Gets the value of the counter with the specified name. * - * @param markName The name of the marks that should be counted. + * @param counterName The name of the counter. */ - export function getCount(markName: string) { - return enabled && getProperty(markCounts, markName) || 0; + export function getCount(counterName: string) { + return enabled && getProperty(counters, counterName) || 0; } /** - * Gets the most recent timestamp for the marks with the specified name. - * - * @param markName The name of the mark. + * Marks the start of a performance measurement. */ - export function getTimestamp(markName: string) { - return enabled && getProperty(markTimestamps, markName) || 0; - } - - /** - * Clears performance marks. - * - * @param markName The name of the mark whose time values should be cleared. If not - * specified, all marks will be cleared. - */ - export function clearMarks(markName?: string) { - if (markName === undefined) { - forEachKey(markTimestamps, clearMark); - } - else { - clearMark(markName); - } - } - - function clearMark(markName: string) { - if (delete markTimestamps[markName]) { - delete markCounts[markName]; - } + export function mark() { + return enabled ? Date.now() : 0; } /** * Adds a performance measurement with the specified name. * * @param measureName The name of the performance measurement. - * @param startMarkName The name of the starting mark. - * If provided, the most recent time value of the start mark is used. - * If not specified, the value is the time that the performance service was - * initialized or the last time it was reset. - * @param endMarkName The name of the ending mark. - * If provided, the most recent time value of the end mark is used. - * If not specified, the current time is used. + * @param marker The timestamp of the starting mark. */ - export function measure(measureName: string, startMarkName?: string, endMarkName?: string) { + export function measure(measureName: string, marker: number) { if (enabled) { - const startTime = startMarkName ? getTimestamp(startMarkName) : start; - const endTime = endMarkName ? getTimestamp(endMarkName) : now(); - const duration = endTime - startTime; - measureDurations[measureName] = getDuration(measureName) + duration; + measures[measureName] = (getProperty(measures, measureName) || 0) + (mark() - marker); } } - /** - * Gets the names of all recorded measures. - */ - export function getMeasureNames() { - return getKeys(measureDurations); - } - /** * Gets the total duration of all measurements with the supplied name. * * @param measureName The name of the measure whose durations should be accumulated. */ export function getDuration(measureName: string) { - return enabled && getProperty(measureDurations, measureName) || 0; - } - - /** - * Clears performance measures. - * - * @param measureName The name of the measure whose durations should be cleared. If not - * specified, all measures will be cleared. - */ - export function clearMeasures(measureName?: string) { - if (measureName === undefined) { - forEachKey(measureDurations, clearMeasure); - } - else { - clearMeasure(measureName); - } - } - - function clearMeasure(measureName: string) { - delete measureDurations[measureName]; + return enabled && getProperty(measures, measureName) || 0; } /** * Resets all marks and measurements in the performance service. */ export function reset() { - clearMarks(); - clearMeasures(); - start = now(); + counters = {}; + measures = {}; } /** Enables performance measurements for the compiler. */ diff --git a/src/compiler/printer.ts b/src/compiler/printer.ts index 12d399b04d8..c577f7999f5 100644 --- a/src/compiler/printer.ts +++ b/src/compiler/printer.ts @@ -281,9 +281,9 @@ const _super = (function (geti, seti) { } function printSourceFileWithExtendedDiagnostics(node: SourceFile) { - performance.mark("printStart"); + const printStart = performance.mark(); printSourceFile(node); - performance.measure("printTime", "printStart"); + performance.measure("printTime", printStart); return node; } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 5559f9d3740..03db19ed445 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -776,9 +776,9 @@ namespace ts { function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile { let text: string; try { - performance.mark("ioReadStart"); + const ioReadStart = performance.mark(); text = sys.readFile(fileName, options.charset); - performance.measure("ioReadTime", "ioReadStart"); + performance.measure("ioReadTime", ioReadStart); } catch (e) { if (onError) { @@ -845,7 +845,7 @@ namespace ts { function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) { try { - performance.mark("ioWriteStart"); + const ioWriteStart = performance.mark(); ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName))); if (isWatchSet(options) && sys.createHash && sys.getModifiedTime) { @@ -855,7 +855,7 @@ namespace ts { sys.writeFile(fileName, data, writeByteOrderMark); } - performance.measure("ioWriteTime", "ioWriteStart"); + performance.measure("ioWriteTime", ioWriteStart); } catch (e) { if (onError) { @@ -957,7 +957,7 @@ namespace ts { let resolvedTypeReferenceDirectives: Map = {}; let fileProcessingDiagnostics = createDiagnosticCollection(); - performance.mark("programStart"); + const programStart = performance.mark(); host = host || createCompilerHost(options); @@ -1050,7 +1050,7 @@ namespace ts { verifyCompilerOptions(); - performance.measure("programTime", "programStart"); + performance.measure("programTime", programStart); return program; @@ -1283,7 +1283,7 @@ namespace ts { // checked is to not pass the file to getEmitResolver. const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile); - performance.mark("emitStart"); + const emitStart = performance.mark(); // TODO(rbuckton): remove USE_TRANSFORMS condition when we switch to transforms permanently. let useLegacyEmitter = options.useLegacyEmitter; @@ -1297,7 +1297,7 @@ namespace ts { getEmitHost(writeFileCallback), sourceFile); - performance.measure("emitTime", "emitStart"); + performance.measure("emitTime", emitStart); return emitResult; } diff --git a/src/compiler/sourcemap.ts b/src/compiler/sourcemap.ts index 8e7cc3b1d44..de056196bd0 100644 --- a/src/compiler/sourcemap.ts +++ b/src/compiler/sourcemap.ts @@ -782,30 +782,30 @@ namespace ts { getSourceMapData, setSourceFile, emitPos(pos: number): void { - performance.mark("sourcemapStart"); + const sourcemapStart = performance.mark(); emitPos(pos); - performance.measure("sourcemapTime", "sourcemapStart"); + performance.measure("sourceMapTime", sourcemapStart); }, emitStart(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (node: Node) => boolean, ignoreChildrenCallback?: (node: Node) => boolean, getTextRangeCallback?: (node: Node) => TextRange): void { - performance.mark("sourcemapStart"); + const sourcemapStart = performance.mark(); emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - performance.measure("sourcemapTime", "sourcemapStart"); + performance.measure("sourceMapTime", sourcemapStart); }, emitEnd(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (node: Node) => boolean, ignoreChildrenCallback?: (node: Node) => boolean, getTextRangeCallback?: (node: Node) => TextRange): void { - performance.mark("sourcemapStart"); + const sourcemapStart = performance.mark(); emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - performance.measure("sourcemapTime", "sourcemapStart"); + performance.measure("sourceMapTime", sourcemapStart); }, emitTokenStart(token: SyntaxKind, tokenStartPos: number, contextNode?: Node, ignoreTokenCallback?: (node: Node) => boolean, getTokenTextRangeCallback?: (node: Node, token: SyntaxKind) => TextRange): number { - performance.mark("sourcemapStart"); + const sourcemapStart = performance.mark(); tokenStartPos = emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - performance.measure("sourcemapTime", "sourcemapStart"); + performance.measure("sourceMapTime", sourcemapStart); return tokenStartPos; }, emitTokenEnd(token: SyntaxKind, tokenEndPos: number, contextNode?: Node, ignoreTokenCallback?: (node: Node) => boolean, getTokenTextRangeCallback?: (node: Node, token: SyntaxKind) => TextRange): number { - performance.mark("sourcemapStart"); + const sourcemapStart = performance.mark(); tokenEndPos = emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - performance.measure("sourcemapTime", "sourcemapStart"); + performance.measure("sourceMapTime", sourcemapStart); return tokenEndPos; }, changeEmitSourcePos, diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 9d521c17f28..f7bb2f5d4d8 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -571,6 +571,12 @@ namespace ts { reportStatisticalValue("Memory used", Math.round(memoryUsed / 1000) + "K"); } + if (compilerOptions.extendedDiagnostics) { + reportTimeStatistic("Print time", performance.getDuration("printTime")); + reportTimeStatistic("Comment time", performance.getDuration("commentTime")); + reportTimeStatistic("SourceMap time", performance.getDuration("sourceMapTime")); + } + // Individual component times. // Note: To match the behavior of previous versions of the compiler, the reported parse time includes // I/O read time and processing time for triple-slash references and module imports, and the reported @@ -587,27 +593,6 @@ namespace ts { reportTimeStatistic("Emit time", emitTime); reportTimeStatistic("Total time", programTime + bindTime + checkTime + emitTime); - if (compilerOptions.extendedDiagnostics) { - sys.write("Extended Diagnostics:" + sys.newLine); - sys.write("Marks:" + sys.newLine); - for (const markName of performance.getMarkNames()) { - if (/^(ioReadStart|ioWriteStart|programStart|bindStart|checkStart|emitStart)$/.test(markName)) { - continue; - } - - reportCountStatistic(" " + markName, performance.getCount(markName)); - } - - sys.write("Measures:" + sys.newLine); - for (const measureName of performance.getMeasureNames()) { - if (/^(ioReadTime|ioWriteTime|programTime|bindTime|checkTime|emitTime)$/.test(measureName)) { - continue; - } - - reportTimeStatistic(" " + measureName, performance.getDuration(measureName)); - } - } - performance.disable(); performance.reset(); } From 3d3b3a45dca323c4bfccbc3f854a23c751720a06 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 26 May 2016 22:32:21 -0700 Subject: [PATCH 3/7] Fix unresolved merge issue. --- src/compiler/tsc.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 1ce96eff259..24c54e71170 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -607,6 +607,7 @@ namespace ts { reportTimeStatistic("Check time", checkTime); reportTimeStatistic("Emit time", emitTime); reportTimeStatistic("Total time", programTime + bindTime + checkTime + emitTime); + reportStatistics(); performance.disable(); performance.reset(); From 0d2b1c47f8eb6fd09257395332f243cf733b340b Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 27 May 2016 14:55:11 -0700 Subject: [PATCH 4/7] Moved code around to fix compile errors in processDiagnosticMessages script. --- src/compiler/core.ts | 12 +++++ src/compiler/transformer.ts | 94 +++++++++++++++++++++++++++++++++++++ src/compiler/types.ts | 93 ------------------------------------ src/compiler/utilities.ts | 12 ----- 4 files changed, 106 insertions(+), 105 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 1980cf9ad9e..de6e5357ea7 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1120,6 +1120,18 @@ namespace ts { } } + export function getEnvironmentVariable(name: string, host?: CompilerHost) { + if (host && host.getEnvironmentVariable) { + return host.getEnvironmentVariable(name); + } + + if (sys && sys.getEnvironmentVariable) { + return sys.getEnvironmentVariable(name); + } + + return ""; + } + export function copyListRemovingItem(item: T, list: T[]) { const copiedList: T[] = []; for (const e of list) { diff --git a/src/compiler/transformer.ts b/src/compiler/transformer.ts index 9e58b02c208..a448893f345 100644 --- a/src/compiler/transformer.ts +++ b/src/compiler/transformer.ts @@ -23,6 +23,100 @@ namespace ts { EmitNotifications = 1 << 1, } + /* @internal */ + export interface TransformationContext extends LexicalEnvironment { + getCompilerOptions(): CompilerOptions; + getEmitResolver(): EmitResolver; + getEmitHost(): EmitHost; + + /** + * Gets flags used to customize later transformations or emit. + */ + getNodeEmitFlags(node: Node): NodeEmitFlags; + + /** + * Sets flags used to customize later transformations or emit. + */ + setNodeEmitFlags(node: T, flags: NodeEmitFlags): T; + + /** + * Gets the TextRange to use for source maps for the node. + */ + getSourceMapRange(node: Node): TextRange; + + /** + * Sets the TextRange to use for source maps for the node. + */ + setSourceMapRange(node: T, range: TextRange): T; + + /** + * Gets the TextRange to use for source maps for a token of a node. + */ + getTokenSourceMapRange(node: Node, token: SyntaxKind): TextRange; + + /** + * Sets the TextRange to use for source maps for a token of a node. + */ + setTokenSourceMapRange(node: T, token: SyntaxKind, range: TextRange): T; + + /** + * Gets the TextRange to use for comments for the node. + */ + getCommentRange(node: Node): TextRange; + + /** + * Sets the TextRange to use for comments for the node. + */ + setCommentRange(node: T, range: TextRange): T; + + /** + * Hoists a function declaration to the containing scope. + */ + hoistFunctionDeclaration(node: FunctionDeclaration): void; + + /** + * Hoists a variable declaration to the containing scope. + */ + hoistVariableDeclaration(node: Identifier): void; + + /** + * Enables expression substitutions in the pretty printer for the provided SyntaxKind. + */ + enableSubstitution(kind: SyntaxKind): void; + + /** + * Determines whether expression substitutions are enabled for the provided node. + */ + isSubstitutionEnabled(node: Node): boolean; + + /** + * Hook used by transformers to substitute expressions just before they + * are emitted by the pretty printer. + */ + onSubstituteNode?: (node: Node, isExpression: boolean) => Node; + + /** + * Enables before/after emit notifications in the pretty printer for the provided + * SyntaxKind. + */ + enableEmitNotification(kind: SyntaxKind): void; + + /** + * Determines whether before/after emit notifications should be raised in the pretty + * printer when it emits a node. + */ + isEmitNotificationEnabled(node: Node): boolean; + + /** + * Hook used to allow transformers to capture state before or after + * the printer emits a node. + */ + onEmitNode?: (node: Node, emit: (node: Node) => void) => void; + } + + /* @internal */ + export type Transformer = (context: TransformationContext) => (node: SourceFile) => SourceFile; + export function getTransformers(compilerOptions: CompilerOptions) { const jsx = compilerOptions.jsx; const languageVersion = getEmitScriptTarget(compilerOptions); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 2b601b9c1ea..d868e024e0f 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2975,99 +2975,6 @@ namespace ts { endLexicalEnvironment(): Statement[]; } - /* @internal */ - export interface TransformationContext extends LexicalEnvironment { - getCompilerOptions(): CompilerOptions; - getEmitResolver(): EmitResolver; - getEmitHost(): EmitHost; - - /** - * Gets flags used to customize later transformations or emit. - */ - getNodeEmitFlags(node: Node): NodeEmitFlags; - - /** - * Sets flags used to customize later transformations or emit. - */ - setNodeEmitFlags(node: T, flags: NodeEmitFlags): T; - - /** - * Gets the TextRange to use for source maps for the node. - */ - getSourceMapRange(node: Node): TextRange; - - /** - * Sets the TextRange to use for source maps for the node. - */ - setSourceMapRange(node: T, range: TextRange): T; - - /** - * Gets the TextRange to use for source maps for a token of a node. - */ - getTokenSourceMapRange(node: Node, token: SyntaxKind): TextRange; - - /** - * Sets the TextRange to use for source maps for a token of a node. - */ - setTokenSourceMapRange(node: T, token: SyntaxKind, range: TextRange): T; - - /** - * Gets the TextRange to use for comments for the node. - */ - getCommentRange(node: Node): TextRange; - - /** - * Sets the TextRange to use for comments for the node. - */ - setCommentRange(node: T, range: TextRange): T; - - /** - * Hoists a function declaration to the containing scope. - */ - hoistFunctionDeclaration(node: FunctionDeclaration): void; - - /** - * Hoists a variable declaration to the containing scope. - */ - hoistVariableDeclaration(node: Identifier): void; - - /** - * Enables expression substitutions in the pretty printer for the provided SyntaxKind. - */ - enableSubstitution(kind: SyntaxKind): void; - - /** - * Determines whether expression substitutions are enabled for the provided node. - */ - isSubstitutionEnabled(node: Node): boolean; - - /** - * Hook used by transformers to substitute expressions just before they - * are emitted by the pretty printer. - */ - onSubstituteNode?: (node: Node, isExpression: boolean) => Node; - - /** - * Enables before/after emit notifications in the pretty printer for the provided - * SyntaxKind. - */ - enableEmitNotification(kind: SyntaxKind): void; - - /** - * Determines whether before/after emit notifications should be raised in the pretty - * printer when it emits a node. - */ - isEmitNotificationEnabled(node: Node): boolean; - - /** - * Hook used to allow transformers to capture state before or after - * the printer emits a node. - */ - onEmitNode?: (node: Node, emit: (node: Node) => void) => void; - } - - /* @internal */ - export type Transformer = (context: TransformationContext) => (node: SourceFile) => SourceFile; export interface TextSpan { start: number; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 655f731f10c..76c70195017 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -207,18 +207,6 @@ namespace ts { return `${ file.fileName }(${ loc.line + 1 },${ loc.character + 1 })`; } - export function getEnvironmentVariable(name: string, host?: CompilerHost) { - if (host && host.getEnvironmentVariable) { - return host.getEnvironmentVariable(name); - } - - if (sys && sys.getEnvironmentVariable) { - return sys.getEnvironmentVariable(name); - } - - return ""; - } - export function getStartPosOfNode(node: Node): number { return node.pos; } From 82e2531f6daad275e0bf5ba141856d224424cad5 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 27 May 2016 14:55:59 -0700 Subject: [PATCH 5/7] Performance API cleanup, pre-init with common values. --- src/compiler/core.ts | 43 +++++++++++++++++++++++-------------------- src/compiler/tsc.ts | 2 -- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index de6e5357ea7..324975a9da9 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1151,9 +1151,8 @@ namespace ts { /** Performance measurements for the compiler. */ /*@internal*/ export namespace performance { - let counters: Map = {}; - let measures: Map = {}; - let enabled = false; + let counters: Map; + let measures: Map; /** * Increments a counter with the specified name. @@ -1161,7 +1160,7 @@ namespace ts { * @param counterName The name of the counter. */ export function increment(counterName: string) { - if (enabled) { + if (counters) { counters[counterName] = (getProperty(counters, counterName) || 0) + 1; } } @@ -1172,14 +1171,14 @@ namespace ts { * @param counterName The name of the counter. */ export function getCount(counterName: string) { - return enabled && getProperty(counters, counterName) || 0; + return counters && getProperty(counters, counterName) || 0; } /** * Marks the start of a performance measurement. */ export function mark() { - return enabled ? Date.now() : 0; + return measures ? Date.now() : 0; } /** @@ -1189,7 +1188,7 @@ namespace ts { * @param marker The timestamp of the starting mark. */ export function measure(measureName: string, marker: number) { - if (enabled) { + if (measures) { measures[measureName] = (getProperty(measures, measureName) || 0) + (mark() - marker); } } @@ -1200,25 +1199,29 @@ namespace ts { * @param measureName The name of the measure whose durations should be accumulated. */ export function getDuration(measureName: string) { - return enabled && getProperty(measures, measureName) || 0; + return measures && getProperty(measures, measureName) || 0; } - /** - * Resets all marks and measurements in the performance service. - */ - export function reset() { - counters = {}; - measures = {}; - } - - /** Enables performance measurements for the compiler. */ + /** Enables (and resets) performance measurements for the compiler. */ export function enable() { - enabled = true; + counters = { }; + measures = { + programTime: 0, + parseTime: 0, + bindTime: 0, + emitTime: 0, + ioReadTime: 0, + ioWriteTime: 0, + printTime: 0, + commentTime: 0, + sourceMapTime: 0 + }; } - /** Disables performance measurements for the compiler. */ + /** Disables (and clears) performance measurements for the compiler. */ export function disable() { - enabled = false; + counters = undefined; + measures = undefined; } } } diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index f7bb2f5d4d8..2c15a972bf8 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -546,7 +546,6 @@ namespace ts { function compile(fileNames: string[], compilerOptions: CompilerOptions, compilerHost: CompilerHost) { if (compilerOptions.diagnostics || compilerOptions.extendedDiagnostics) { performance.enable(); - performance.reset(); } const program = createProgram(fileNames, compilerOptions, compilerHost); @@ -594,7 +593,6 @@ namespace ts { reportTimeStatistic("Total time", programTime + bindTime + checkTime + emitTime); performance.disable(); - performance.reset(); } return { program, exitStatus }; From 08bd78e4986f20b06a8d054969fc8ec302423d70 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 27 May 2016 16:52:44 -0700 Subject: [PATCH 6/7] Remove unused types. --- src/compiler/tsc.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 5a1bd894551..e2b6a77457d 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -11,16 +11,6 @@ namespace ts { value: string; } - interface Mark { - markName: string; - count: number; - } - - interface Measure { - measureName: string; - duration: number; - } - let reportDiagnostic = reportDiagnosticSimply; function reportDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): void { From 5fbb326d8d82a28c8e51768620564b6840039e45 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 27 May 2016 17:05:35 -0700 Subject: [PATCH 7/7] Fix various linter warnings. --- src/compiler/comments.ts | 2 +- src/compiler/factory.ts | 14 ++++++++------ src/compiler/sourcemap.ts | 4 ---- src/compiler/transformer.ts | 4 +++- src/compiler/transformers/es6.ts | 14 -------------- src/compiler/transformers/module/es6.ts | 8 ++++---- src/compiler/transformers/module/module.ts | 4 ++-- src/compiler/transformers/module/system.ts | 4 ---- src/compiler/transformers/ts.ts | 15 +-------------- 9 files changed, 19 insertions(+), 50 deletions(-) diff --git a/src/compiler/comments.ts b/src/compiler/comments.ts index 6f0cb94b7d7..8ee7878d1e6 100644 --- a/src/compiler/comments.ts +++ b/src/compiler/comments.ts @@ -296,7 +296,7 @@ namespace ts { }, emitTrailingComments(range: TextRange, comments: CommentRange[]): void { const commentStart = performance.mark(); - emitLeadingComments(range, comments); + emitTrailingComments(range, comments); performance.measure("commentTime", commentStart); }, emitLeadingDetachedComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean): void { diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 0516787130a..8427b1af0af 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -3,8 +3,6 @@ /* @internal */ namespace ts { - const synthesizedLocation: TextRange = { pos: -1, end: -1 }; - let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; let SourceFileConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; @@ -151,7 +149,8 @@ namespace ts { name.text = ""; name.originalKeywordKind = SyntaxKind.Unknown; name.autoGenerateKind = GeneratedIdentifierKind.Auto; - name.autoGenerateId = nextAutoGenerateId++; + name.autoGenerateId = nextAutoGenerateId; + nextAutoGenerateId++; if (recordTempVariable) { recordTempVariable(name); } @@ -163,7 +162,8 @@ namespace ts { name.text = ""; name.originalKeywordKind = SyntaxKind.Unknown; name.autoGenerateKind = GeneratedIdentifierKind.Loop; - name.autoGenerateId = nextAutoGenerateId++; + name.autoGenerateId = nextAutoGenerateId; + nextAutoGenerateId++; return name; } @@ -172,7 +172,8 @@ namespace ts { name.text = text; name.originalKeywordKind = SyntaxKind.Unknown; name.autoGenerateKind = GeneratedIdentifierKind.Unique; - name.autoGenerateId = nextAutoGenerateId++; + name.autoGenerateId = nextAutoGenerateId; + nextAutoGenerateId++; return name; } @@ -182,7 +183,8 @@ namespace ts { name.text = ""; name.originalKeywordKind = SyntaxKind.Unknown; name.autoGenerateKind = GeneratedIdentifierKind.Node; - name.autoGenerateId = nextAutoGenerateId++; + name.autoGenerateId = nextAutoGenerateId; + nextAutoGenerateId++; return name; } diff --git a/src/compiler/sourcemap.ts b/src/compiler/sourcemap.ts index 93676c1181d..123b658cd90 100644 --- a/src/compiler/sourcemap.ts +++ b/src/compiler/sourcemap.ts @@ -508,10 +508,6 @@ namespace ts { return skipTrivia(currentSourceText, rangeHasDecorators ? (range as Node).decorators.end : range.pos); } - function getStartPos(range: TextRange) { - return skipTrivia(currentSourceText, range.pos); - } - /** * Emits a mapping for the start of a range. * diff --git a/src/compiler/transformer.ts b/src/compiler/transformer.ts index a448893f345..6642c83007a 100644 --- a/src/compiler/transformer.ts +++ b/src/compiler/transformer.ts @@ -156,7 +156,9 @@ namespace ts { * @param transforms An array of Transformers. */ export function transformFiles(resolver: EmitResolver, host: EmitHost, sourceFiles: SourceFile[], transformers: Transformer[]) { - const transformId = nextTransformId++; + const transformId = nextTransformId; + nextTransformId++; + const tokenSourceMapRanges: Map = { }; const lexicalEnvironmentVariableDeclarationsStack: VariableDeclaration[][] = []; const lexicalEnvironmentFunctionDeclarationsStack: FunctionDeclaration[][] = []; diff --git a/src/compiler/transformers/es6.ts b/src/compiler/transformers/es6.ts index 9103b09f6bb..9c814affd50 100644 --- a/src/compiler/transformers/es6.ts +++ b/src/compiler/transformers/es6.ts @@ -2918,20 +2918,6 @@ namespace ts { return getDeclarationName(node, allowComments, allowSourceMaps, NodeEmitFlags.LocalName); } - /** - * Gets the export name for a declaration for use in expressions. - * - * An export name will *always* be prefixed with an module or namespace export modifier - * like "exports." if one is required. - * - * @param node The declaration. - * @param allowComments A value indicating whether comments may be emitted for the name. - * @param allowSourceMaps A value indicating whether source maps may be emitted for the name. - */ - function getExportName(node: ClassDeclaration | ClassExpression | FunctionDeclaration, allowComments?: boolean, allowSourceMaps?: boolean) { - return getDeclarationName(node, allowComments, allowSourceMaps, NodeEmitFlags.ExportName); - } - /** * Gets the name of a declaration, without source map or comments. * diff --git a/src/compiler/transformers/module/es6.ts b/src/compiler/transformers/module/es6.ts index 742f208d61c..5bf8a972aff 100644 --- a/src/compiler/transformers/module/es6.ts +++ b/src/compiler/transformers/module/es6.ts @@ -50,7 +50,7 @@ namespace ts { return undefined; // do not emit export equals for ES6 } const original = getOriginalNode(node); - return nodeIsSynthesized(original) || resolver.isValueAliasDeclaration(original) ? node: undefined; + return nodeIsSynthesized(original) || resolver.isValueAliasDeclaration(original) ? node : undefined; } function visitExportDeclaration(node: ExportDeclaration): ExportDeclaration { @@ -64,7 +64,7 @@ namespace ts { if (node.exportClause === newExportClause) { return node; } - return newExportClause + return newExportClause ? createExportDeclaration(newExportClause, node.moduleSpecifier) : undefined; } @@ -106,12 +106,12 @@ namespace ts { const newNamedBindings = visitNode(node.namedBindings, visitor, isNamedImportBindings, /*optional*/ true); return newDefaultImport !== node.name || newNamedBindings !== node.namedBindings ? createImportClause(newDefaultImport, newNamedBindings) - : node; + : node; } function visitNamedBindings(node: NamedImportBindings): VisitResult { if (node.kind === SyntaxKind.NamespaceImport) { - return resolver.isReferencedAliasDeclaration(node) ? node: undefined; + return resolver.isReferencedAliasDeclaration(node) ? node : undefined; } else { const newNamedImportElements = visitNodes((node).elements, visitor, isImportSpecifier); diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index 121365dd84c..625e90ea02e 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -619,7 +619,7 @@ namespace ts { if (hasModifier(node, ModifierFlags.Export)) { const variables = getInitializedVariables(node.declarationList); if (variables.length > 0) { - let inlineAssignments = createStatement( + const inlineAssignments = createStatement( inlineExpressions( map(variables, transformInitializedVariable) ), @@ -648,7 +648,7 @@ namespace ts { function addExportMemberAssignmentsForBindingName(resultStatements: Statement[], name: BindingName): void { if (isBindingPattern(name)) { for (const element of name.elements) { - addExportMemberAssignmentsForBindingName(resultStatements, element.name) + addExportMemberAssignmentsForBindingName(resultStatements, element.name); } } else { diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index 5519923333d..1f601ae4b55 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -12,10 +12,6 @@ namespace ts { const { getNodeEmitFlags, setNodeEmitFlags, - getCommentRange, - setCommentRange, - getSourceMapRange, - setSourceMapRange, startLexicalEnvironment, endLexicalEnvironment, hoistVariableDeclaration, diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 1b0cb290ace..d51e5c20b86 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2974,19 +2974,6 @@ namespace ts { } } - function getDeclarationNameExpression(node: DeclarationStatement) { - const name = getDeclarationName(node); - if (isNamespaceExport(node)) { - return getNamespaceMemberName(name); - } - else { - // We set the "ExportName" flag to indicate to any module transformer - // downstream that any `exports.` prefix should be added. - setNodeEmitFlags(name, getNodeEmitFlags(name) | NodeEmitFlags.ExportName); - return name; - } - } - function getClassPrototype(node: ClassExpression | ClassDeclaration) { return createPropertyAccess(getDeclarationName(node), "prototype"); } @@ -3090,7 +3077,7 @@ namespace ts { currentDecoratedClassAliases[getOriginalNodeId(node)] = decoratedClassAliases[getOriginalNodeId(node)]; } else if (node.kind === SyntaxKind.Identifier) { - const declaration = resolver.getReferencedValueDeclaration(node) + const declaration = resolver.getReferencedValueDeclaration(node); if (declaration && isClassWithDecorators(declaration)) { currentDecoratedClassAliases[getOriginalNodeId(declaration)] = decoratedClassAliases[getOriginalNodeId(declaration)]; }