Merge pull request #8929 from Microsoft/transforms-extractFromPrinter

[Transforms] Extract transformFiles call from printFile
This commit is contained in:
Ron Buckton committed 2016-06-03 11:52:52 -07:00
commit 877e3c4815
6 files changed
+2772 -2588

No files matched your search

+10 -10
View File
@@ -17162,7 +17162,7 @@ namespace ts {
function isArgumentsLocalBinding(node: Identifier): boolean {
if (!isGeneratedIdentifier(node)) {
node = getSourceTreeNodeOfType(node, isIdentifier);
node = getParseTreeNode(node, isIdentifier);
if (node) {
return getReferencedValueSymbol(node) === argumentsSymbol;
}
@@ -17208,7 +17208,7 @@ namespace ts {
// When resolved as an expression identifier, if the given node references an exported entity, return the declaration
// node of the exported entity's container. Otherwise, return undefined.
function getReferencedExportContainer(node: Identifier, prefixLocals?: boolean): SourceFile | ModuleDeclaration | EnumDeclaration {
node = getSourceTreeNodeOfType(node, isIdentifier);
node = getParseTreeNode(node, isIdentifier);
if (node) {
// When resolving the export container for the name of a module or enum
// declaration, we need to start resolution at the declaration's container.
@@ -17246,7 +17246,7 @@ namespace ts {
// When resolved as an expression identifier, if the given node references an import, return the declaration of
// that import. Otherwise, return undefined.
function getReferencedImportDeclaration(node: Identifier): Declaration {
node = getSourceTreeNodeOfType(node, isIdentifier);
node = getParseTreeNode(node, isIdentifier);
if (node) {
const symbol = getReferencedValueSymbol(node);
if (symbol && symbol.flags & SymbolFlags.Alias) {
@@ -17305,7 +17305,7 @@ namespace ts {
// return the declaration of that entity. Otherwise, return undefined.
function getReferencedDeclarationWithCollidingName(node: Identifier): Declaration {
if (!isGeneratedIdentifier(node)) {
node = getSourceTreeNodeOfType(node, isIdentifier);
node = getParseTreeNode(node, isIdentifier);
if (node) {
const symbol = getReferencedValueSymbol(node);
if (symbol && isSymbolOfDeclarationWithCollidingName(symbol)) {
@@ -17320,7 +17320,7 @@ namespace ts {
// Return true if the given node is a declaration of a nested block scoped entity with a name that either hides an
// existing name or might hide a name when compiled downlevel
function isDeclarationWithCollidingName(node: Declaration): boolean {
node = getSourceTreeNodeOfType(node, isDeclaration);
node = getParseTreeNode(node, isDeclaration);
if (node) {
const symbol = getSymbolOfNode(node);
if (symbol) {
@@ -17332,7 +17332,7 @@ namespace ts {
}
function isValueAliasDeclaration(node: Node): boolean {
node = getSourceTreeNode(node);
node = getParseTreeNode(node);
if (node === undefined) {
// A synthesized node comes from an emit transformation and is always a value.
return true;
@@ -17358,7 +17358,7 @@ namespace ts {
}
function isTopLevelValueImportEqualsWithEntityName(node: ImportEqualsDeclaration): boolean {
node = getSourceTreeNodeOfType(node, isImportEqualsDeclaration);
node = getParseTreeNode(node, isImportEqualsDeclaration);
if (node === undefined || node.parent.kind !== SyntaxKind.SourceFile || !isInternalModuleImportEqualsDeclaration(node)) {
// parent is not source file or it is not reference to internal module
return false;
@@ -17384,7 +17384,7 @@ namespace ts {
}
function isReferencedAliasDeclaration(node: Node, checkChildren?: boolean): boolean {
node = getSourceTreeNode(node);
node = getParseTreeNode(node);
if (isAliasSymbolDeclaration(node)) {
const symbol = getSymbolOfNode(node);
if (symbol && getSymbolLinks(symbol).referenced) {
@@ -17420,7 +17420,7 @@ namespace ts {
}
function getNodeCheckFlags(node: Node): NodeCheckFlags {
node = getSourceTreeNode(node);
node = getParseTreeNode(node);
return node ? getNodeLinks(node).flags : undefined;
}
@@ -17551,7 +17551,7 @@ namespace ts {
function getReferencedValueDeclaration(reference: Identifier): Declaration {
if (!isGeneratedIdentifier(reference)) {
reference = getSourceTreeNodeOfType(reference, isIdentifier);
reference = getParseTreeNode(reference, isIdentifier);
if (reference) {
const symbol = getReferencedValueSymbol(reference);
if (symbol) {
+20 -1
View File
@@ -1150,9 +1150,23 @@ namespace ts {
/** Performance measurements for the compiler. */
/*@internal*/
export namespace performance {
declare const onProfilerEvent: { (markName: string): void; profiler: boolean; };
let profilerEvent: (markName: string) => void;
let counters: Map<number>;
let measures: Map<number>;
/**
* Emit a performance event if ts-profiler is connected. This is primarily used
* to generate heap snapshots.
*
* @param eventName A name for the event.
*/
export function emit(eventName: string) {
if (profilerEvent) {
onProfilerEvent(eventName);
}
}
/**
* Increments a counter with the specified name.
*
@@ -1188,7 +1202,7 @@ namespace ts {
*/
export function measure(measureName: string, marker: number) {
if (measures) {
measures[measureName] = (getProperty(measures, measureName) || 0) + (mark() - marker);
measures[measureName] = (getProperty(measures, measureName) || 0) + (Date.now() - marker);
}
}
@@ -1215,12 +1229,17 @@ namespace ts {
commentTime: 0,
sourceMapTime: 0
};
profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent.profiler === true
? onProfilerEvent
: undefined;
}
/** Disables (and clears) performance measurements for the compiler. */
export function disable() {
counters = undefined;
measures = undefined;
profilerEvent = undefined;
}
}
}
+2505 -2512
View File
File diff suppressed because it is too large. Load diff
+92 -45
View File
@@ -23,7 +23,52 @@ namespace ts {
EmitNotifications = 1 << 1,
}
/* @internal */
export interface TransformationResult {
/**
* Gets the transformed source files.
*/
getSourceFiles(): SourceFile[];
/**
* Gets the TextRange to use for source maps for a token of a node.
*/
getTokenSourceMapRange(node: Node, token: SyntaxKind): TextRange;
/**
* Determines whether expression substitutions are enabled for the provided node.
*/
isSubstitutionEnabled(node: Node): boolean;
/**
* Determines whether before/after emit notifications should be raised in the pretty
* printer when it emits a node.
*/
isEmitNotificationEnabled(node: Node): boolean;
/**
* Hook used by transformers to substitute expressions just before they
* are emitted by the pretty printer.
*
* @param node The node to substitute.
* @param isExpression A value indicating whether the node is in an expression context.
*/
onSubstituteNode(node: Node, isExpression: boolean): Node;
/**
* Hook used to allow transformers to capture state before or after
* the printer emits a node.
*
* @param node The node to emit.
* @param emitCallback A callback used to emit the node.
*/
onEmitNode(node: Node, emitCallback: (node: Node) => void): void;
/**
* Reset transient transformation properties on parse tree nodes.
*/
dispose(): void;
}
export interface TransformationContext extends LexicalEnvironment {
getCompilerOptions(): CompilerOptions;
getEmitResolver(): EmitResolver;
@@ -155,7 +200,7 @@ namespace ts {
* @param sourceFiles An array of source files
* @param transforms An array of Transformers.
*/
export function transformFiles(resolver: EmitResolver, host: EmitHost, sourceFiles: SourceFile[], transformers: Transformer[]) {
export function transformFiles(resolver: EmitResolver, host: EmitHost, sourceFiles: SourceFile[], transformers: Transformer[]): TransformationResult {
const transformId = nextTransformId;
nextTransformId++;
@@ -163,7 +208,7 @@ namespace ts {
const lexicalEnvironmentVariableDeclarationsStack: VariableDeclaration[][] = [];
const lexicalEnvironmentFunctionDeclarationsStack: FunctionDeclaration[][] = [];
const enabledSyntaxKindFeatures = new Array<SyntaxKindFeatureFlags>(SyntaxKind.Count);
const sourceTreeNodesWithAnnotations: Node[] = [];
const parseTreeNodesWithAnnotations: Node[] = [];
let lastTokenSourceMapRangeNode: Node;
let lastTokenSourceMapRangeToken: SyntaxKind;
@@ -171,7 +216,6 @@ namespace ts {
let lexicalEnvironmentStackOffset = 0;
let hoistedVariableDeclarations: VariableDeclaration[];
let hoistedFunctionDeclarations: FunctionDeclaration[];
let currentSourceFile: SourceFile;
let lexicalEnvironmentDisabled: boolean;
// The transformation context is provided to each transformer as part of transformer
@@ -204,7 +248,37 @@ namespace ts {
const transformation = chain(...transformers)(context);
// Transform each source file.
return map(sourceFiles, transformSourceFile);
const transformed = map(sourceFiles, transformSourceFile);
// Disable modification of the lexical environment.
lexicalEnvironmentDisabled = true;
return {
getSourceFiles: () => transformed,
getTokenSourceMapRange,
isSubstitutionEnabled,
isEmitNotificationEnabled,
onSubstituteNode: context.onSubstituteNode,
onEmitNode: context.onEmitNode,
dispose() {
// During transformation we may need to annotate a parse tree node with transient
// transformation properties. As parse tree nodes live longer than transformation
// nodes, we need to make sure we reclaim any memory allocated for custom ranges
// from these nodes to ensure we do not hold onto entire subtrees just for position
// information. We also need to reset these nodes to a pre-transformation state
// for incremental parsing scenarios so that we do not impact later emit.
for (const node of parseTreeNodesWithAnnotations) {
if (node.transformId === transformId) {
node.transformId = 0;
node.emitFlags = 0;
node.commentRange = undefined;
node.sourceMapRange = undefined;
}
}
parseTreeNodesWithAnnotations.length = 0;
}
};
/**
* Transforms a source file.
@@ -216,21 +290,7 @@ namespace ts {
return sourceFile;
}
currentSourceFile = sourceFile;
sourceFile = transformation(sourceFile);
// Cleanup source tree nodes with annotations
for (const node of sourceTreeNodesWithAnnotations) {
if (node.transformId === transformId) {
node.transformId = 0;
node.emitFlags = 0;
node.commentRange = undefined;
node.sourceMapRange = undefined;
}
}
sourceTreeNodesWithAnnotations.length = 0;
return sourceFile;
return transformation(sourceFile);
}
/**
@@ -281,15 +341,6 @@ namespace ts {
* @param emit A callback used to emit the node in the printer.
*/
function onEmitNode(node: Node, emit: (node: Node) => void) {
// Ensure that lexical environment modifications are disabled during the print phase.
if (!lexicalEnvironmentDisabled) {
const savedLexicalEnvironmentDisabled = lexicalEnvironmentDisabled;
lexicalEnvironmentDisabled = true;
emit(node);
lexicalEnvironmentDisabled = savedLexicalEnvironmentDisabled;
return;
}
emit(node);
}
@@ -302,9 +353,9 @@ namespace ts {
function beforeSetAnnotation(node: Node) {
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
// parse tree node we are annotating. This allows us to clean them up after
// all transformations have completed.
sourceTreeNodesWithAnnotations.push(node);
parseTreeNodesWithAnnotations.push(node);
node.transformId = transformId;
}
}
@@ -520,7 +571,11 @@ namespace ts {
function chain<T, U>(...args: ((t: T) => (u: U) => U)[]): (t: T) => (u: U) => U;
function chain<T, U>(a: (t: T) => (u: U) => U, b: (t: T) => (u: U) => U, c: (t: T) => (u: U) => U, d: (t: T) => (u: U) => U, e: (t: T) => (u: U) => U): (t: T) => (u: U) => U {
if (e) {
const args = arrayOf<(t: T) => (u: U) => U>(arguments);
const args: ((t: T) => (u: U) => U)[] = [];
for (let i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
return t => compose(...map(args, f => f(t)));
}
else if (d) {
@@ -549,7 +604,11 @@ namespace ts {
function compose<T>(...args: ((t: T) => T)[]): (t: T) => T;
function compose<T>(a: (t: T) => T, b: (t: T) => T, c: (t: T) => T, d: (t: T) => T, e: (t: T) => T): (t: T) => T {
if (e) {
const args = arrayOf(arguments);
const args: ((t: T) => T)[] = [];
for (let i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
return t => reduceLeft<(t: T) => T, T>(args, (u, f) => f(u), t);
}
else if (d) {
@@ -568,16 +627,4 @@ namespace ts {
return t => t;
}
}
/**
* Makes an array from an ArrayLike.
*/
function arrayOf<T>(arrayLike: ArrayLike<T>) {
const length = arrayLike.length;
const array: T[] = new Array<T>(length);
for (let i = 0; i < length; i++) {
array[i] = arrayLike[i];
}
return array;
}
}
+5 -7
View File
@@ -1425,15 +1425,13 @@ namespace ts {
// If we are here it is most likely because our expression is a destructuring assignment.
switch (node.expression.kind) {
case SyntaxKind.ParenthesizedExpression:
return createStatement(
visitParenthesizedExpression(<ParenthesizedExpression>node.expression, /*needsDestructuringValue*/ false),
/*location*/ node
return updateStatement(node,
visitParenthesizedExpression(<ParenthesizedExpression>node.expression, /*needsDestructuringValue*/ false)
);
case SyntaxKind.BinaryExpression:
return createStatement(
visitBinaryExpression(<BinaryExpression>node.expression, /*needsDestructuringValue*/ false),
/*location*/ node
return updateStatement(node,
visitBinaryExpression(<BinaryExpression>node.expression, /*needsDestructuringValue*/ false)
);
}
@@ -2831,7 +2829,7 @@ namespace ts {
// Only substitute the identifier if we have enabled substitutions for block-scoped
// bindings.
if (enabledSubstitutions & ES6SubstitutionFlags.BlockScopedBindings) {
const original = getSourceTreeNodeOfType(node, isIdentifier);
const original = getParseTreeNode(node, isIdentifier);
if (original && isNameOfDeclarationWithCollidingName(original)) {
return getGeneratedNameForNode(original);
}
+140 -13
View File
@@ -300,8 +300,8 @@ namespace ts {
return getTokenPosOfNode(node.jsDocComments[0]);
}
// For a syntax list, it is possible that one of its children has JSDocComment nodes, while
// the syntax list itself considers them as normal trivia. Therefore if we simply skip
// For a syntax list, it is possible that one of its children has JSDocComment nodes, while
// the syntax list itself considers them as normal trivia. Therefore if we simply skip
// trivia for the list, we may have skipped the JSDocComment as well. So we should process its
// first child to determine the actual position of its first token.
if (node.kind === SyntaxKind.SyntaxList && (<SyntaxList>node)._children.length > 0) {
@@ -1926,27 +1926,55 @@ namespace ts {
return node;
}
export function isSourceTreeNode(node: Node): boolean {
/**
* Gets a value indicating whether a node originated in the parse tree.
*
* @param node The node to test.
*/
export function isParseTreeNode(node: Node): boolean {
return (node.flags & NodeFlags.Synthesized) === 0;
}
export function getSourceTreeNode(node: Node): Node {
if (isSourceTreeNode(node)) {
/**
* Gets the original parse tree node for a node.
*
* @param node The original node.
* @returns The original parse tree node if found; otherwise, undefined.
*/
export function getParseTreeNode(node: Node): Node;
/**
* Gets the original parse tree node for a node.
*
* @param node The original node.
* @param nodeTest A callback used to ensure the correct type of parse tree node is returned.
* @returns The original parse tree node if found; otherwise, undefined.
*/
export function getParseTreeNode<T extends Node>(node: Node, nodeTest?: (node: Node) => node is T): T;
export function getParseTreeNode(node: Node, nodeTest?: (node: Node) => boolean): Node {
if (isParseTreeNode(node)) {
return node;
}
node = getOriginalNode(node);
if (isSourceTreeNode(node)) {
if (isParseTreeNode(node) && (!nodeTest || nodeTest(node))) {
return node;
}
return undefined;
}
export function getSourceTreeNodeOfType<T extends Node>(node: T, nodeTest: (node: Node) => node is T): T {
const source = getSourceTreeNode(node);
return source && nodeTest(source) ? source : undefined;
export function getOriginalSourceFiles(sourceFiles: SourceFile[]) {
const originalSourceFiles: SourceFile[] = [];
for (const sourceFile of sourceFiles) {
const originalSourceFile = getParseTreeNode(sourceFile, isSourceFile);
if (originalSourceFile) {
originalSourceFiles.push(originalSourceFile);
}
}
return originalSourceFiles;
}
export function getOriginalNodeId(node: Node) {
@@ -2457,6 +2485,109 @@ namespace ts {
declarationFilePath: string;
}
/**
* Gets the source files that are expected to have an emit output.
*
* Originally part of `forEachExpectedEmitFile`, this functionality was extracted to support
* transformations.
*
* @param host An EmitHost.
* @param targetSourceFile An optional target source file to emit.
*/
export function getSourceFilesToEmit(host: EmitHost, targetSourceFile?: SourceFile) {
const options = host.getCompilerOptions();
if (options.outFile || options.out) {
const moduleKind = getEmitModuleKind(options);
const moduleEmitEnabled = moduleKind === ModuleKind.AMD || moduleKind === ModuleKind.System;
const sourceFiles = host.getSourceFiles();
// Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified
return filter(sourceFiles, moduleEmitEnabled ? isNonDeclarationFile : isBundleEmitNonExternalModule);
}
else {
const sourceFiles = targetSourceFile === undefined ? host.getSourceFiles() : [targetSourceFile];
return filter(sourceFiles, isNonDeclarationFile);
}
}
function isNonDeclarationFile(sourceFile: SourceFile) {
return !isDeclarationFile(sourceFile);
}
function isBundleEmitNonExternalModule(sourceFile: SourceFile) {
return !isDeclarationFile(sourceFile) && !isExternalModule(sourceFile);
}
/**
* Iterates over each source file to emit. The source files are expected to have been
* transformed for use by the pretty printer.
*
* Originally part of `forEachExpectedEmitFile`, this functionality was extracted to support
* transformations.
*
* @param host An EmitHost.
* @param sourceFiles The transformed source files to emit.
* @param action The action to execute.
*/
export function forEachTransformedEmitFile(host: EmitHost, sourceFiles: SourceFile[],
action: (jsFilePath: string, sourceMapFilePath: string, declarationFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean) => void) {
const options = host.getCompilerOptions();
// Emit on each source file
if (options.outFile || options.out) {
onBundledEmit(host, sourceFiles);
}
else {
for (const sourceFile of sourceFiles) {
if (!isDeclarationFile(sourceFile)) {
onSingleFileEmit(host, sourceFile);
}
}
}
function onSingleFileEmit(host: EmitHost, sourceFile: SourceFile) {
// JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
// So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
// For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
let extension = ".js";
if (options.jsx === JsxEmit.Preserve) {
if (isSourceFileJavaScript(sourceFile)) {
if (fileExtensionIs(sourceFile.fileName, ".jsx")) {
extension = ".jsx";
}
}
else if (sourceFile.languageVariant === LanguageVariant.JSX) {
// TypeScript source file preserving JSX syntax
extension = ".jsx";
}
}
const jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension);
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
const declarationFilePath = !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined;
action(jsFilePath, sourceMapFilePath, declarationFilePath, [sourceFile], /*isBundledEmit*/ false);
}
function onBundledEmit(host: EmitHost, sourceFiles: SourceFile[]) {
if (sourceFiles.length) {
const jsFilePath = options.outFile || options.out;
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
const declarationFilePath = options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : undefined;
action(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, /*isBundledEmit*/ true);
}
}
}
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
return options.sourceMap ? jsFilePath + ".map" : undefined;
}
/**
* Iterates over the source files that are expected to have an emit output. This function
* is used by the legacy emitter and the declaration emitter and should not be used by
* the tree transforming emitter.
*
* @param host An EmitHost.
* @param action The action to execute.
* @param targetSourceFile An optional target source file to emit.
*/
export function forEachExpectedEmitFile(host: EmitHost,
action: (emitFileNames: EmitFileNames, sourceFiles: SourceFile[], isBundledEmit: boolean) => void,
targetSourceFile?: SourceFile) {
@@ -2517,10 +2648,6 @@ namespace ts {
action(emitFileNames, bundledSources, /*isBundledEmit*/true);
}
}
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
return options.sourceMap ? jsFilePath + ".map" : undefined;
}
}
export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) {