diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ef3d67ddb0e..538d2d91af4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -356,8 +356,7 @@ module ts { var node = getDeclarationOfKind(symbol, SyntaxKind.ImportDeclaration); var target = node.externalModuleName ? resolveExternalModuleName(node, node.externalModuleName) : - resolveEntityName(node, node.entityName, node.entityName.kind === SyntaxKind.QualifiedName ? - SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace : SymbolFlags.Namespace); + getSymbolOfPartOfRightHandSideOfImport(node.entityName, node); if (links.target === resolvingSymbol) { links.target = target || unknownSymbol; } @@ -371,6 +370,33 @@ module ts { return links.target; } + // This function is only for imports with entity names + function getSymbolOfPartOfRightHandSideOfImport(entityName: EntityName, importDeclaration?: ImportDeclaration): Symbol { + if (!importDeclaration) { + importDeclaration = getAncestor(entityName, SyntaxKind.ImportDeclaration); + Debug.assert(importDeclaration); + } + // There are three things we might try to look for. In the following examples, + // the search term is enclosed in |...|: + // + // import a = |b|; // Namespace + // import a = |b.c|; // Value, type, namespace + // import a = |b.c|.d; // Namespace + if (entityName.kind === SyntaxKind.Identifier && isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { + entityName = entityName.parent; + } + // Check for case 1 and 3 in the above example + if (entityName.kind === SyntaxKind.Identifier || entityName.parent.kind === SyntaxKind.QualifiedName) { + return resolveEntityName(importDeclaration, entityName, SymbolFlags.Namespace); + } + else { + // Case 2 in above example + // entityName.kind could be a QualifiedName or a Missing identifier + Debug.assert(entityName.parent.kind === SyntaxKind.ImportDeclaration); + return resolveEntityName(importDeclaration, entityName, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace); + } + } + function getFullyQualifiedName(symbol: Symbol) { return symbol.parent ? getFullyQualifiedName(symbol.parent) + "." + symbolToString(symbol) : symbolToString(symbol); } @@ -6894,18 +6920,10 @@ module ts { if (isInRightSideOfImportOrExportAssignment(node)) { var symbol: Symbol; - if (node.parent.kind === SyntaxKind.ExportAssignment) { - symbol = getSymbolInfo(node); - } - else { - // It is an import statement - if (isRightSideOfQualifiedNameOrPropertyAccess(node)) { - node = node.parent; - } - // We include all declaration spaces for aliases. This is likely too inclusive, as the rules - // for resolving aliases are quite particular. Ideally this should reuse the logic in resolveAlias. - symbol = resolveEntityName(node, node, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Import); - } + symbol = node.parent.kind === SyntaxKind.ExportAssignment + ? getSymbolInfo(node) + : getSymbolOfPartOfRightHandSideOfImport(node); + var declaredType = getDeclaredTypeOfSymbol(symbol); return declaredType !== unknownType ? declaredType : getTypeOfSymbol(symbol); } diff --git a/tests/baselines/reference/importOnAliasedIdentifiers.types b/tests/baselines/reference/importOnAliasedIdentifiers.types index 187e7ceb97f..a7d2f678a30 100644 --- a/tests/baselines/reference/importOnAliasedIdentifiers.types +++ b/tests/baselines/reference/importOnAliasedIdentifiers.types @@ -19,11 +19,11 @@ module B { import Y = A; // Alias only for module A >Y : typeof A ->A : A +>A : typeof A import Z = A.X; // Alias for both type and member A.X >Z : X ->A : A +>A : typeof A >X : X var v: Z = Z; diff --git a/tests/baselines/reference/internalImportUnInstantiatedModuleNotReferencingInstanceNoConflict.types b/tests/baselines/reference/internalImportUnInstantiatedModuleNotReferencingInstanceNoConflict.types index b961f93fc8c..01549965a13 100644 --- a/tests/baselines/reference/internalImportUnInstantiatedModuleNotReferencingInstanceNoConflict.types +++ b/tests/baselines/reference/internalImportUnInstantiatedModuleNotReferencingInstanceNoConflict.types @@ -15,6 +15,6 @@ module B { import Y = A; >Y : Y ->A : number +>A : A }