Skip to content

Commit 6965a06

Browse files
committed
Support getReferences on rightside of export assignment and import statements
1 parent bbeeb8d commit 6965a06

File tree

6 files changed

+104
-22
lines changed

6 files changed

+104
-22
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6850,10 +6850,16 @@ module ts {
68506850

68516851
function getSymbolInfo(node: Node) {
68526852
if (isDeclarationOrFunctionExpressionOrCatchVariableName(node)) {
6853-
// In this case, we call getSymbolOfNode instead of getSymbolInfo because it is a declaration
6853+
// This is a declaration, call getSymbolOfNode
68546854
return getSymbolOfNode(node.parent);
68556855
}
68566856

6857+
if (isInRightSideOfImportOrExportAssignment(node)) {
6858+
return node.parent.kind === SyntaxKind.ExportAssignment
6859+
? getSymbolOfEntityName(<Identifier>node)
6860+
: getSymbolOfPartOfRightHandSideOfImport(node);
6861+
}
6862+
68576863
switch (node.kind) {
68586864
case SyntaxKind.Identifier:
68596865
case SyntaxKind.PropertyAccess:
@@ -6874,27 +6880,22 @@ module ts {
68746880
return undefined;
68756881

68766882
case SyntaxKind.StringLiteral:
6877-
case SyntaxKind.NumericLiteral:
6878-
switch (node.parent.kind) {
6879-
// index access
6880-
case SyntaxKind.IndexedAccess:
6881-
if ((<IndexedAccess>node.parent).index === node) {
6882-
var objectType = checkExpression((<IndexedAccess>node.parent).object);
6883-
if (objectType === unknownType) return undefined;
6884-
var apparentType = getApparentType(objectType);
6885-
if (<Type>apparentType === unknownType) return undefined;
6886-
return getPropertyOfApparentType(apparentType, (<LiteralExpression>node).text);
6887-
}
6888-
break;
6883+
// External module name in an import declaration
6884+
if (node.parent.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node.parent).externalModuleName === node) {
6885+
var importSymbol = getSymbolOfNode(node.parent);
6886+
var moduleType = getTypeOfSymbol(importSymbol);
6887+
return moduleType ? moduleType.symbol : undefined;
6888+
}
68896889

6890-
// External module name in an import declaration
6891-
case SyntaxKind.ImportDeclaration:
6892-
if ((<ImportDeclaration>node.parent).externalModuleName === node) {
6893-
var importSymbol = getSymbolOfNode(node.parent);
6894-
var moduleType = getTypeOfSymbol(importSymbol);
6895-
return moduleType ? moduleType.symbol : undefined;
6896-
}
6897-
break;
6890+
// Intentinal fallthrough
6891+
case SyntaxKind.NumericLiteral:
6892+
// index access
6893+
if (node.parent.kind == SyntaxKind.IndexedAccess && (<IndexedAccess>node.parent).index === node) {
6894+
var objectType = checkExpression((<IndexedAccess>node.parent).object);
6895+
if (objectType === unknownType) return undefined;
6896+
var apparentType = getApparentType(objectType);
6897+
if (<Type>apparentType === unknownType) return undefined;
6898+
return getPropertyOfApparentType(apparentType, (<LiteralExpression>node).text);
68986899
}
68996900
break;
69006901
}

src/services/services.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2485,8 +2485,37 @@ module ts {
24852485
return root.parent.kind === SyntaxKind.TypeReference && !isLastClause;
24862486
}
24872487

2488+
function isInRightSideOfImport(node: EntityName) {
2489+
while (node.parent.kind === SyntaxKind.QualifiedName) {
2490+
node = node.parent;
2491+
}
2492+
2493+
return node.parent.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node.parent).entityName === node;
2494+
}
2495+
2496+
function getMeaningFromRightHandSideOfImport(node: Node) {
2497+
Debug.assert(node.kind === SyntaxKind.Identifier);
2498+
2499+
// import a = |b|; // Namespace
2500+
// import a = |b.c|; // Value, type, namespace
2501+
// import a = |b.c|.d; // Namespace
2502+
2503+
if (node.parent.kind === SyntaxKind.QualifiedName &&
2504+
(<QualifiedName>node.parent).right === node &&
2505+
node.parent.parent.kind === SyntaxKind.ImportDeclaration) {
2506+
return SearchMeaning.Value | SearchMeaning.Type | SearchMeaning.Namespace;
2507+
}
2508+
return SearchMeaning.Namespace;
2509+
}
2510+
24882511
function getMeaningFromLocation(node: Node): SearchMeaning {
2489-
if (isDeclarationIdentifier(<Identifier>node)) {
2512+
if (node.parent.kind === SyntaxKind.ExportAssignment) {
2513+
return SearchMeaning.Value | SearchMeaning.Type | SearchMeaning.Namespace;
2514+
}
2515+
else if (isInRightSideOfImport(node)) {
2516+
return getMeaningFromRightHandSideOfImport(node);
2517+
}
2518+
else if (isDeclarationIdentifier(<Identifier>node)) {
24902519
return getMeaningFromDeclaration(node.parent);
24912520
}
24922521
else if (isTypeReference(node)) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////interface Foo { }
4+
////module Foo { export interface Bar { } }
5+
////function Foo() { }
6+
////
7+
////export = /*1*/Foo;
8+
9+
goTo.marker("1");
10+
verify.referencesCountIs(4);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////interface Foo { }
4+
////module Foo {
5+
//// export interface Bar { }
6+
//// export module Bar { export interface Baz { } }
7+
//// export function Bar() { }
8+
////}
9+
////
10+
////// module
11+
////import a1 = /*1*/Foo;
12+
13+
goTo.marker("1");
14+
verify.referencesCountIs(2);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////interface Foo { }
4+
////module Foo {
5+
//// export interface Bar { }
6+
//// export module Bar { export interface Baz { } }
7+
//// export function Bar() { }
8+
////}
9+
////
10+
////// module, value and type
11+
////import a2 = Foo./*1*/Bar;
12+
13+
goTo.marker("1");
14+
verify.referencesCountIs(4);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////interface Foo { }
4+
////module Foo {
5+
//// export interface Bar { }
6+
//// export module Bar { export interface Baz { } }
7+
//// export function Bar() { }
8+
////}
9+
////
10+
////// module
11+
////import a3 = Foo./*1*/Bar.Baz;
12+
13+
goTo.marker("1");
14+
verify.referencesCountIs(2);

0 commit comments

Comments
 (0)