From 334b22452a038725bad262c64fedea725eeefc41 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 3 Jun 2015 10:59:14 -0700 Subject: [PATCH] Moving logic around in getReferencedImportDeclaration --- src/compiler/checker.ts | 39 ++++++++++++++++++++------------------- src/compiler/emitter.ts | 23 ++++++++++++----------- src/compiler/types.ts | 2 +- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 78dbe9077c6..85bb37c135e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11809,16 +11809,11 @@ module ts { } } - // When resolved as an expression identifier, if the given node references a default import or a named import, return - // the declaration node of that import. Otherwise, return undefined. - function getReferencedImportDeclaration(node: Identifier): ImportClause | ImportSpecifier { + // 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 { let symbol = getReferencedValueSymbol(node); - if (symbol && symbol.flags & SymbolFlags.Alias) { - let declaration = getDeclarationOfAliasSymbol(symbol); - if (declaration.kind === SyntaxKind.ImportClause || declaration.kind === SyntaxKind.ImportSpecifier) { - return declaration; - } - } + return symbol && symbol.flags & SymbolFlags.Alias ? getDeclarationOfAliasSymbol(symbol) : undefined; } function isStatementWithLocals(node: Node) { @@ -11833,24 +11828,30 @@ module ts { return false; } - function getIsNestedRedeclaration(symbol: Symbol): boolean { - let links = getSymbolLinks(symbol); - if (links.isNestedRedeclaration === undefined) { - let container = getEnclosingBlockScopeContainer(symbol.valueDeclaration); - links.isNestedRedeclaration = isStatementWithLocals(container) && - !!resolveName(container.parent, symbol.name, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + function isNestedRedeclarationSymbol(symbol: Symbol): boolean { + if (symbol.flags & SymbolFlags.BlockScoped) { + let links = getSymbolLinks(symbol); + if (links.isNestedRedeclaration === undefined) { + let container = getEnclosingBlockScopeContainer(symbol.valueDeclaration); + links.isNestedRedeclaration = isStatementWithLocals(container) && + !!resolveName(container.parent, symbol.name, SymbolFlags.Value, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + } + return links.isNestedRedeclaration; } - return links.isNestedRedeclaration; + return false; } + // When resolved as an expression identifier, if the given node references a nested block scoped entity with + // a name that hides an existing name, return the declaration of that entity. Otherwise, return undefined. function getReferencedNestedRedeclaration(node: Identifier): Declaration { let symbol = getReferencedValueSymbol(node); - return symbol && symbol.flags & SymbolFlags.BlockScoped && getIsNestedRedeclaration(symbol) ? symbol.valueDeclaration : undefined; + return symbol && isNestedRedeclarationSymbol(symbol) ? symbol.valueDeclaration : undefined; } + // Return true if the given node is a declaration of a nested block scoped entity with a name that hides an + // existing name. function isNestedRedeclaration(node: Declaration): boolean { - let symbol = getSymbolOfNode(node); - return symbol.flags & SymbolFlags.BlockScoped && getIsNestedRedeclaration(symbol); + return isNestedRedeclarationSymbol(getSymbolOfNode(node)); } function isValueAliasDeclaration(node: Node): boolean { diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 96dec48d82c..6a68c6b0e85 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1237,24 +1237,25 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { } } else if (languageVersion < ScriptTarget.ES6) { - let alias = resolver.getReferencedImportDeclaration(node); - if (alias) { - if (alias.kind === SyntaxKind.ImportClause) { + let declaration = resolver.getReferencedImportDeclaration(node); + if (declaration) { + if (declaration.kind === SyntaxKind.ImportClause) { // Identifier references default import - write(getGeneratedNameForNode(alias.parent)); + write(getGeneratedNameForNode(declaration.parent)); write(languageVersion === ScriptTarget.ES3 ? '["default"]' : ".default"); + return; } - else { + else if (declaration.kind === SyntaxKind.ImportSpecifier) { // Identifier references named import - write(getGeneratedNameForNode(alias.parent.parent.parent)); + write(getGeneratedNameForNode(declaration.parent.parent.parent)); write("."); - writeTextOfNode(currentSourceFile, (alias).propertyName || (alias).name); + writeTextOfNode(currentSourceFile, (declaration).propertyName || (declaration).name); + return; } - return; } - let local = resolver.getReferencedNestedRedeclaration(node); - if (local) { - write(getGeneratedNameForNode(local.name)); + declaration = resolver.getReferencedNestedRedeclaration(node); + if (declaration) { + write(getGeneratedNameForNode(declaration.name)); return; } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index a8478a82797..205e1034448 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1397,7 +1397,7 @@ module ts { export interface EmitResolver { hasGlobalName(name: string): boolean; getReferencedExportContainer(node: Identifier): SourceFile | ModuleDeclaration | EnumDeclaration; - getReferencedImportDeclaration(node: Identifier): ImportClause | ImportSpecifier; + getReferencedImportDeclaration(node: Identifier): Declaration; getReferencedNestedRedeclaration(node: Identifier): Declaration; isNestedRedeclaration(node: Declaration): boolean; isValueAliasDeclaration(node: Node): boolean;