Skip to content

Do not resolve alias when getting symbol of import equal's right hand side #8789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1154,11 +1154,7 @@ namespace ts {
}

// This function is only for imports with entity names
function getSymbolOfPartOfRightHandSideOfImportEquals(entityName: EntityName, importDeclaration?: ImportEqualsDeclaration): Symbol {
if (!importDeclaration) {
importDeclaration = <ImportEqualsDeclaration>getAncestor(entityName, SyntaxKind.ImportEqualsDeclaration);
Debug.assert(importDeclaration !== undefined);
}
function getSymbolOfPartOfRightHandSideOfImportEquals(entityName: EntityName, importDeclaration: ImportEqualsDeclaration, dontResolveAlias?: boolean): Symbol {
// There are three things we might try to look for. In the following examples,
// the search term is enclosed in |...|:
//
Expand All @@ -1170,13 +1166,13 @@ namespace ts {
}
// Check for case 1 and 3 in the above example
if (entityName.kind === SyntaxKind.Identifier || entityName.parent.kind === SyntaxKind.QualifiedName) {
return resolveEntityName(entityName, SymbolFlags.Namespace);
return resolveEntityName(entityName, SymbolFlags.Namespace, /*ignoreErrors*/ false, dontResolveAlias);
}
else {
// Case 2 in above example
// entityName.kind could be a QualifiedName or a Missing identifier
Debug.assert(entityName.parent.kind === SyntaxKind.ImportEqualsDeclaration);
return resolveEntityName(entityName, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace);
return resolveEntityName(entityName, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace, /*ignoreErrors*/ false, dontResolveAlias);
}
}

Expand All @@ -1185,7 +1181,7 @@ namespace ts {
}

// Resolves a qualified name and any involved aliases
function resolveEntityName(name: EntityName | Expression, meaning: SymbolFlags, ignoreErrors?: boolean): Symbol {
function resolveEntityName(name: EntityName | Expression, meaning: SymbolFlags, ignoreErrors?: boolean, dontResolveAlias?: boolean): Symbol {
if (nodeIsMissing(name)) {
return undefined;
}
Expand Down Expand Up @@ -1219,7 +1215,7 @@ namespace ts {
Debug.fail("Unknown entity name kind.");
}
Debug.assert((symbol.flags & SymbolFlags.Instantiated) === 0, "Should never get an instantiated symbol here.");
return symbol.flags & meaning ? symbol : resolveAlias(symbol);
return (symbol.flags & meaning) || dontResolveAlias ? symbol : resolveAlias(symbol);
}

function resolveExternalModuleName(location: Node, moduleReferenceExpression: Expression): Symbol {
Expand Down Expand Up @@ -16441,7 +16437,9 @@ namespace ts {
if (entityName.kind !== SyntaxKind.PropertyAccessExpression) {
if (isInRightSideOfImportOrExportAssignment(<EntityName>entityName)) {
// Since we already checked for ExportAssignment, this really could only be an Import
return getSymbolOfPartOfRightHandSideOfImportEquals(<EntityName>entityName);
const importEqualsDeclaration = <ImportEqualsDeclaration>getAncestor(entityName, SyntaxKind.ImportEqualsDeclaration);
Debug.assert(importEqualsDeclaration !== undefined);
return getSymbolOfPartOfRightHandSideOfImportEquals(<EntityName>entityName, importEqualsDeclaration, /*dontResolveAlias*/ true);
}
}

Expand Down Expand Up @@ -16531,9 +16529,7 @@ namespace ts {

if (node.kind === SyntaxKind.Identifier) {
if (isInRightSideOfImportOrExportAssignment(<Identifier>node)) {
return node.parent.kind === SyntaxKind.ExportAssignment
? getSymbolOfEntityNameOrPropertyAccessExpression(<Identifier>node)
: getSymbolOfPartOfRightHandSideOfImportEquals(<Identifier>node);
return getSymbolOfEntityNameOrPropertyAccessExpression(<Identifier>node);
}
else if (node.parent.kind === SyntaxKind.BindingElement &&
node.parent.parent.kind === SyntaxKind.ObjectBindingPattern &&
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/acceptableAlias1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ module M {
import r = M.X;
>r : Symbol(r, Decl(acceptableAlias1.ts, 4, 1))
>M : Symbol(M, Decl(acceptableAlias1.ts, 0, 0))
>X : Symbol(r, Decl(acceptableAlias1.ts, 0, 10))
>X : Symbol(M.X, Decl(acceptableAlias1.ts, 2, 5))

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import foo = require("./foo_0");
// None of the below should cause a runtime dependency on foo_0
import f = foo.M1;
>f : Symbol(f, Decl(foo_1.ts, 0, 32))
>foo : Symbol(foo, Decl(foo_0.ts, 0, 0))
>foo : Symbol(foo, Decl(foo_1.ts, 0, 0))
>M1 : Symbol(foo.M1, Decl(foo_0.ts, 8, 1))

var i: f.I2;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/chainedImportAlias.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import x = require('./chainedImportAlias_file0');

import y = x;
>y : Symbol(y, Decl(chainedImportAlias_file1.ts, 0, 49))
>x : Symbol(x, Decl(chainedImportAlias_file0.ts, 0, 0))
>x : Symbol(x, Decl(chainedImportAlias_file1.ts, 0, 0))

y.m.foo();
>y.m.foo : Symbol(x.m.foo, Decl(chainedImportAlias_file0.ts, 0, 17))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import foo = require("./foo_0");
// None of the below should cause a runtime dependency on foo_0
import f = foo.M1;
>f : Symbol(f, Decl(foo_1.ts, 0, 32))
>foo : Symbol(foo, Decl(foo_0.ts, 0, 0))
>foo : Symbol(foo, Decl(foo_1.ts, 0, 0))
>M1 : Symbol(foo.M1, Decl(foo_0.ts, 8, 1))

var i: f.I2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var y = a.x;

export import b = a;
>b : Symbol(b, Decl(declFileForExportedImport_1.ts, 2, 12))
>a : Symbol(a, Decl(declFileForExportedImport_0.ts, 0, 0))
>a : Symbol(a, Decl(declFileForExportedImport_1.ts, 0, 0))

var z = b.x;
>z : Symbol(z, Decl(declFileForExportedImport_1.ts, 5, 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import a = m.c;

import b = a;
>b : Symbol(b, Decl(declFileImportChainInExportAssignment.ts, 6, 15))
>a : Symbol(a, Decl(declFileImportChainInExportAssignment.ts, 0, 10))
>a : Symbol(a, Decl(declFileImportChainInExportAssignment.ts, 5, 1))

export = b;
>b : Symbol(b, Decl(declFileImportChainInExportAssignment.ts, 6, 15))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export module M {

export import d = im;
>d : Symbol(d, Decl(declarationEmit_nameConflicts_0.ts, 11, 24))
>im : Symbol(d, Decl(declarationEmit_nameConflicts_1.ts, 0, 0))
>im : Symbol(im, Decl(declarationEmit_nameConflicts_0.ts, 0, 0))
}

export module M.P {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/dependencyViaImportAlias.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import a = require('A');

import A = a.A;
>A : Symbol(A, Decl(B.ts, 0, 24))
>a : Symbol(a, Decl(A.ts, 0, 0))
>a : Symbol(a, Decl(B.ts, 0, 0))
>A : Symbol(a.A, Decl(A.ts, 0, 0))

export = A;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { a } from "./es6ImportNamedImportInIndirectExportAssignment_0";

import x = a;
>x : Symbol(x, Decl(es6ImportNamedImportInIndirectExportAssignment_1.ts, 0, 71))
>a : Symbol(a, Decl(es6ImportNamedImportInIndirectExportAssignment_0.ts, 0, 0))
>a : Symbol(a, Decl(es6ImportNamedImportInIndirectExportAssignment_1.ts, 0, 8))

export = x;
>x : Symbol(x, Decl(es6ImportNamedImportInIndirectExportAssignment_1.ts, 0, 71))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare module 'timezonecomplete' {

export import TimeUnit = basics.TimeUnit;
>TimeUnit : Symbol(TimeUnit, Decl(externalModuleReferenceDoubleUnderscore1.ts, 1, 57))
>basics : Symbol(basics, Decl(externalModuleReferenceDoubleUnderscore1.ts, 3, 1))
>basics : Symbol(basics, Decl(externalModuleReferenceDoubleUnderscore1.ts, 0, 35))
>TimeUnit : Symbol(basics.TimeUnit, Decl(externalModuleReferenceDoubleUnderscore1.ts, 5, 44))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module m_private {
//import r2 = require('m'); // would be error
export import C = r; // no error
>C : Symbol(C, Decl(importAliasAnExternalModuleInsideAnInternalModule_file1.ts, 1, 18))
>r : Symbol(C, Decl(importAliasAnExternalModuleInsideAnInternalModule_file0.ts, 0, 0))
>r : Symbol(r, Decl(importAliasAnExternalModuleInsideAnInternalModule_file1.ts, 0, 0))

C.m.foo();
>C.m.foo : Symbol(C.m.foo, Decl(importAliasAnExternalModuleInsideAnInternalModule_file0.ts, 0, 17))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import appJs = require("file1");

import Services = appJs.Services;
>Services : Symbol(Services, Decl(file2.ts, 0, 32))
>appJs : Symbol(appJs, Decl(file1.ts, 0, 0))
>appJs : Symbol(appJs, Decl(file2.ts, 0, 0))
>Services : Symbol(appJs.Services, Decl(file1.ts, 0, 12))

import UserServices = Services.UserServices;
>UserServices : Symbol(UserServices, Decl(file2.ts, 1, 33))
>Services : Symbol(appJs.Services, Decl(file1.ts, 0, 12))
>Services : Symbol(Services, Decl(file2.ts, 0, 32))
>UserServices : Symbol(Services.UserServices, Decl(file1.ts, 1, 28))

var x = new UserServices().getUserName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import appJs = require("file1");

import Services = appJs.App.Services;
>Services : Symbol(Services, Decl(file2.ts, 0, 32))
>appJs : Symbol(appJs, Decl(file1.ts, 0, 0))
>appJs : Symbol(appJs, Decl(file2.ts, 0, 0))
>App : Symbol(appJs.App, Decl(file1.ts, 0, 0))
>Services : Symbol(Services, Decl(file1.ts, 0, 19))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import RT_ALIAS = require("file1");

import ReferredTo = RT_ALIAS.elaborate.nested.mod.name.ReferredTo;
>ReferredTo : Symbol(ReferredTo, Decl(file2.ts, 0, 35))
>RT_ALIAS : Symbol(RT_ALIAS, Decl(file1.ts, 0, 0))
>RT_ALIAS : Symbol(RT_ALIAS, Decl(file2.ts, 0, 0))
>elaborate : Symbol(RT_ALIAS.elaborate, Decl(file1.ts, 0, 0))
>nested : Symbol(RT_ALIAS.elaborate.nested, Decl(file1.ts, 0, 24))
>mod : Symbol(RT_ALIAS.elaborate.nested.mod, Decl(file1.ts, 0, 31))
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/tsxPreserveEmit1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ReactRouter = require('react-router');

import Route = ReactRouter.Route;
>Route : Symbol(Route, Decl(test.tsx, 2, 45))
>ReactRouter : Symbol(ReactRouter, Decl(react.d.ts, 4, 1))
>ReactRouter : Symbol(ReactRouter, Decl(test.tsx, 1, 32))
>Route : Symbol(ReactRouter.Route, Decl(react.d.ts, 7, 4))

var routes1 = <Route />;
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/fourslash/renameAlias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts'/>

////module SomeModule { export class SomeClass { } }
////import [|M|] = SomeModule;
////import C = [|M|].SomeClass;

let ranges = test.ranges()
for (let range of ranges) {
goTo.position(range.start);
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
}
11 changes: 11 additions & 0 deletions tests/cases/fourslash/renameAlias2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts'/>

////module [|SomeModule|] { export class SomeClass { } }
////import M = [|SomeModule|];
////import C = M.SomeClass;

let ranges = test.ranges()
for (let range of ranges) {
goTo.position(range.start);
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
}
11 changes: 11 additions & 0 deletions tests/cases/fourslash/renameAlias3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference path='fourslash.ts'/>

////module SomeModule { export class [|SomeClass|] { } }
////import M = SomeModule;
////import C = M.[|SomeClass|];

let ranges = test.ranges()
for (let range of ranges) {
goTo.position(range.start);
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
}
16 changes: 16 additions & 0 deletions tests/cases/fourslash/renameAliasExternalModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts'/>

// @Filename: a.ts
////module SomeModule { export class SomeClass { } }
////export = SomeModule;

// @Filename: b.ts
////import [|M|] = require("./a");
////import C = [|M|].SomeClass;

let ranges = test.ranges()
for (let range of ranges) {
goTo.file(range.fileName);
goTo.position(range.start);
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
}
16 changes: 16 additions & 0 deletions tests/cases/fourslash/renameAliasExternalModule2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts'/>

// @Filename: a.ts
////module [|SomeModule|] { export class SomeClass { } }
////export = [|SomeModule|];

// @Filename: b.ts
////import M = require("./a");
////import C = M.SomeClass;

let ranges = test.ranges()
for (let range of ranges) {
goTo.file(range.fileName);
goTo.position(range.start);
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
}
16 changes: 16 additions & 0 deletions tests/cases/fourslash/renameAliasExternalModule3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts'/>

// @Filename: a.ts
////module SomeModule { export class [|SomeClass|] { } }
////export = SomeModule;

// @Filename: b.ts
////import M = require("./a");
////import C = M.[|SomeClass|];

let ranges = test.ranges()
for (let range of ranges) {
goTo.file(range.fileName);
goTo.position(range.start);
verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false);
}