Support resolution of any part of the RHS of an import

This commit is contained in:
Jason Freeman
2014-08-19 15:04:30 -07:00
parent d60eecf0a4
commit bfa2e940c0
3 changed files with 35 additions and 17 deletions

View File

@@ -356,8 +356,7 @@ module ts {
var node = <ImportDeclaration>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);
}

View File

@@ -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;

View File

@@ -15,6 +15,6 @@ module B {
import Y = A;
>Y : Y
>A : number
>A : A
}