Find references of a module by filename (#41805)

* Naive implementation enough to build and write a test

* Add simple test

* Add project references test

* Add deduplication test, accept baselines

* Add test for referencing a script (doesn’t do anything)

* Update API baselines

* Use refFileMap for non-module references

* Fix find-all-refs on module specifier

* Remove unused util

* Don’t store text range on ts.RefFile

* Ensure string literal could itself be a file reference

* Remove unused utilities

* Improve baseline format

* Preserve old behavior of falling back to string literal references

* Update baselines from master

* Fix old RefFileMap code after merge

* Add test for additional response info

* Undo test change
This commit is contained in:
Andrew Branch
2020-12-11 12:37:02 -08:00
committed by GitHub
parent 1c1cd9b08d
commit 9dfbf07d8a
36 changed files with 810 additions and 45 deletions

View File

@@ -171,6 +171,7 @@
"unittests/tsserver/getApplicableRefactors.ts",
"unittests/tsserver/getEditsForFileRename.ts",
"unittests/tsserver/getExportReferences.ts",
"unittests/tsserver/getFileReferences.ts",
"unittests/tsserver/importHelpers.ts",
"unittests/tsserver/importSuggestionsCache.ts",
"unittests/tsserver/inferredProjects.ts",

View File

@@ -0,0 +1,58 @@
namespace ts.projectSystem {
describe("unittests:: tsserver:: getFileReferences", () => {
const importA = `import "./a";`;
const importCurlyFromA = `import {} from "./a";`;
const importAFromA = `import { a } from "/project/a";`;
const typeofImportA = `type T = typeof import("./a").a;`;
const aTs: File = {
path: "/project/a.ts",
content: "export const a = {};",
};
const bTs: File = {
path: "/project/b.ts",
content: importA,
};
const cTs: File = {
path: "/project/c.ts",
content: importCurlyFromA
};
const dTs: File = {
path: "/project/d.ts",
content: [importAFromA, typeofImportA].join("\n")
};
const tsconfig: File = {
path: "/project/tsconfig.json",
content: "{}",
};
function makeSampleSession() {
const host = createServerHost([aTs, bTs, cTs, dTs, tsconfig]);
const session = createSession(host);
openFilesForSession([aTs, bTs, cTs, dTs], session);
return session;
}
it("should get file references", () => {
const session = makeSampleSession();
const response = executeSessionRequest<protocol.FileReferencesRequest, protocol.FileReferencesResponse>(
session,
protocol.CommandTypes.FileReferences,
{ file: aTs.path },
);
const expectResponse: protocol.FileReferencesResponseBody = {
refs: [
makeReferenceItem({ file: bTs, text: "./a", lineText: importA, contextText: importA, isDefinition: false, isWriteAccess: false }),
makeReferenceItem({ file: cTs, text: "./a", lineText: importCurlyFromA, contextText: importCurlyFromA, isDefinition: false, isWriteAccess: false }),
makeReferenceItem({ file: dTs, text: "/project/a", lineText: importAFromA, contextText: importAFromA, isDefinition: false, isWriteAccess: false }),
makeReferenceItem({ file: dTs, text: "./a", lineText: typeofImportA, contextText: typeofImportA, isDefinition: false, isWriteAccess: false }),
],
symbolName: `"${aTs.path}"`,
};
assert.deepEqual(response, expectResponse);
});
});
}

View File

@@ -207,6 +207,8 @@ namespace ts.server {
CommandNames.Implementation,
CommandNames.ImplementationFull,
CommandNames.Exit,
CommandNames.FileReferences,
CommandNames.FileReferencesFull,
CommandNames.Format,
CommandNames.Formatonkey,
CommandNames.FormatFull,