Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 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
30 changes: 30 additions & 0 deletions internal/fourslash/tests/codeLensReferencesIncludeImports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/fourslash"
"github.com/microsoft/typescript-go/internal/ls/lsutil"
"github.com/microsoft/typescript-go/internal/testutil"
)

func TestCodeLensReferencesIncludeImports(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")

const content = `
// @Filename: /abc.ts
export function abc() { }

// @Filename: /other.ts
import { abc } from "./abc";
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyBaselineCodeLens(t, &lsutil.UserPreferences{
CodeLens: lsutil.CodeLensUserPreferences{
ReferencesCodeLensEnabled: true,
ReferencesCodeLensShowOnAllFunctions: true,
},
})
}
14 changes: 14 additions & 0 deletions internal/ls/findallreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,12 @@ func (state *refState) getReferencesAtLocation(sourceFile *ast.SourceFile, posit
return
}

// For non-aliased imports like `import { foo }`, skip when coming from an export search,
// as these are already added in searchForImportsOfExport.
if parent.Kind == ast.KindImportSpecifier && parent.PropertyName() == nil && parent.Name() == referenceLocation && search.comingFrom == ImpExpKindExport {
return
}

if parent.Kind == ast.KindExportSpecifier {
state.getReferencesAtExportSpecifier(referenceLocation, referenceSymbol, parent.AsExportSpecifier(), search, addReferencesHere, false /*alwaysGetReferences*/)
return
Expand Down Expand Up @@ -2133,6 +2139,14 @@ func (state *refState) searchForImportsOfExport(exportLocation *ast.Node, export

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// === Code Lenses ===
// === /exports.ts ===
// let callCount = 0;
// export function /*CODELENS: 3 references*/foo(n: number): void {
// export function /*CODELENS: 4 references*/foo(n: number): void {
// callCount++;
// if (n > 0) {
// [|foo|](n - 1);
Expand All @@ -17,7 +17,7 @@
//

// === /importer.ts ===
// import { foo, bar } from "./exports";
// import { [|foo|], bar } from "./exports";
//
// [|foo|](5);
// console.log(bar);
Expand All @@ -27,7 +27,7 @@

// === Code Lenses ===
// === /importer.ts ===
// import { foo, bar } from "./exports";
// import { foo, [|bar|] } from "./exports";
//
// foo(5);
// console.log([|bar|]);
Expand All @@ -38,5 +38,5 @@
//
// foo(5);
//
// export const /*CODELENS: 1 reference*/bar = 123;
// export const /*CODELENS: 2 references*/bar = 123;
//
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

// === Code Lenses ===
// === /classPointable.ts ===
// import { Pointable } from "./pointable";
// import { [|Pointable|] } from "./pointable";
//
// class Point implements [|Pointable|] {
// getX(): number {
Expand All @@ -98,7 +98,7 @@
// --- (line: 7) skipped ---

// === /objectPointable.ts ===
// import { Pointable } from "./pointable";
// import { [|Pointable|] } from "./pointable";
//
// let x = 0;
// let y = 0;
Expand All @@ -109,7 +109,7 @@
// --- (line: 9) skipped ---

// === /pointable.ts ===
// export interface /*CODELENS: 2 references*/Pointable {
// export interface /*CODELENS: 4 references*/Pointable {
// getX(): number;
// getY(): number;
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// === Code Lenses ===
// === /other.ts ===
// import { [|abc|] } from "./abc";
//

// === /abc.ts ===
// export function /*CODELENS: 1 reference*/abc() { }
//
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

// === documentHighlights ===
// === /b.ts ===
// import { /*HIGHLIGHTS*/[|x|] } from "./a";
// import { /*HIGHLIGHTS*/[|[|x|]|] } from "./a";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// === findAllReferences ===
// === /bar.ts ===
// import { [|Foo|]/*FIND ALL REFS*/ } from "./foo";
// import { [|[|Foo|]|]/*FIND ALL REFS*/ } from "./foo";

// === /foo.ts ===
// export { [|Foo|] }
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// new D();

// === /b.ts ===
// import { C } from "./a";
// import { [|C|] } from "./a";
// new [|C|]();

// === /c.ts ===
// import { C } from "./a";
// import { [|C|] } from "./a";
// class D extends C {
// constructor() {
// [|super|]();
Expand Down Expand Up @@ -50,11 +50,11 @@
// new D();

// === /b.ts ===
// import { C } from "./a";
// import { [|C|] } from "./a";
// new [|C|]();

// === /c.ts ===
// import { C } from "./a";
// import { [|C|] } from "./a";
// class D extends C {
// constructor() {
// [|super|]();
Expand Down Expand Up @@ -86,11 +86,11 @@
// new D();

// === /b.ts ===
// import { C } from "./a";
// import { [|C|] } from "./a";
// new [|C|]();

// === /c.ts ===
// import { C } from "./a";
// import { [|C|] } from "./a";
// class D extends C {
// constructor() {
// [|super|]();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// exports.[|A|] = class {};

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


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

// === /b.js ===
// import { [|A|] } from "./a";
// import { [|[|A|]|] } from "./a";
// /*FIND ALL REFS*/[|A|];
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// === findAllReferences ===
// === /b.ts ===
// import { /*FIND ALL REFS*/[|f|] } from "a";
// import { /*FIND ALL REFS*/[|[|f|]|] } from "a";

// === /c.ts ===
// A.[|f|]();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
// export const [|D|] = C;

// === /b.ts ===
// import { /*FIND ALL REFS*/[|D|] } from "./a";
// import { /*FIND ALL REFS*/[|[|D|]|] } from "./a";
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// === findAllReferences ===
// === /a.js ===
// /**
// * @import { [|A|] } from "./b";
// * @import { [|[|A|]|] } from "./b";
// */
//
// /**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// --- (line: 5) skipped ---

// === /player.js ===
// import { [|Component|] } from './component.js';
// import { [|[|Component|]|] } from './component.js';
//
// /**
// * @extends [|Component|]/*FIND ALL REFS*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// export { [|B|] as [|B1|] } from "./f";

// === /b.ts ===
// import B, { B1 } from "./a";
// import B, { [|B1|] } from "./a";
// const d = new [|B|]("b");
// const d1 = new [|B1|]("b1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// }

// === /b.ts ===
// import { /*FIND ALL REFS*/[|Class|] } from "./a";
// import { /*FIND ALL REFS*/[|[|Class|]|] } from "./a";
//
// var c = new [|Class|]();

Expand All @@ -34,7 +34,7 @@
// }

// === /b.ts ===
// import { [|Class|] } from "./a";
// import { [|[|Class|]|] } from "./a";
//
// var c = new /*FIND ALL REFS*/[|Class|]();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

// === /file2.ts ===
// declare function log(s: string | number): void;
// import { /*FIND ALL REFS*/[|q|] } from "./file1";
// import { /*FIND ALL REFS*/[|[|q|]|] } from "./file1";
// log([|q|] + 1);


Expand All @@ -65,7 +65,7 @@

// === /file2.ts ===
// declare function log(s: string | number): void;
// import { [|q|] } from "./file1";
// import { [|[|q|]|] } from "./file1";
// log(/*FIND ALL REFS*/[|q|] + 1);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
// export { [|x|] as [|y|] };

// === /b.ts ===
// import { /*FIND ALL REFS*/[|x|], [|y|] } from "./a";
// import { /*FIND ALL REFS*/[|[|x|]|], [|y|] } from "./a";
// [|x|]; [|y|];


Expand All @@ -53,7 +53,7 @@
// export { [|x|] as [|y|] };

// === /b.ts ===
// import { [|x|], [|y|] } from "./a";
// import { [|[|x|]|], [|y|] } from "./a";
// /*FIND ALL REFS*/[|x|]; [|y|];


Expand All @@ -77,7 +77,7 @@
// export { x as [|y|] };

// === /b.ts ===
// import { x, /*FIND ALL REFS*/[|y|] } from "./a";
// import { x, /*FIND ALL REFS*/[|[|y|]|] } from "./a";
// x; [|y|];


Expand All @@ -89,5 +89,5 @@
// export { x as [|y|] };

// === /b.ts ===
// import { x, [|y|] } from "./a";
// import { x, [|[|y|]|] } from "./a";
// x; /*FIND ALL REFS*/[|y|];
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

// === /c.ts ===
// export { x } from "./b";
// import { /*FIND ALL REFS*/[|x|] } from "./a";
// import { /*FIND ALL REFS*/[|[|x|]|] } from "./a";
// [|x|];


Expand All @@ -54,7 +54,7 @@

// === /c.ts ===
// export { x } from "./b";
// import { [|x|] } from "./a";
// import { [|[|x|]|] } from "./a";
// /*FIND ALL REFS*/[|x|];


Expand All @@ -69,4 +69,4 @@
// x;

// === /d.ts ===
// import { /*FIND ALL REFS*/[|x|] } from "./c";
// import { /*FIND ALL REFS*/[|[|x|]|] } from "./c";
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// export function [|foo|](): void {}

// === /c.ts ===
// import { /*FIND ALL REFS*/[|foo|] } from "./b";
// import { /*FIND ALL REFS*/[|[|foo|]|] } from "./b";
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// export * as [|Leaf|] from './leafModule';

// === /importing.ts ===
// import { /*FIND ALL REFS*/[|Leaf|] } from './exporting';
// import { /*FIND ALL REFS*/[|[|Leaf|]|] } from './exporting';
// [|Leaf|].hello()


Expand All @@ -43,5 +43,5 @@
// export * as [|Leaf|] from './leafModule';

// === /importing.ts ===
// import { [|Leaf|] } from './exporting';
// import { [|[|Leaf|]|] } from './exporting';
// /*FIND ALL REFS*/[|Leaf|].hello()
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

// === findAllReferences ===
// === /app.ts ===
// import { /*FIND ALL REFS*/[|foo|] } from './foo/types';
// import { /*FIND ALL REFS*/[|[|foo|]|] } from './foo/types';
// export type fullType = [|foo|].Full;
// type namespaceImport = typeof import('./foo/types');
// type fullType2 = import('./foo/types').[|foo|].Full;
Expand All @@ -75,7 +75,7 @@

// === findAllReferences ===
// === /app.ts ===
// import { [|foo|] } from './foo/types';
// import { [|[|foo|]|] } from './foo/types';
// export type fullType = /*FIND ALL REFS*/[|foo|].Full;
// type namespaceImport = typeof import('./foo/types');
// type fullType2 = import('./foo/types').[|foo|].Full;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// <div style={{ fontFamily: "sans-serif" }}></div>;

// === /index.tsx ===
// import { /*FIND ALL REFS*/[|SubmissionComp|] } from "./RedditSubmission"
// import { /*FIND ALL REFS*/[|[|SubmissionComp|]|] } from "./RedditSubmission"
// function displaySubreddit(subreddit: string) {
// let components = submissions
// .map((value, index) => <[|SubmissionComp|] key={ index } elementPosition= { index } {...value.data} />);
Expand Down
Loading