Skip to content

Commit 334b224

Browse files
committed
Moving logic around in getReferencedImportDeclaration
1 parent 1e39130 commit 334b224

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11809,16 +11809,11 @@ module ts {
1180911809
}
1181011810
}
1181111811

11812-
// When resolved as an expression identifier, if the given node references a default import or a named import, return
11813-
// the declaration node of that import. Otherwise, return undefined.
11814-
function getReferencedImportDeclaration(node: Identifier): ImportClause | ImportSpecifier {
11812+
// When resolved as an expression identifier, if the given node references an import, return the declaration of
11813+
// that import. Otherwise, return undefined.
11814+
function getReferencedImportDeclaration(node: Identifier): Declaration {
1181511815
let symbol = getReferencedValueSymbol(node);
11816-
if (symbol && symbol.flags & SymbolFlags.Alias) {
11817-
let declaration = getDeclarationOfAliasSymbol(symbol);
11818-
if (declaration.kind === SyntaxKind.ImportClause || declaration.kind === SyntaxKind.ImportSpecifier) {
11819-
return <ImportClause | ImportSpecifier>declaration;
11820-
}
11821-
}
11816+
return symbol && symbol.flags & SymbolFlags.Alias ? getDeclarationOfAliasSymbol(symbol) : undefined;
1182211817
}
1182311818

1182411819
function isStatementWithLocals(node: Node) {
@@ -11833,24 +11828,30 @@ module ts {
1183311828
return false;
1183411829
}
1183511830

11836-
function getIsNestedRedeclaration(symbol: Symbol): boolean {
11837-
let links = getSymbolLinks(symbol);
11838-
if (links.isNestedRedeclaration === undefined) {
11839-
let container = getEnclosingBlockScopeContainer(symbol.valueDeclaration);
11840-
links.isNestedRedeclaration = isStatementWithLocals(container) &&
11841-
!!resolveName(container.parent, symbol.name, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
11831+
function isNestedRedeclarationSymbol(symbol: Symbol): boolean {
11832+
if (symbol.flags & SymbolFlags.BlockScoped) {
11833+
let links = getSymbolLinks(symbol);
11834+
if (links.isNestedRedeclaration === undefined) {
11835+
let container = getEnclosingBlockScopeContainer(symbol.valueDeclaration);
11836+
links.isNestedRedeclaration = isStatementWithLocals(container) &&
11837+
!!resolveName(container.parent, symbol.name, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
11838+
}
11839+
return links.isNestedRedeclaration;
1184211840
}
11843-
return links.isNestedRedeclaration;
11841+
return false;
1184411842
}
1184511843

11844+
// When resolved as an expression identifier, if the given node references a nested block scoped entity with
11845+
// a name that hides an existing name, return the declaration of that entity. Otherwise, return undefined.
1184611846
function getReferencedNestedRedeclaration(node: Identifier): Declaration {
1184711847
let symbol = getReferencedValueSymbol(node);
11848-
return symbol && symbol.flags & SymbolFlags.BlockScoped && getIsNestedRedeclaration(symbol) ? symbol.valueDeclaration : undefined;
11848+
return symbol && isNestedRedeclarationSymbol(symbol) ? symbol.valueDeclaration : undefined;
1184911849
}
1185011850

11851+
// Return true if the given node is a declaration of a nested block scoped entity with a name that hides an
11852+
// existing name.
1185111853
function isNestedRedeclaration(node: Declaration): boolean {
11852-
let symbol = getSymbolOfNode(node);
11853-
return symbol.flags & SymbolFlags.BlockScoped && getIsNestedRedeclaration(symbol);
11854+
return isNestedRedeclarationSymbol(getSymbolOfNode(node));
1185411855
}
1185511856

1185611857
function isValueAliasDeclaration(node: Node): boolean {

src/compiler/emitter.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,24 +1237,25 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12371237
}
12381238
}
12391239
else if (languageVersion < ScriptTarget.ES6) {
1240-
let alias = resolver.getReferencedImportDeclaration(node);
1241-
if (alias) {
1242-
if (alias.kind === SyntaxKind.ImportClause) {
1240+
let declaration = resolver.getReferencedImportDeclaration(node);
1241+
if (declaration) {
1242+
if (declaration.kind === SyntaxKind.ImportClause) {
12431243
// Identifier references default import
1244-
write(getGeneratedNameForNode(<ImportDeclaration>alias.parent));
1244+
write(getGeneratedNameForNode(<ImportDeclaration>declaration.parent));
12451245
write(languageVersion === ScriptTarget.ES3 ? '["default"]' : ".default");
1246+
return;
12461247
}
1247-
else {
1248+
else if (declaration.kind === SyntaxKind.ImportSpecifier) {
12481249
// Identifier references named import
1249-
write(getGeneratedNameForNode(<ImportDeclaration>alias.parent.parent.parent));
1250+
write(getGeneratedNameForNode(<ImportDeclaration>declaration.parent.parent.parent));
12501251
write(".");
1251-
writeTextOfNode(currentSourceFile, (<ImportSpecifier>alias).propertyName || (<ImportSpecifier>alias).name);
1252+
writeTextOfNode(currentSourceFile, (<ImportSpecifier>declaration).propertyName || (<ImportSpecifier>declaration).name);
1253+
return;
12521254
}
1253-
return;
12541255
}
1255-
let local = resolver.getReferencedNestedRedeclaration(node);
1256-
if (local) {
1257-
write(getGeneratedNameForNode(local.name));
1256+
declaration = resolver.getReferencedNestedRedeclaration(node);
1257+
if (declaration) {
1258+
write(getGeneratedNameForNode(declaration.name));
12581259
return;
12591260
}
12601261
}

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ module ts {
13971397
export interface EmitResolver {
13981398
hasGlobalName(name: string): boolean;
13991399
getReferencedExportContainer(node: Identifier): SourceFile | ModuleDeclaration | EnumDeclaration;
1400-
getReferencedImportDeclaration(node: Identifier): ImportClause | ImportSpecifier;
1400+
getReferencedImportDeclaration(node: Identifier): Declaration;
14011401
getReferencedNestedRedeclaration(node: Identifier): Declaration;
14021402
isNestedRedeclaration(node: Declaration): boolean;
14031403
isValueAliasDeclaration(node: Node): boolean;

0 commit comments

Comments
 (0)