Skip to content

Commit fdb1c2f

Browse files
authored
fix: export default anonymous function works with prefixText and suffixText when disabled (microsoft#48259)
1 parent 50a4b92 commit fdb1c2f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: src/services/findAllReferences.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,12 @@ namespace ts.FindAllReferences {
16731673

16741674
function addReference(referenceLocation: Node, relatedSymbol: Symbol | RelatedSymbol, state: State): void {
16751675
const { kind, symbol } = "kind" in relatedSymbol ? relatedSymbol : { kind: undefined, symbol: relatedSymbol }; // eslint-disable-line no-in-operator
1676+
1677+
// if rename symbol from default export anonymous function, for example `export default function() {}`, we do not need to add reference
1678+
if (state.options.use === FindReferencesUse.Rename && referenceLocation.kind === SyntaxKind.DefaultKeyword) {
1679+
return;
1680+
}
1681+
16761682
const addRef = state.referenceAdder(symbol);
16771683
if (state.options.implementations) {
16781684
addImplementationReferences(referenceLocation, addRef, state);

Diff for: src/testRunner/unittests/tsserver/rename.ts

+37
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,43 @@ namespace ts.projectSystem {
174174
});
175175
});
176176

177+
it("export default anonymous function works with prefixText and suffixText when disabled", () => {
178+
const aTs: File = { path: "/a.ts", content: "export default function() {}" };
179+
const bTs: File = { path: "/b.ts", content: `import aTest from "./a"; function test() { return aTest(); }` };
180+
181+
const session = createSession(createServerHost([aTs, bTs]));
182+
openFilesForSession([bTs], session);
183+
184+
session.getProjectService().setHostConfiguration({ preferences: { providePrefixAndSuffixTextForRename: false } });
185+
const response1 = executeSessionRequest<protocol.RenameRequest, protocol.RenameResponse>(session, protocol.CommandTypes.Rename, protocolFileLocationFromSubstring(bTs, "aTest("));
186+
assert.deepEqual<protocol.RenameResponseBody | undefined>(response1, {
187+
info: {
188+
canRename: true,
189+
fileToRename: undefined,
190+
displayName: "aTest",
191+
fullDisplayName: "aTest",
192+
kind: ScriptElementKind.alias,
193+
kindModifiers: "export",
194+
triggerSpan: protocolTextSpanFromSubstring(bTs.content, "aTest", { index: 1 })
195+
},
196+
locs: [{
197+
file: bTs.path,
198+
locs: [
199+
protocolRenameSpanFromSubstring({
200+
fileText: bTs.content,
201+
text: "aTest",
202+
contextText: `import aTest from "./a";`
203+
}),
204+
protocolRenameSpanFromSubstring({
205+
fileText: bTs.content,
206+
text: "aTest",
207+
options: { index: 1 },
208+
})
209+
]
210+
}],
211+
});
212+
});
213+
177214
it("rename behavior is based on file of rename initiation", () => {
178215
const aTs: File = { path: "/a.ts", content: "const x = 1; export { x };" };
179216
const bTs: File = { path: "/b.ts", content: `import { x } from "./a"; const y = x + 1;` };

0 commit comments

Comments
 (0)