Skip to content

fix(48258): export default anonymous function works with prefixText and suffixText when disabled #48259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/services/findAllReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions src/testRunner/unittests/tsserver/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<protocol.RenameRequest, protocol.RenameResponse>(session, protocol.CommandTypes.Rename, protocolFileLocationFromSubstring(bTs, "aTest("));
assert.deepEqual<protocol.RenameResponseBody | undefined>(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;` };
Expand Down