Skip to content

Commit d137923

Browse files
Copilotjakebailey
andcommitted
Fix references code lens to include imports as references
When finding references for an exported symbol, the import specifier (e.g., `abc` in `import { abc } from "./abc"`) should be counted as a reference to the export. This change: 1. Adds the import location as a reference to the export symbol in searchForImportsOfExport for non-aliased imports. 2. Skips duplicate addition of import specifiers when the search is coming from an export context, since they're already added by the explicit addRef call. Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent e1aa16c commit d137923

File tree

46 files changed

+106
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+106
-76
lines changed

internal/ls/findallreferences.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,12 @@ func (state *refState) getReferencesAtLocation(sourceFile *ast.SourceFile, posit
19101910
return
19111911
}
19121912

1913+
// For non-aliased imports like `import { foo }`, skip when coming from an export search,
1914+
// as these are already added in searchForImportsOfExport.
1915+
if parent.Kind == ast.KindImportSpecifier && parent.PropertyName() == nil && parent.Name() == referenceLocation && search.comingFrom == ImpExpKindExport {
1916+
return
1917+
}
1918+
19131919
if parent.Kind == ast.KindExportSpecifier {
19141920
state.getReferencesAtExportSpecifier(referenceLocation, referenceSymbol, parent.AsExportSpecifier(), search, addReferencesHere, false /*alwaysGetReferences*/)
19151921
return
@@ -2133,6 +2139,14 @@ func (state *refState) searchForImportsOfExport(exportLocation *ast.Node, export
21332139

21342140
// For each import, find all references to that import in its source file.
21352141
for _, i := range r.importSearches {
2142+
// For `import { abc }` (no alias), add the import as a reference to the export symbol.
2143+
// For `import { foo as bar }`, `foo` is already added via singleReferences above,
2144+
// and `bar` is just a local alias, not a reference to the export.
2145+
if ast.IsImportSpecifier(i.importLocation.Parent) && i.importLocation.Parent.PropertyName() == nil {
2146+
addRef := state.referenceAdder(exportSymbol)
2147+
addRef(i.importLocation, entryKindNode)
2148+
}
2149+
// Then search for uses of the imported symbol in the file
21362150
state.getReferencesInSourceFile(ast.GetSourceFileOfNode(i.importLocation), state.createSearch(i.importLocation, i.importSymbol, ImpExpKindExport, "", nil), true /*addReferencesHere*/)
21372151
}
21382152

testdata/baselines/reference/fourslash/codeLenses/codeLensFunctionsAndConstants01.baseline.jsonc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// === Code Lenses ===
22
// === /exports.ts ===
33
// let callCount = 0;
4-
// export function /*CODELENS: 3 references*/foo(n: number): void {
4+
// export function /*CODELENS: 4 references*/foo(n: number): void {
55
// callCount++;
66
// if (n > 0) {
77
// [|foo|](n - 1);
@@ -17,7 +17,7 @@
1717
//
1818

1919
// === /importer.ts ===
20-
// import { foo, bar } from "./exports";
20+
// import { [|foo|], bar } from "./exports";
2121
//
2222
// [|foo|](5);
2323
// console.log(bar);
@@ -27,7 +27,7 @@
2727

2828
// === Code Lenses ===
2929
// === /importer.ts ===
30-
// import { foo, bar } from "./exports";
30+
// import { foo, [|bar|] } from "./exports";
3131
//
3232
// foo(5);
3333
// console.log([|bar|]);
@@ -38,5 +38,5 @@
3838
//
3939
// foo(5);
4040
//
41-
// export const /*CODELENS: 1 reference*/bar = 123;
41+
// export const /*CODELENS: 2 references*/bar = 123;
4242
//

testdata/baselines/reference/fourslash/codeLenses/codeLensInterface01.baseline.jsonc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
// === Code Lenses ===
9191
// === /classPointable.ts ===
92-
// import { Pointable } from "./pointable";
92+
// import { [|Pointable|] } from "./pointable";
9393
//
9494
// class Point implements [|Pointable|] {
9595
// getX(): number {
@@ -98,7 +98,7 @@
9898
// --- (line: 7) skipped ---
9999

100100
// === /objectPointable.ts ===
101-
// import { Pointable } from "./pointable";
101+
// import { [|Pointable|] } from "./pointable";
102102
//
103103
// let x = 0;
104104
// let y = 0;
@@ -109,7 +109,7 @@
109109
// --- (line: 9) skipped ---
110110

111111
// === /pointable.ts ===
112-
// export interface /*CODELENS: 2 references*/Pointable {
112+
// export interface /*CODELENS: 4 references*/Pointable {
113113
// getX(): number;
114114
// getY(): number;
115115
// }
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
// === Code Lenses ===
2+
// === /other.ts ===
3+
// import { [|abc|] } from "./abc";
4+
//
5+
26
// === /abc.ts ===
3-
// export function /*CODELENS: 0 references*/abc() { }
7+
// export function /*CODELENS: 1 reference*/abc() { }
48
//

testdata/baselines/reference/fourslash/documentHighlights/documentHighlights_filesToSearch.baseline.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
// === documentHighlights ===
88
// === /b.ts ===
9-
// import { /*HIGHLIGHTS*/[|x|] } from "./a";
9+
// import { /*HIGHLIGHTS*/[|[|x|]|] } from "./a";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// === findAllReferences ===
22
// === /bar.ts ===
3-
// import { [|Foo|]/*FIND ALL REFS*/ } from "./foo";
3+
// import { [|[|Foo|]|]/*FIND ALL REFS*/ } from "./foo";
44

55
// === /foo.ts ===
66
// export { [|Foo|] }

testdata/baselines/reference/fourslash/findAllReferences/findAllReferencesOfConstructor.baseline.jsonc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
// new D();
1515

1616
// === /b.ts ===
17-
// import { C } from "./a";
17+
// import { [|C|] } from "./a";
1818
// new [|C|]();
1919

2020
// === /c.ts ===
21-
// import { C } from "./a";
21+
// import { [|C|] } from "./a";
2222
// class D extends C {
2323
// constructor() {
2424
// [|super|]();
@@ -50,11 +50,11 @@
5050
// new D();
5151

5252
// === /b.ts ===
53-
// import { C } from "./a";
53+
// import { [|C|] } from "./a";
5454
// new [|C|]();
5555

5656
// === /c.ts ===
57-
// import { C } from "./a";
57+
// import { [|C|] } from "./a";
5858
// class D extends C {
5959
// constructor() {
6060
// [|super|]();
@@ -86,11 +86,11 @@
8686
// new D();
8787

8888
// === /b.ts ===
89-
// import { C } from "./a";
89+
// import { [|C|] } from "./a";
9090
// new [|C|]();
9191

9292
// === /c.ts ===
93-
// import { C } from "./a";
93+
// import { [|C|] } from "./a";
9494
// class D extends C {
9595
// constructor() {
9696
// [|super|]();

testdata/baselines/reference/fourslash/findAllReferences/findAllRefsClassExpression2.baseline.jsonc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// exports.[|A|] = class {};
1414

1515
// === /b.js ===
16-
// import { /*FIND ALL REFS*/[|A|] } from "./a";
16+
// import { /*FIND ALL REFS*/[|[|A|]|] } from "./a";
1717
// [|A|];
1818

1919

@@ -23,5 +23,5 @@
2323
// exports.[|A|] = class {};
2424

2525
// === /b.js ===
26-
// import { [|A|] } from "./a";
26+
// import { [|[|A|]|] } from "./a";
2727
// /*FIND ALL REFS*/[|A|];

testdata/baselines/reference/fourslash/findAllReferences/findAllRefsExportAsNamespace.baseline.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// === findAllReferences ===
1515
// === /b.ts ===
16-
// import { /*FIND ALL REFS*/[|f|] } from "a";
16+
// import { /*FIND ALL REFS*/[|[|f|]|] } from "a";
1717

1818
// === /c.ts ===
1919
// A.[|f|]();

testdata/baselines/reference/fourslash/findAllReferences/findAllRefsExportConstEqualToClass.baseline.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
// export const [|D|] = C;
1515

1616
// === /b.ts ===
17-
// import { /*FIND ALL REFS*/[|D|] } from "./a";
17+
// import { /*FIND ALL REFS*/[|[|D|]|] } from "./a";

0 commit comments

Comments
 (0)