From 0d2b1c47f8eb6fd09257395332f243cf733b340b Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 27 May 2016 14:55:11 -0700 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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)]; }