Skip to content

Commit 76b1600

Browse files
author
Andy Hanson
committed
Support find-all-references for anonymous default exports
1 parent 49676c5 commit 76b1600

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22986,6 +22986,9 @@ namespace ts {
2298622986
return getPropertyOfType(objectType, (<NumericLiteral>node).text as __String);
2298722987
}
2298822988
break;
22989+
22990+
case SyntaxKind.DefaultKeyword:
22991+
return getSymbolOfNode(node.parent);
2298922992
}
2299022993
return undefined;
2299122994
}

src/services/findAllReferences.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ namespace ts.FindAllReferences {
176176
fileName: node.getSourceFile().fileName,
177177
textSpan: getTextSpan(node),
178178
isWriteAccess: isWriteAccess(node),
179-
isDefinition: isAnyDeclarationName(node) || isLiteralComputedPropertyDeclarationName(node),
179+
isDefinition: node.kind === SyntaxKind.DefaultKeyword
180+
|| isAnyDeclarationName(node)
181+
|| isLiteralComputedPropertyDeclarationName(node),
180182
isInString
181183
};
182184
}
@@ -243,7 +245,7 @@ namespace ts.FindAllReferences {
243245

244246
/** A node is considered a writeAccess iff it is a name of a declaration or a target of an assignment */
245247
function isWriteAccess(node: Node): boolean {
246-
if (isAnyDeclarationName(node)) {
248+
if (node.kind === SyntaxKind.DefaultKeyword || isAnyDeclarationName(node)) {
247249
return true;
248250
}
249251

@@ -743,7 +745,7 @@ namespace ts.FindAllReferences.Core {
743745

744746
function isValidReferencePosition(node: Node, searchSymbolName: string): boolean {
745747
// Compare the length so we filter out strict superstrings of the symbol we are looking for
746-
switch (node && node.kind) {
748+
switch (node.kind) {
747749
case SyntaxKind.Identifier:
748750
return (node as Identifier).text.length === searchSymbolName.length;
749751

@@ -754,6 +756,9 @@ namespace ts.FindAllReferences.Core {
754756
case SyntaxKind.NumericLiteral:
755757
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node) && (node as NumericLiteral).text.length === searchSymbolName.length;
756758

759+
case SyntaxKind.DefaultKeyword:
760+
return "default".length === searchSymbolName.length;
761+
757762
default:
758763
return false;
759764
}

src/services/importTracker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,11 @@ namespace ts.FindAllReferences {
525525
importedSymbol = getExportEqualsLocalSymbol(importedSymbol, checker);
526526
}
527527

528-
if (symbolName(importedSymbol) === symbol.escapedName) { // If this is a rename import, do not continue searching.
528+
// If the import has a different name than the export, do not continue searching.
529+
// If `importedName` is undefined, do continue searching as the export is anonymous.
530+
// (All imports returned from this function will be ignored anyway if we are in rename and this is a not a named export.)
531+
const importedName = symbolName(importedSymbol);
532+
if (importedName === undefined || importedName === symbol.escapedName) {
529533
return { kind: ImportExport.Import, symbol: importedSymbol, ...isImport };
530534
}
531535
}

tests/cases/fourslash/findAllRefsForDefaultExport04.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
// @Filename: /a.ts
44
////const [|{| "isWriteAccess": true, "isDefinition": true |}a|] = 0;
5-
////export default [|a|];
5+
////export [|{| "isWriteAccess": true, "isDefinition": true |}default|] [|a|];
66

77
// @Filename: /b.ts
88
////import [|{| "isWriteAccess": true, "isDefinition": true |}a|] from "./a";
99
////[|a|];
1010

11-
const [r0, r1, r2, r3] = test.ranges();
12-
verify.referenceGroups([r0, r1], [
13-
{ definition: "const a: 0", ranges: [r0, r1] },
14-
{ definition: "import a", ranges: [r2, r3] }
11+
const [r0, r1, r2, r3, r4] = test.ranges();
12+
verify.referenceGroups([r0, r2], [
13+
{ definition: "const a: 0", ranges: [r0, r2] },
14+
{ definition: "import a", ranges: [r3, r4] }
15+
]);
16+
verify.referenceGroups(r1, [
17+
// TODO:GH#17990
18+
{ definition: "import default", ranges: [r1] },
19+
{ definition: "import a", ranges: [r3, r4] },
20+
]);
21+
verify.referenceGroups([r3, r4], [
22+
{ definition: "import a", ranges: [r3, r4] },
23+
// TODO:GH#17990
24+
{ definition: "import default", ranges: [r1] },
1525
]);
16-
verify.singleReferenceGroup("import a", [r2, r3]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export [|{| "isWriteAccess": true, "isDefinition": true |}default|] function() {}
5+
6+
// @Filename: /b.ts
7+
////import [|{| "isWriteAccess": true, "isDefinition": true |}f|] from "./a";
8+
9+
const [r0, r1] = test.ranges();
10+
verify.referenceGroups(r0, [
11+
{ definition: "function default(): void", ranges: [r0] },
12+
{ definition: "import f", ranges: [r1] },
13+
]);
14+
verify.referenceGroups(r1, [
15+
{ definition: "import f", ranges: [r1] },
16+
{ definition: "function default(): void", ranges: [r0] },
17+
]);
18+
19+
// Verify that it doesn't try to rename "default"
20+
verify.renameInfoFailed(r0);
21+
verify.renameLocations(r1, [r1]);

0 commit comments

Comments
 (0)