Skip to content

Commit 6512579

Browse files
author
Andy
authored
Merge pull request microsoft#13643 from Microsoft/find_all_refs_default
Support find-all-references for default exports
2 parents 57400fd + 5bf9b30 commit 6512579

File tree

9 files changed

+275
-188
lines changed

9 files changed

+275
-188
lines changed

src/compiler/utilities.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ namespace ts {
395395

396396
function isShorthandAmbientModule(node: Node): boolean {
397397
// The only kind of module that can be missing a body is a shorthand ambient module.
398-
return node.kind === SyntaxKind.ModuleDeclaration && (!(<ModuleDeclaration>node).body);
398+
return node && node.kind === SyntaxKind.ModuleDeclaration && (!(<ModuleDeclaration>node).body);
399399
}
400400

401401
export function isBlockScopedContainerTopLevel(node: Node): boolean {
@@ -3104,7 +3104,11 @@ namespace ts {
31043104
}
31053105

31063106
export function getLocalSymbolForExportDefault(symbol: Symbol) {
3107-
return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, ModifierFlags.Default) ? symbol.valueDeclaration.localSymbol : undefined;
3107+
return isExportDefaultSymbol(symbol) ? symbol.valueDeclaration.localSymbol : undefined;
3108+
}
3109+
3110+
export function isExportDefaultSymbol(symbol: Symbol): boolean {
3111+
return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, ModifierFlags.Default);
31083112
}
31093113

31103114
/** Return ".ts", ".d.ts", or ".tsx", if that is the extension. */

src/services/documentHighlights.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace ts.DocumentHighlights {
1717
}
1818

1919
function getSemanticDocumentHighlights(node: Node, typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFilesToSearch: SourceFile[]): DocumentHighlights[] {
20-
const referencedSymbols = FindAllReferences.getReferencedSymbolsForNode(typeChecker, cancellationToken, node, sourceFilesToSearch, /*findInStrings*/false, /*findInComments*/false, /*implementations*/false);
20+
const referencedSymbols = FindAllReferences.getReferencedSymbolsForNode(typeChecker, cancellationToken, node, sourceFilesToSearch);
2121
return referencedSymbols && convertReferencedSymbols(referencedSymbols);
2222
}
2323

src/services/findAllReferences.ts

Lines changed: 223 additions & 174 deletions
Large diffs are not rendered by default.

src/services/goToImplementation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace ts.GoToImplementation {
1717
else {
1818
// Perform "Find all References" and retrieve only those that are implementations
1919
const referencedSymbols = FindAllReferences.getReferencedSymbolsForNode(typeChecker, cancellationToken,
20-
node, sourceFiles, /*findInStrings*/false, /*findInComments*/false, /*implementations*/true);
20+
node, sourceFiles, /*findInStrings*/false, /*findInComments*/false, /*isForRename*/false, /*implementations*/true);
2121
const result = flatMap(referencedSymbols, symbol =>
2222
map(symbol.references, ({ textSpan, fileName }) => ({ textSpan, fileName })));
2323

src/services/services.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,25 +1404,25 @@ namespace ts {
14041404
}
14051405

14061406
function findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] {
1407-
const referencedSymbols = findReferencedSymbols(fileName, position, findInStrings, findInComments);
1407+
const referencedSymbols = findReferencedSymbols(fileName, position, findInStrings, findInComments, /*isForRename*/true);
14081408
return FindAllReferences.convertReferences(referencedSymbols);
14091409
}
14101410

14111411
function getReferencesAtPosition(fileName: string, position: number): ReferenceEntry[] {
1412-
const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false);
1412+
const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false, /*isForRename*/false);
14131413
return FindAllReferences.convertReferences(referencedSymbols);
14141414
}
14151415

14161416
function findReferences(fileName: string, position: number): ReferencedSymbol[] {
1417-
const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false);
1417+
const referencedSymbols = findReferencedSymbols(fileName, position, /*findInStrings*/ false, /*findInComments*/ false, /*isForRename*/false);
14181418

14191419
// Only include referenced symbols that have a valid definition.
14201420
return filter(referencedSymbols, rs => !!rs.definition);
14211421
}
14221422

1423-
function findReferencedSymbols(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): ReferencedSymbol[] {
1423+
function findReferencedSymbols(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, isForRename: boolean): ReferencedSymbol[] {
14241424
synchronizeHostData();
1425-
return FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments);
1425+
return FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments, isForRename);
14261426
}
14271427

14281428
/// NavigateTo

src/services/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ namespace ts {
13191319
return name;
13201320
}
13211321

1322-
export function isImportOrExportSpecifierName(location: Node): boolean {
1322+
export function isImportOrExportSpecifierName(location: Node): location is Identifier {
13231323
return location.parent &&
13241324
(location.parent.kind === SyntaxKind.ImportSpecifier || location.parent.kind === SyntaxKind.ExportSpecifier) &&
13251325
(<ImportOrExportSpecifier>location.parent).propertyName === location;
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+
// @Filename: a.ts
4+
////export default function /*def*/[|f|]() {}
5+
6+
// @Filename: b.ts
7+
////import [|g|] from "./a";
8+
/////*ref*/[|g|]();
9+
10+
verify.rangesReferenceEachOther();
11+
verify.goToDefinition("ref", "def");
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @Filename: B.ts
4-
////export default class [|B|] {
4+
////export default class /*1*/[|B|] {
55
//// test() {
66
//// }
77
////}
@@ -11,4 +11,17 @@
1111
////let b = new [|B|]();
1212
////b.test();
1313

14-
verify.rangesAreRenameLocations();
14+
goTo.marker("1");
15+
verify.occurrencesAtPositionCount(1);
16+
17+
const [C, B0, B1] = test.ranges();
18+
verify.rangesReferenceEachOther();
19+
20+
goTo.rangeStart(C);
21+
verify.renameLocations(false, false, [C, B0, B1]);
22+
23+
const rangesInB = [B0, B1];
24+
for (const r of rangesInB) {
25+
goTo.rangeStart(r);
26+
verify.renameLocations(false, false, rangesInB);
27+
}

tests/cases/fourslash/renameDefaultImportDifferentName.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @Filename: B.ts
4-
////export default class /*1*/C {
4+
////export default class /*1*/[|C|] {
55
//// test() {
66
//// }
77
////}
@@ -14,4 +14,14 @@
1414
goTo.marker("1");
1515
verify.occurrencesAtPositionCount(1);
1616

17-
verify.rangesAreRenameLocations();
17+
const [C, B0, B1] = test.ranges();
18+
verify.rangesReferenceEachOther();
19+
20+
goTo.rangeStart(C);
21+
verify.renameLocations(false, false, [C, B0, B1]);
22+
23+
const rangesInB = [B0, B1];
24+
for (const r of rangesInB) {
25+
goTo.rangeStart(r);
26+
verify.renameLocations(false, false, rangesInB);
27+
}

0 commit comments

Comments
 (0)