Skip to content

Commit bfa2e94

Browse files
committed
Support resolution of any part of the RHS of an import
1 parent d60eecf commit bfa2e94

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/compiler/checker.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ module ts {
356356
var node = <ImportDeclaration>getDeclarationOfKind(symbol, SyntaxKind.ImportDeclaration);
357357
var target = node.externalModuleName ?
358358
resolveExternalModuleName(node, node.externalModuleName) :
359-
resolveEntityName(node, node.entityName, node.entityName.kind === SyntaxKind.QualifiedName ?
360-
SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace : SymbolFlags.Namespace);
359+
getSymbolOfPartOfRightHandSideOfImport(node.entityName, node);
361360
if (links.target === resolvingSymbol) {
362361
links.target = target || unknownSymbol;
363362
}
@@ -371,6 +370,33 @@ module ts {
371370
return links.target;
372371
}
373372

373+
// This function is only for imports with entity names
374+
function getSymbolOfPartOfRightHandSideOfImport(entityName: EntityName, importDeclaration?: ImportDeclaration): Symbol {
375+
if (!importDeclaration) {
376+
importDeclaration = getAncestor(entityName, SyntaxKind.ImportDeclaration);
377+
Debug.assert(importDeclaration);
378+
}
379+
// There are three things we might try to look for. In the following examples,
380+
// the search term is enclosed in |...|:
381+
//
382+
// import a = |b|; // Namespace
383+
// import a = |b.c|; // Value, type, namespace
384+
// import a = |b.c|.d; // Namespace
385+
if (entityName.kind === SyntaxKind.Identifier && isRightSideOfQualifiedNameOrPropertyAccess(entityName)) {
386+
entityName = entityName.parent;
387+
}
388+
// Check for case 1 and 3 in the above example
389+
if (entityName.kind === SyntaxKind.Identifier || entityName.parent.kind === SyntaxKind.QualifiedName) {
390+
return resolveEntityName(importDeclaration, entityName, SymbolFlags.Namespace);
391+
}
392+
else {
393+
// Case 2 in above example
394+
// entityName.kind could be a QualifiedName or a Missing identifier
395+
Debug.assert(entityName.parent.kind === SyntaxKind.ImportDeclaration);
396+
return resolveEntityName(importDeclaration, entityName, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace);
397+
}
398+
}
399+
374400
function getFullyQualifiedName(symbol: Symbol) {
375401
return symbol.parent ? getFullyQualifiedName(symbol.parent) + "." + symbolToString(symbol) : symbolToString(symbol);
376402
}
@@ -6894,18 +6920,10 @@ module ts {
68946920

68956921
if (isInRightSideOfImportOrExportAssignment(node)) {
68966922
var symbol: Symbol;
6897-
if (node.parent.kind === SyntaxKind.ExportAssignment) {
6898-
symbol = getSymbolInfo(node);
6899-
}
6900-
else {
6901-
// It is an import statement
6902-
if (isRightSideOfQualifiedNameOrPropertyAccess(node)) {
6903-
node = node.parent;
6904-
}
6905-
// We include all declaration spaces for aliases. This is likely too inclusive, as the rules
6906-
// for resolving aliases are quite particular. Ideally this should reuse the logic in resolveAlias.
6907-
symbol = resolveEntityName(node, node, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Import);
6908-
}
6923+
symbol = node.parent.kind === SyntaxKind.ExportAssignment
6924+
? getSymbolInfo(node)
6925+
: getSymbolOfPartOfRightHandSideOfImport(node);
6926+
69096927
var declaredType = getDeclaredTypeOfSymbol(symbol);
69106928
return declaredType !== unknownType ? declaredType : getTypeOfSymbol(symbol);
69116929
}

tests/baselines/reference/importOnAliasedIdentifiers.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ module B {
1919

2020
import Y = A; // Alias only for module A
2121
>Y : typeof A
22-
>A : A
22+
>A : typeof A
2323

2424
import Z = A.X; // Alias for both type and member A.X
2525
>Z : X
26-
>A : A
26+
>A : typeof A
2727
>X : X
2828

2929
var v: Z = Z;

tests/baselines/reference/internalImportUnInstantiatedModuleNotReferencingInstanceNoConflict.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ module B {
1515

1616
import Y = A;
1717
>Y : Y
18-
>A : number
18+
>A : A
1919
}
2020

0 commit comments

Comments
 (0)