Skip to content

Improve reference finding for imported variables #6367

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 7 additions & 5 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5490,9 +5490,11 @@ namespace ts {
};
}

function isImportOrExportSpecifierImportSymbol(symbol: Symbol) {
function isES6StyleImportSymbol(symbol: Symbol) {
return (symbol.flags & SymbolFlags.Alias) && forEach(symbol.declarations, declaration => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you only use kind, can you just declare a variable named kind or destructure it from the parameter?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Fixed and amended to the last commit

return declaration.kind === SyntaxKind.ImportSpecifier || declaration.kind === SyntaxKind.ExportSpecifier;
const { kind } = declaration;
return kind === SyntaxKind.ImportSpecifier || kind === SyntaxKind.ExportSpecifier
|| kind === SyntaxKind.ImportClause || kind === SyntaxKind.NamespaceImport;
});
}

Expand Down Expand Up @@ -5936,8 +5938,8 @@ namespace ts {
// The search set contains at least the current symbol
let result = [symbol];

// If the symbol is an alias, add what it alaises to the list
if (isImportOrExportSpecifierImportSymbol(symbol)) {
// If the symbol is an alias, add what it aliases to the list
if (isES6StyleImportSymbol(symbol)) {
result.push(typeChecker.getAliasedSymbol(symbol));
}

Expand Down Expand Up @@ -6028,7 +6030,7 @@ namespace ts {

// If the reference symbol is an alias, check if what it is aliasing is one of the search
// symbols.
if (isImportOrExportSpecifierImportSymbol(referenceSymbol)) {
if (isES6StyleImportSymbol(referenceSymbol)) {
const aliasedSymbol = typeChecker.getAliasedSymbol(referenceSymbol);
if (searchSymbols.indexOf(aliasedSymbol) >= 0) {
return aliasedSymbol;
Expand Down
10 changes: 10 additions & 0 deletions tests/cases/fourslash/renameImportAndExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />

////import [|a|] from "module";
////export { [|a|] };

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

////import [|foo|] from 'bar';
////const bar = { [|foo|] };

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

////import * as [|foo|] from 'bar';
////const bar = { [|foo|] };

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