diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 952266d5ebc..636ea64ecea 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -1673,6 +1673,12 @@ namespace ts.FindAllReferences { function addReference(referenceLocation: Node, relatedSymbol: Symbol | RelatedSymbol, state: State): void { const { kind, symbol } = "kind" in relatedSymbol ? relatedSymbol : { kind: undefined, symbol: relatedSymbol }; // eslint-disable-line no-in-operator + + // if rename symbol from default export anonymous function, for example `export default function() {}`, we do not need to add reference + if (state.options.use === FindReferencesUse.Rename && referenceLocation.kind === SyntaxKind.DefaultKeyword) { + return; + } + const addRef = state.referenceAdder(symbol); if (state.options.implementations) { addImplementationReferences(referenceLocation, addRef, state); diff --git a/src/testRunner/unittests/tsserver/rename.ts b/src/testRunner/unittests/tsserver/rename.ts index a30eb895f09..6c0af5688da 100644 --- a/src/testRunner/unittests/tsserver/rename.ts +++ b/src/testRunner/unittests/tsserver/rename.ts @@ -174,6 +174,43 @@ namespace ts.projectSystem { }); }); + it("export default anonymous function works with prefixText and suffixText when disabled", () => { + const aTs: File = { path: "/a.ts", content: "export default function() {}" }; + const bTs: File = { path: "/b.ts", content: `import aTest from "./a"; function test() { return aTest(); }` }; + + const session = createSession(createServerHost([aTs, bTs])); + openFilesForSession([bTs], session); + + session.getProjectService().setHostConfiguration({ preferences: { providePrefixAndSuffixTextForRename: false } }); + const response1 = executeSessionRequest(session, protocol.CommandTypes.Rename, protocolFileLocationFromSubstring(bTs, "aTest(")); + assert.deepEqual(response1, { + info: { + canRename: true, + fileToRename: undefined, + displayName: "aTest", + fullDisplayName: "aTest", + kind: ScriptElementKind.alias, + kindModifiers: "export", + triggerSpan: protocolTextSpanFromSubstring(bTs.content, "aTest", { index: 1 }) + }, + locs: [{ + file: bTs.path, + locs: [ + protocolRenameSpanFromSubstring({ + fileText: bTs.content, + text: "aTest", + contextText: `import aTest from "./a";` + }), + protocolRenameSpanFromSubstring({ + fileText: bTs.content, + text: "aTest", + options: { index: 1 }, + }) + ] + }], + }); + }); + it("rename behavior is based on file of rename initiation", () => { const aTs: File = { path: "/a.ts", content: "const x = 1; export { x };" }; const bTs: File = { path: "/b.ts", content: `import { x } from "./a"; const y = x + 1;` };