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

This commit is contained in:
Jm
2022-03-23 01:43:41 +08:00
committed by GitHub
parent 50a4b92bca
commit fdb1c2fc35
2 changed files with 43 additions and 0 deletions

View File

@@ -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);

View File

@@ -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;` };