@@ -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
/* @internal */
@@ -264,8 +270,8 @@ module ts {
264
270
}
265
271
266
272
function getNodeLinks(node: Node): NodeLinks {
267
- if (!node.id) node.id = nextNodeId++ ;
268
- return nodeLinks[node.id ] || (nodeLinks[node.id ] = {});
273
+ let nodeId = getNodeId(node) ;
274
+ return nodeLinks[nodeId ] || (nodeLinks[nodeId ] = {});
269
275
}
270
276
271
277
function getSourceFile(node: Node): SourceFile {
@@ -10963,134 +10969,7 @@ module ts {
10963
10969
return symbol.flags & SymbolFlags.ValueModule && symbol.declarations.length === 1 && symbol.declarations[0].kind === SyntaxKind.SourceFile;
10964
10970
}
10965
10971
10966
- function isNodeDescendentOf(node: Node, ancestor: Node): boolean {
10967
- while (node) {
10968
- if (node === ancestor) return true;
10969
- node = node.parent;
10970
- }
10971
- return false;
10972
- }
10973
-
10974
- function isUniqueLocalName(name: string, container: Node): boolean {
10975
- for (let node = container; isNodeDescendentOf(node, container); node = node.nextContainer) {
10976
- if (node.locals && hasProperty(node.locals, name)) {
10977
- // We conservatively include alias symbols to cover cases where they're emitted as locals
10978
- if (node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) {
10979
- return false;
10980
- }
10981
- }
10982
- }
10983
- return true;
10984
- }
10985
-
10986
- function getGeneratedNamesForSourceFile(sourceFile: SourceFile): Map<string> {
10987
- let links = getNodeLinks(sourceFile);
10988
- let generatedNames = links.generatedNames;
10989
- if (!generatedNames) {
10990
- generatedNames = links.generatedNames = {};
10991
- generateNames(sourceFile);
10992
- }
10993
- return generatedNames;
10994
-
10995
- function generateNames(node: Node) {
10996
- switch (node.kind) {
10997
- case SyntaxKind.FunctionDeclaration:
10998
- case SyntaxKind.ClassDeclaration:
10999
- generateNameForFunctionOrClassDeclaration(<Declaration>node);
11000
- break;
11001
- case SyntaxKind.ModuleDeclaration:
11002
- generateNameForModuleOrEnum(<ModuleDeclaration>node);
11003
- generateNames((<ModuleDeclaration>node).body);
11004
- break;
11005
- case SyntaxKind.EnumDeclaration:
11006
- generateNameForModuleOrEnum(<EnumDeclaration>node);
11007
- break;
11008
- case SyntaxKind.ImportDeclaration:
11009
- generateNameForImportDeclaration(<ImportDeclaration>node);
11010
- break;
11011
- case SyntaxKind.ExportDeclaration:
11012
- generateNameForExportDeclaration(<ExportDeclaration>node);
11013
- break;
11014
- case SyntaxKind.ExportAssignment:
11015
- generateNameForExportAssignment(<ExportAssignment>node);
11016
- break;
11017
- case SyntaxKind.SourceFile:
11018
- case SyntaxKind.ModuleBlock:
11019
- forEach((<SourceFile | ModuleBlock>node).statements, generateNames);
11020
- break;
11021
- }
11022
- }
11023
-
11024
- function isExistingName(name: string) {
11025
- return hasProperty(globals, name) || hasProperty(sourceFile.identifiers, name) || hasProperty(generatedNames, name);
11026
- }
11027
-
11028
- function makeUniqueName(baseName: string): string {
11029
- let name = generateUniqueName(baseName, isExistingName);
11030
- return generatedNames[name] = name;
11031
- }
11032
-
11033
- function assignGeneratedName(node: Node, name: string) {
11034
- getNodeLinks(node).generatedName = unescapeIdentifier(name);
11035
- }
11036
-
11037
- function generateNameForFunctionOrClassDeclaration(node: Declaration) {
11038
- if (!node.name) {
11039
- assignGeneratedName(node, makeUniqueName("default"));
11040
- }
11041
- }
11042
-
11043
- function generateNameForModuleOrEnum(node: ModuleDeclaration | EnumDeclaration) {
11044
- if (node.name.kind === SyntaxKind.Identifier) {
11045
- let name = node.name.text;
11046
- // Use module/enum name itself if it is unique, otherwise make a unique variation
11047
- assignGeneratedName(node, isUniqueLocalName(name, node) ? name : makeUniqueName(name));
11048
- }
11049
- }
11050
-
11051
- function generateNameForImportOrExportDeclaration(node: ImportDeclaration | ExportDeclaration) {
11052
- let expr = getExternalModuleName(node);
11053
- let baseName = expr.kind === SyntaxKind.StringLiteral ?
11054
- escapeIdentifier(makeIdentifierFromModuleName((<LiteralExpression>expr).text)) : "module";
11055
- assignGeneratedName(node, makeUniqueName(baseName));
11056
- }
11057
-
11058
- function generateNameForImportDeclaration(node: ImportDeclaration) {
11059
- if (node.importClause && node.importClause.namedBindings && node.importClause.namedBindings.kind === SyntaxKind.NamedImports) {
11060
- generateNameForImportOrExportDeclaration(node);
11061
- }
11062
- }
11063
-
11064
- function generateNameForExportDeclaration(node: ExportDeclaration) {
11065
- if (node.moduleSpecifier) {
11066
- generateNameForImportOrExportDeclaration(node);
11067
- }
11068
- }
11069
-
11070
- function generateNameForExportAssignment(node: ExportAssignment) {
11071
- if (node.expression && node.expression.kind !== SyntaxKind.Identifier) {
11072
- assignGeneratedName(node, makeUniqueName("default"));
11073
- }
11074
- }
11075
- }
11076
-
11077
- function getGeneratedNameForNode(node: Node) {
11078
- let links = getNodeLinks(node);
11079
- if (!links.generatedName) {
11080
- getGeneratedNamesForSourceFile(getSourceFile(node));
11081
- }
11082
- return links.generatedName;
11083
- }
11084
-
11085
- function getLocalNameOfContainer(container: ModuleDeclaration | EnumDeclaration): string {
11086
- return getGeneratedNameForNode(container);
11087
- }
11088
-
11089
- function getLocalNameForImportDeclaration(node: ImportDeclaration): string {
11090
- return getGeneratedNameForNode(node);
11091
- }
11092
-
11093
- function getAliasNameSubstitution(symbol: Symbol): string {
10972
+ function getAliasNameSubstitution(symbol: Symbol, getGeneratedNameForNode: (Node: Node) => string): string {
11094
10973
let declaration = getDeclarationOfAliasSymbol(symbol);
11095
10974
if (declaration && declaration.kind === SyntaxKind.ImportSpecifier) {
11096
10975
let moduleName = getGeneratedNameForNode(<ImportDeclaration>declaration.parent.parent.parent);
@@ -11099,7 +10978,7 @@ module ts {
11099
10978
}
11100
10979
}
11101
10980
11102
- function getExportNameSubstitution(symbol: Symbol, location: Node): string {
10981
+ function getExportNameSubstitution(symbol: Symbol, location: Node, getGeneratedNameForNode: (Node: Node) => string ): string {
11103
10982
if (isExternalModuleSymbol(symbol.parent)) {
11104
10983
var symbolName = unescapeIdentifier(symbol.name);
11105
10984
// If this is es6 or higher, just use the name of the export
@@ -11121,24 +11000,24 @@ module ts {
11121
11000
}
11122
11001
}
11123
11002
11124
- function getExpressionNameSubstitution(node: Identifier): string {
11003
+ function getExpressionNameSubstitution(node: Identifier, getGeneratedNameForNode: (Node: Node) => string ): string {
11125
11004
let symbol = getNodeLinks(node).resolvedSymbol;
11126
11005
if (symbol) {
11127
11006
// Whan an identifier resolves to a parented symbol, it references an exported entity from
11128
11007
// another declaration of the same internal module.
11129
11008
if (symbol.parent) {
11130
- return getExportNameSubstitution(symbol, node.parent);
11009
+ return getExportNameSubstitution(symbol, node.parent, getGeneratedNameForNode );
11131
11010
}
11132
11011
// If we reference an exported entity within the same module declaration, then whether
11133
11012
// we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the
11134
11013
// kinds that we do NOT prefix.
11135
11014
let exportSymbol = getExportSymbolOfValueSymbolIfExported(symbol);
11136
11015
if (symbol !== exportSymbol && !(exportSymbol.flags & SymbolFlags.ExportHasLocal)) {
11137
- return getExportNameSubstitution(exportSymbol, node.parent);
11016
+ return getExportNameSubstitution(exportSymbol, node.parent, getGeneratedNameForNode );
11138
11017
}
11139
11018
// Named imports from ES6 import declarations are rewritten
11140
11019
if (symbol.flags & SymbolFlags.Alias && languageVersion < ScriptTarget.ES6) {
11141
- return getAliasNameSubstitution(symbol);
11020
+ return getAliasNameSubstitution(symbol, getGeneratedNameForNode );
11142
11021
}
11143
11022
}
11144
11023
}
@@ -11241,10 +11120,13 @@ module ts {
11241
11120
getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
11242
11121
}
11243
11122
11244
- function isUnknownIdentifier(location: Node, name: string): boolean {
11245
- Debug.assert(!nodeIsSynthesized(location), "isUnknownIdentifier called with a synthesized location");
11246
- return !resolveName(location, name, SymbolFlags.Value, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined) &&
11247
- !hasProperty(getGeneratedNamesForSourceFile(getSourceFile(location)), name);
11123
+ function hasGlobalName(name: string): boolean {
11124
+ return hasProperty(globals, name);
11125
+ }
11126
+
11127
+ function resolvesToSomeValue(location: Node, name: string): boolean {
11128
+ Debug.assert(!nodeIsSynthesized(location), "resolvesToSomeValue called with a synthesized location");
11129
+ return !!resolveName(location, name, SymbolFlags.Value, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined);
11248
11130
}
11249
11131
11250
11132
function getBlockScopedVariableId(n: Identifier): number {
@@ -11274,8 +11156,8 @@ module ts {
11274
11156
11275
11157
function createResolver(): EmitResolver {
11276
11158
return {
11277
- getGeneratedNameForNode,
11278
11159
getExpressionNameSubstitution,
11160
+ hasGlobalName,
11279
11161
hasExportDefaultValue,
11280
11162
isReferencedAliasDeclaration,
11281
11163
getNodeCheckFlags,
@@ -11288,7 +11170,7 @@ module ts {
11288
11170
isSymbolAccessible,
11289
11171
isEntityNameVisible,
11290
11172
getConstantValue,
11291
- isUnknownIdentifier ,
11173
+ resolvesToSomeValue ,
11292
11174
collectLinkedAliases,
11293
11175
getBlockScopedVariableId,
11294
11176
};
0 commit comments