Skip to content

Commit 3515652

Browse files
committed
Merge pull request #8789 from Microsoft/getSymbolAtAliasLocation
Do not resolve alias when getting symbol of import equal's right hand side
2 parents a1f110f + b8b38f3 commit 3515652

22 files changed

+106
-29
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,11 +1180,7 @@ namespace ts {
11801180
}
11811181

11821182
// This function is only for imports with entity names
1183-
function getSymbolOfPartOfRightHandSideOfImportEquals(entityName: EntityName, importDeclaration?: ImportEqualsDeclaration): Symbol {
1184-
if (!importDeclaration) {
1185-
importDeclaration = <ImportEqualsDeclaration>getAncestor(entityName, SyntaxKind.ImportEqualsDeclaration);
1186-
Debug.assert(importDeclaration !== undefined);
1187-
}
1183+
function getSymbolOfPartOfRightHandSideOfImportEquals(entityName: EntityName, importDeclaration: ImportEqualsDeclaration, dontResolveAlias?: boolean): Symbol {
11881184
// There are three things we might try to look for. In the following examples,
11891185
// the search term is enclosed in |...|:
11901186
//
@@ -1196,13 +1192,13 @@ namespace ts {
11961192
}
11971193
// Check for case 1 and 3 in the above example
11981194
if (entityName.kind === SyntaxKind.Identifier || entityName.parent.kind === SyntaxKind.QualifiedName) {
1199-
return resolveEntityName(entityName, SymbolFlags.Namespace);
1195+
return resolveEntityName(entityName, SymbolFlags.Namespace, /*ignoreErrors*/ false, dontResolveAlias);
12001196
}
12011197
else {
12021198
// Case 2 in above example
12031199
// entityName.kind could be a QualifiedName or a Missing identifier
12041200
Debug.assert(entityName.parent.kind === SyntaxKind.ImportEqualsDeclaration);
1205-
return resolveEntityName(entityName, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace);
1201+
return resolveEntityName(entityName, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace, /*ignoreErrors*/ false, dontResolveAlias);
12061202
}
12071203
}
12081204

@@ -1211,7 +1207,7 @@ namespace ts {
12111207
}
12121208

12131209
// Resolves a qualified name and any involved aliases
1214-
function resolveEntityName(name: EntityName | Expression, meaning: SymbolFlags, ignoreErrors?: boolean): Symbol {
1210+
function resolveEntityName(name: EntityName | Expression, meaning: SymbolFlags, ignoreErrors?: boolean, dontResolveAlias?: boolean): Symbol {
12151211
if (nodeIsMissing(name)) {
12161212
return undefined;
12171213
}
@@ -1245,7 +1241,7 @@ namespace ts {
12451241
Debug.fail("Unknown entity name kind.");
12461242
}
12471243
Debug.assert((symbol.flags & SymbolFlags.Instantiated) === 0, "Should never get an instantiated symbol here.");
1248-
return symbol.flags & meaning ? symbol : resolveAlias(symbol);
1244+
return (symbol.flags & meaning) || dontResolveAlias ? symbol : resolveAlias(symbol);
12491245
}
12501246

12511247
function resolveExternalModuleName(location: Node, moduleReferenceExpression: Expression): Symbol {
@@ -16803,7 +16799,9 @@ namespace ts {
1680316799
if (entityName.kind !== SyntaxKind.PropertyAccessExpression) {
1680416800
if (isInRightSideOfImportOrExportAssignment(<EntityName>entityName)) {
1680516801
// Since we already checked for ExportAssignment, this really could only be an Import
16806-
return getSymbolOfPartOfRightHandSideOfImportEquals(<EntityName>entityName);
16802+
const importEqualsDeclaration = <ImportEqualsDeclaration>getAncestor(entityName, SyntaxKind.ImportEqualsDeclaration);
16803+
Debug.assert(importEqualsDeclaration !== undefined);
16804+
return getSymbolOfPartOfRightHandSideOfImportEquals(<EntityName>entityName, importEqualsDeclaration, /*dontResolveAlias*/ true);
1680716805
}
1680816806
}
1680916807

@@ -16899,9 +16897,7 @@ namespace ts {
1689916897

1690016898
if (node.kind === SyntaxKind.Identifier) {
1690116899
if (isInRightSideOfImportOrExportAssignment(<Identifier>node)) {
16902-
return node.parent.kind === SyntaxKind.ExportAssignment
16903-
? getSymbolOfEntityNameOrPropertyAccessExpression(<Identifier>node)
16904-
: getSymbolOfPartOfRightHandSideOfImportEquals(<Identifier>node);
16900+
return getSymbolOfEntityNameOrPropertyAccessExpression(<Identifier>node);
1690516901
}
1690616902
else if (node.parent.kind === SyntaxKind.BindingElement &&
1690716903
node.parent.parent.kind === SyntaxKind.ObjectBindingPattern &&

tests/baselines/reference/acceptableAlias1.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ module M {
1313
import r = M.X;
1414
>r : Symbol(r, Decl(acceptableAlias1.ts, 4, 1))
1515
>M : Symbol(M, Decl(acceptableAlias1.ts, 0, 0))
16-
>X : Symbol(r, Decl(acceptableAlias1.ts, 0, 10))
16+
>X : Symbol(M.X, Decl(acceptableAlias1.ts, 2, 5))
1717

tests/baselines/reference/amdImportNotAsPrimaryExpression.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import foo = require("./foo_0");
55
// None of the below should cause a runtime dependency on foo_0
66
import f = foo.M1;
77
>f : Symbol(f, Decl(foo_1.ts, 0, 32))
8-
>foo : Symbol(foo, Decl(foo_0.ts, 0, 0))
8+
>foo : Symbol(foo, Decl(foo_1.ts, 0, 0))
99
>M1 : Symbol(foo.M1, Decl(foo_0.ts, 8, 1))
1010

1111
var i: f.I2;

tests/baselines/reference/chainedImportAlias.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import x = require('./chainedImportAlias_file0');
44

55
import y = x;
66
>y : Symbol(y, Decl(chainedImportAlias_file1.ts, 0, 49))
7-
>x : Symbol(x, Decl(chainedImportAlias_file0.ts, 0, 0))
7+
>x : Symbol(x, Decl(chainedImportAlias_file1.ts, 0, 0))
88

99
y.m.foo();
1010
>y.m.foo : Symbol(x.m.foo, Decl(chainedImportAlias_file0.ts, 0, 17))

tests/baselines/reference/commonJSImportNotAsPrimaryExpression.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import foo = require("./foo_0");
55
// None of the below should cause a runtime dependency on foo_0
66
import f = foo.M1;
77
>f : Symbol(f, Decl(foo_1.ts, 0, 32))
8-
>foo : Symbol(foo, Decl(foo_0.ts, 0, 0))
8+
>foo : Symbol(foo, Decl(foo_1.ts, 0, 0))
99
>M1 : Symbol(foo.M1, Decl(foo_0.ts, 8, 1))
1010

1111
var i: f.I2;

tests/baselines/reference/declFileForExportedImport.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var y = a.x;
1111

1212
export import b = a;
1313
>b : Symbol(b, Decl(declFileForExportedImport_1.ts, 2, 12))
14-
>a : Symbol(a, Decl(declFileForExportedImport_0.ts, 0, 0))
14+
>a : Symbol(a, Decl(declFileForExportedImport_1.ts, 0, 0))
1515

1616
var z = b.x;
1717
>z : Symbol(z, Decl(declFileForExportedImport_1.ts, 5, 3))

tests/baselines/reference/declFileImportChainInExportAssignment.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import a = m.c;
1717

1818
import b = a;
1919
>b : Symbol(b, Decl(declFileImportChainInExportAssignment.ts, 6, 15))
20-
>a : Symbol(a, Decl(declFileImportChainInExportAssignment.ts, 0, 10))
20+
>a : Symbol(a, Decl(declFileImportChainInExportAssignment.ts, 5, 1))
2121

2222
export = b;
2323
>b : Symbol(b, Decl(declFileImportChainInExportAssignment.ts, 6, 15))

tests/baselines/reference/declarationEmit_nameConflicts.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export module M {
3737

3838
export import d = im;
3939
>d : Symbol(d, Decl(declarationEmit_nameConflicts_0.ts, 11, 24))
40-
>im : Symbol(d, Decl(declarationEmit_nameConflicts_1.ts, 0, 0))
40+
>im : Symbol(im, Decl(declarationEmit_nameConflicts_0.ts, 0, 0))
4141
}
4242

4343
export module M.P {

tests/baselines/reference/dependencyViaImportAlias.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import a = require('A');
44

55
import A = a.A;
66
>A : Symbol(A, Decl(B.ts, 0, 24))
7-
>a : Symbol(a, Decl(A.ts, 0, 0))
7+
>a : Symbol(a, Decl(B.ts, 0, 0))
88
>A : Symbol(a.A, Decl(A.ts, 0, 0))
99

1010
export = A;

tests/baselines/reference/es6ImportNamedImportInIndirectExportAssignment.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { a } from "./es6ImportNamedImportInIndirectExportAssignment_0";
1414

1515
import x = a;
1616
>x : Symbol(x, Decl(es6ImportNamedImportInIndirectExportAssignment_1.ts, 0, 71))
17-
>a : Symbol(a, Decl(es6ImportNamedImportInIndirectExportAssignment_0.ts, 0, 0))
17+
>a : Symbol(a, Decl(es6ImportNamedImportInIndirectExportAssignment_1.ts, 0, 8))
1818

1919
export = x;
2020
>x : Symbol(x, Decl(es6ImportNamedImportInIndirectExportAssignment_1.ts, 0, 71))

tests/baselines/reference/externalModuleReferenceDoubleUnderscore1.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare module 'timezonecomplete' {
55

66
export import TimeUnit = basics.TimeUnit;
77
>TimeUnit : Symbol(TimeUnit, Decl(externalModuleReferenceDoubleUnderscore1.ts, 1, 57))
8-
>basics : Symbol(basics, Decl(externalModuleReferenceDoubleUnderscore1.ts, 3, 1))
8+
>basics : Symbol(basics, Decl(externalModuleReferenceDoubleUnderscore1.ts, 0, 35))
99
>TimeUnit : Symbol(basics.TimeUnit, Decl(externalModuleReferenceDoubleUnderscore1.ts, 5, 44))
1010
}
1111

tests/baselines/reference/importAliasAnExternalModuleInsideAnInternalModule.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module m_private {
88
//import r2 = require('m'); // would be error
99
export import C = r; // no error
1010
>C : Symbol(C, Decl(importAliasAnExternalModuleInsideAnInternalModule_file1.ts, 1, 18))
11-
>r : Symbol(C, Decl(importAliasAnExternalModuleInsideAnInternalModule_file0.ts, 0, 0))
11+
>r : Symbol(r, Decl(importAliasAnExternalModuleInsideAnInternalModule_file1.ts, 0, 0))
1212

1313
C.m.foo();
1414
>C.m.foo : Symbol(C.m.foo, Decl(importAliasAnExternalModuleInsideAnInternalModule_file0.ts, 0, 17))

tests/baselines/reference/import_reference-exported-alias.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import appJs = require("file1");
44

55
import Services = appJs.Services;
66
>Services : Symbol(Services, Decl(file2.ts, 0, 32))
7-
>appJs : Symbol(appJs, Decl(file1.ts, 0, 0))
7+
>appJs : Symbol(appJs, Decl(file2.ts, 0, 0))
88
>Services : Symbol(appJs.Services, Decl(file1.ts, 0, 12))
99

1010
import UserServices = Services.UserServices;
1111
>UserServices : Symbol(UserServices, Decl(file2.ts, 1, 33))
12-
>Services : Symbol(appJs.Services, Decl(file1.ts, 0, 12))
12+
>Services : Symbol(Services, Decl(file2.ts, 0, 32))
1313
>UserServices : Symbol(Services.UserServices, Decl(file1.ts, 1, 28))
1414

1515
var x = new UserServices().getUserName();

tests/baselines/reference/import_reference-to-type-alias.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import appJs = require("file1");
44

55
import Services = appJs.App.Services;
66
>Services : Symbol(Services, Decl(file2.ts, 0, 32))
7-
>appJs : Symbol(appJs, Decl(file1.ts, 0, 0))
7+
>appJs : Symbol(appJs, Decl(file2.ts, 0, 0))
88
>App : Symbol(appJs.App, Decl(file1.ts, 0, 0))
99
>Services : Symbol(Services, Decl(file1.ts, 0, 19))
1010

tests/baselines/reference/importedAliasesInTypePositions.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import RT_ALIAS = require("file1");
44

55
import ReferredTo = RT_ALIAS.elaborate.nested.mod.name.ReferredTo;
66
>ReferredTo : Symbol(ReferredTo, Decl(file2.ts, 0, 35))
7-
>RT_ALIAS : Symbol(RT_ALIAS, Decl(file1.ts, 0, 0))
7+
>RT_ALIAS : Symbol(RT_ALIAS, Decl(file2.ts, 0, 0))
88
>elaborate : Symbol(RT_ALIAS.elaborate, Decl(file1.ts, 0, 0))
99
>nested : Symbol(RT_ALIAS.elaborate.nested, Decl(file1.ts, 0, 24))
1010
>mod : Symbol(RT_ALIAS.elaborate.nested.mod, Decl(file1.ts, 0, 31))

tests/baselines/reference/tsxPreserveEmit1.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ReactRouter = require('react-router');
88

99
import Route = ReactRouter.Route;
1010
>Route : Symbol(Route, Decl(test.tsx, 2, 45))
11-
>ReactRouter : Symbol(ReactRouter, Decl(react.d.ts, 4, 1))
11+
>ReactRouter : Symbol(ReactRouter, Decl(test.tsx, 1, 32))
1212
>Route : Symbol(ReactRouter.Route, Decl(react.d.ts, 7, 4))
1313

1414
var routes1 = <Route />;

tests/cases/fourslash/renameAlias.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////module SomeModule { export class SomeClass { } }
4+
////import [|M|] = SomeModule;
5+
////import C = [|M|].SomeClass;
6+
7+
let ranges = test.ranges()
8+
for (let range of ranges) {
9+
goTo.position(range.start);
10+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
11+
}

tests/cases/fourslash/renameAlias2.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////module [|SomeModule|] { export class SomeClass { } }
4+
////import M = [|SomeModule|];
5+
////import C = M.SomeClass;
6+
7+
let ranges = test.ranges()
8+
for (let range of ranges) {
9+
goTo.position(range.start);
10+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
11+
}

tests/cases/fourslash/renameAlias3.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////module SomeModule { export class [|SomeClass|] { } }
4+
////import M = SomeModule;
5+
////import C = M.[|SomeClass|];
6+
7+
let ranges = test.ranges()
8+
for (let range of ranges) {
9+
goTo.position(range.start);
10+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @Filename: a.ts
4+
////module SomeModule { export class SomeClass { } }
5+
////export = SomeModule;
6+
7+
// @Filename: b.ts
8+
////import [|M|] = require("./a");
9+
////import C = [|M|].SomeClass;
10+
11+
let ranges = test.ranges()
12+
for (let range of ranges) {
13+
goTo.file(range.fileName);
14+
goTo.position(range.start);
15+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @Filename: a.ts
4+
////module [|SomeModule|] { export class SomeClass { } }
5+
////export = [|SomeModule|];
6+
7+
// @Filename: b.ts
8+
////import M = require("./a");
9+
////import C = M.SomeClass;
10+
11+
let ranges = test.ranges()
12+
for (let range of ranges) {
13+
goTo.file(range.fileName);
14+
goTo.position(range.start);
15+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @Filename: a.ts
4+
////module SomeModule { export class [|SomeClass|] { } }
5+
////export = SomeModule;
6+
7+
// @Filename: b.ts
8+
////import M = require("./a");
9+
////import C = M.[|SomeClass|];
10+
11+
let ranges = test.ranges()
12+
for (let range of ranges) {
13+
goTo.file(range.fileName);
14+
goTo.position(range.start);
15+
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
16+
}

0 commit comments

Comments
 (0)