@@ -5,6 +5,12 @@ module ts {
5
5
let nextNodeId = 1;
6
6
let nextMergeId = 1;
7
7
8
+ // @internal
9
+ export function getNodeId(node: Node): number {
10
+ if (!node.id) node.id = nextNodeId++;
11
+ return node.id;
12
+ }
13
+
8
14
/* @internal */ export let checkTime = 0;
9
15
10
16
export function createTypeChecker(host: TypeCheckerHost, produceDiagnostics: boolean): TypeChecker {
@@ -255,8 +261,8 @@ module ts {
255
261
}
256
262
257
263
function getNodeLinks(node: Node): NodeLinks {
258
- if (!node.id) node.id = nextNodeId++ ;
259
- return nodeLinks[node.id ] || (nodeLinks[node.id ] = {});
264
+ let nodeId = getNodeId(node) ;
265
+ return nodeLinks[nodeId ] || (nodeLinks[nodeId ] = {});
260
266
}
261
267
262
268
function getSourceFile(node: Node): SourceFile {
@@ -10954,126 +10960,7 @@ module ts {
10954
10960
return symbol.flags & SymbolFlags.ValueModule && symbol.declarations.length === 1 && symbol.declarations[0].kind === SyntaxKind.SourceFile;
10955
10961
}
10956
10962
10957
- function isNodeDescendentOf(node: Node, ancestor: Node): boolean {
10958
- while (node) {
10959
- if (node === ancestor) return true;
10960
- node = node.parent;
10961
- }
10962
- return false;
10963
- }
10964
-
10965
- function isUniqueLocalName(name: string, container: Node): boolean {
10966
- for (let node = container; isNodeDescendentOf(node, container); node = node.nextContainer) {
10967
- if (node.locals && hasProperty(node.locals, name)) {
10968
- // We conservatively include alias symbols to cover cases where they're emitted as locals
10969
- if (node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) {
10970
- return false;
10971
- }
10972
- }
10973
- }
10974
- return true;
10975
- }
10976
-
10977
- function getGeneratedNamesForSourceFile(sourceFile: SourceFile): Map<string> {
10978
- let links = getNodeLinks(sourceFile);
10979
- let generatedNames = links.generatedNames;
10980
- if (!generatedNames) {
10981
- generatedNames = links.generatedNames = {};
10982
- generateNames(sourceFile);
10983
- }
10984
- return generatedNames;
10985
-
10986
- function generateNames(node: Node) {
10987
- switch (node.kind) {
10988
- case SyntaxKind.FunctionDeclaration:
10989
- case SyntaxKind.ClassDeclaration:
10990
- generateNameForFunctionOrClassDeclaration(<Declaration>node);
10991
- break;
10992
- case SyntaxKind.ModuleDeclaration:
10993
- generateNameForModuleOrEnum(<ModuleDeclaration>node);
10994
- generateNames((<ModuleDeclaration>node).body);
10995
- break;
10996
- case SyntaxKind.EnumDeclaration:
10997
- generateNameForModuleOrEnum(<EnumDeclaration>node);
10998
- break;
10999
- case SyntaxKind.ImportDeclaration:
11000
- generateNameForImportDeclaration(<ImportDeclaration>node);
11001
- break;
11002
- case SyntaxKind.ExportDeclaration:
11003
- generateNameForExportDeclaration(<ExportDeclaration>node);
11004
- break;
11005
- case SyntaxKind.ExportAssignment:
11006
- generateNameForExportAssignment(<ExportAssignment>node);
11007
- break;
11008
- case SyntaxKind.SourceFile:
11009
- case SyntaxKind.ModuleBlock:
11010
- forEach((<SourceFile | ModuleBlock>node).statements, generateNames);
11011
- break;
11012
- }
11013
- }
11014
-
11015
- function isExistingName(name: string) {
11016
- return hasProperty(globals, name) || hasProperty(sourceFile.identifiers, name) || hasProperty(generatedNames, name);
11017
- }
11018
-
11019
- function makeUniqueName(baseName: string): string {
11020
- let name = generateUniqueName(baseName, isExistingName);
11021
- return generatedNames[name] = name;
11022
- }
11023
-
11024
- function assignGeneratedName(node: Node, name: string) {
11025
- getNodeLinks(node).generatedName = unescapeIdentifier(name);
11026
- }
11027
-
11028
- function generateNameForFunctionOrClassDeclaration(node: Declaration) {
11029
- if (!node.name) {
11030
- assignGeneratedName(node, makeUniqueName("default"));
11031
- }
11032
- }
11033
-
11034
- function generateNameForModuleOrEnum(node: ModuleDeclaration | EnumDeclaration) {
11035
- if (node.name.kind === SyntaxKind.Identifier) {
11036
- let name = node.name.text;
11037
- // Use module/enum name itself if it is unique, otherwise make a unique variation
11038
- assignGeneratedName(node, isUniqueLocalName(name, node) ? name : makeUniqueName(name));
11039
- }
11040
- }
11041
-
11042
- function generateNameForImportOrExportDeclaration(node: ImportDeclaration | ExportDeclaration) {
11043
- let expr = getExternalModuleName(node);
11044
- let baseName = expr.kind === SyntaxKind.StringLiteral ?
11045
- escapeIdentifier(makeIdentifierFromModuleName((<LiteralExpression>expr).text)) : "module";
11046
- assignGeneratedName(node, makeUniqueName(baseName));
11047
- }
11048
-
11049
- function generateNameForImportDeclaration(node: ImportDeclaration) {
11050
- if (node.importClause && node.importClause.namedBindings && node.importClause.namedBindings.kind === SyntaxKind.NamedImports) {
11051
- generateNameForImportOrExportDeclaration(node);
11052
- }
11053
- }
11054
-
11055
- function generateNameForExportDeclaration(node: ExportDeclaration) {
11056
- if (node.moduleSpecifier) {
11057
- generateNameForImportOrExportDeclaration(node);
11058
- }
11059
- }
11060
-
11061
- function generateNameForExportAssignment(node: ExportAssignment) {
11062
- if (node.expression && node.expression.kind !== SyntaxKind.Identifier) {
11063
- assignGeneratedName(node, makeUniqueName("default"));
11064
- }
11065
- }
11066
- }
11067
-
11068
- function getGeneratedNameForNode(node: Node) {
11069
- let links = getNodeLinks(node);
11070
- if (!links.generatedName) {
11071
- getGeneratedNamesForSourceFile(getSourceFile(node));
11072
- }
11073
- return links.generatedName;
11074
- }
11075
-
11076
- function getAliasNameSubstitution(symbol: Symbol): string {
10963
+ function getAliasNameSubstitution(symbol: Symbol, getGeneratedNameForNode: (Node: Node) => string): string {
11077
10964
let declaration = getDeclarationOfAliasSymbol(symbol);
11078
10965
if (declaration && declaration.kind === SyntaxKind.ImportSpecifier) {
11079
10966
let moduleName = getGeneratedNameForNode(<ImportDeclaration>declaration.parent.parent.parent);
@@ -11082,7 +10969,7 @@ module ts {
11082
10969
}
11083
10970
}
11084
10971
11085
- function getExportNameSubstitution(symbol: Symbol, location: Node): string {
10972
+ function getExportNameSubstitution(symbol: Symbol, location: Node, getGeneratedNameForNode: (Node: Node) => string ): string {
11086
10973
if (isExternalModuleSymbol(symbol.parent)) {
11087
10974
var symbolName = unescapeIdentifier(symbol.name);
11088
10975
// If this is es6 or higher, just use the name of the export
@@ -11104,24 +10991,24 @@ module ts {
11104
10991
}
11105
10992
}
11106
10993
11107
- function getExpressionNameSubstitution(node: Identifier): string {
10994
+ function getExpressionNameSubstitution(node: Identifier, getGeneratedNameForNode: (Node: Node) => string ): string {
11108
10995
let symbol = getNodeLinks(node).resolvedSymbol;
11109
10996
if (symbol) {
11110
10997
// Whan an identifier resolves to a parented symbol, it references an exported entity from
11111
10998
// another declaration of the same internal module.
11112
10999
if (symbol.parent) {
11113
- return getExportNameSubstitution(symbol, node.parent);
11000
+ return getExportNameSubstitution(symbol, node.parent, getGeneratedNameForNode );
11114
11001
}
11115
11002
// If we reference an exported entity within the same module declaration, then whether
11116
11003
// we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the
11117
11004
// kinds that we do NOT prefix.
11118
11005
let exportSymbol = getExportSymbolOfValueSymbolIfExported(symbol);
11119
11006
if (symbol !== exportSymbol && !(exportSymbol.flags & SymbolFlags.ExportHasLocal)) {
11120
- return getExportNameSubstitution(exportSymbol, node.parent);
11007
+ return getExportNameSubstitution(exportSymbol, node.parent, getGeneratedNameForNode );
11121
11008
}
11122
11009
// Named imports from ES6 import declarations are rewritten
11123
11010
if (symbol.flags & SymbolFlags.Alias && languageVersion < ScriptTarget.ES6) {
11124
- return getAliasNameSubstitution(symbol);
11011
+ return getAliasNameSubstitution(symbol, getGeneratedNameForNode );
11125
11012
}
11126
11013
}
11127
11014
}
@@ -11224,10 +11111,13 @@ module ts {
11224
11111
getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
11225
11112
}
11226
11113
11114
+ function hasGlobalName(name: string): boolean {
11115
+ return hasProperty(globals, name);
11116
+ }
11117
+
11227
11118
function isUnknownIdentifier(location: Node, name: string): boolean {
11228
11119
Debug.assert(!nodeIsSynthesized(location), "isUnknownIdentifier called with a synthesized location");
11229
- return !resolveName(location, name, SymbolFlags.Value, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined) &&
11230
- !hasProperty(getGeneratedNamesForSourceFile(getSourceFile(location)), name);
11120
+ return !resolveName(location, name, SymbolFlags.Value, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined);
11231
11121
}
11232
11122
11233
11123
function getBlockScopedVariableId(n: Identifier): number {
@@ -11257,8 +11147,8 @@ module ts {
11257
11147
11258
11148
function createResolver(): EmitResolver {
11259
11149
return {
11260
- getGeneratedNameForNode,
11261
11150
getExpressionNameSubstitution,
11151
+ hasGlobalName,
11262
11152
hasExportDefaultValue,
11263
11153
isReferencedAliasDeclaration,
11264
11154
getNodeCheckFlags,
0 commit comments