fix54492: allow editor to check for original file extension for rename (#56680)

This commit is contained in:
Isabel Duan
2023-12-08 09:32:52 -08:00
committed by GitHub
parent e3d234cfc8
commit 8dfbfcb058
9 changed files with 236 additions and 9 deletions
+1
View File
@@ -1336,6 +1336,7 @@ export interface RenameInfoSuccess {
/**
* Full display name of item to be renamed.
* If item to be renamed is a file, then this is the original text of the module specifer
*/
fullDisplayName: string;
+4 -4
View File
@@ -185,17 +185,17 @@ function getRenameInfoForModule(node: StringLiteralLike, sourceFile: SourceFile,
const moduleSourceFile = moduleSymbol.declarations && find(moduleSymbol.declarations, isSourceFile);
if (!moduleSourceFile) return undefined;
const withoutIndex = endsWith(node.text, "/index") || endsWith(node.text, "/index.js") ? undefined : tryRemoveSuffix(removeFileExtension(moduleSourceFile.fileName), "/index");
const name = withoutIndex === undefined ? moduleSourceFile.fileName : withoutIndex;
const fileName = withoutIndex === undefined ? moduleSourceFile.fileName : withoutIndex;
const kind = withoutIndex === undefined ? ScriptElementKind.moduleElement : ScriptElementKind.directory;
const indexAfterLastSlash = node.text.lastIndexOf("/") + 1;
// Span should only be the last component of the path. + 1 to account for the quote character.
const triggerSpan = createTextSpan(node.getStart(sourceFile) + 1 + indexAfterLastSlash, node.text.length - indexAfterLastSlash);
return {
canRename: true,
fileToRename: name,
fileToRename: fileName,
kind,
displayName: name,
fullDisplayName: name,
displayName: fileName,
fullDisplayName: node.text,
kindModifiers: ScriptElementKindModifier.none,
triggerSpan,
};
+4
View File
@@ -1291,6 +1291,10 @@ export interface RenameInfoSuccess {
*/
fileToRename?: string;
displayName: string;
/**
* Full display name of item to be renamed.
* If item to be renamed is a file, then this is the original text of the module specifer
*/
fullDisplayName: string;
kind: ScriptElementKind;
kindModifiers: string;
@@ -193,4 +193,23 @@ describe("unittests:: tsserver:: rename", () => {
});
baselineTsserverLogs("rename", "with symlinks and case difference", session);
});
it("rename TS file with js extension", () => {
const aTs: File = { path: "/a.ts", content: "export const a = 1;" };
const bTs: File = { path: "/b.ts", content: `import * as foo from './a.js';` };
const host = createServerHost([aTs, bTs]);
const session = new TestSession(host);
openFilesForSession([aTs, bTs], session);
session.executeCommandSeq<ts.server.protocol.ConfigureRequest>({
command: ts.server.protocol.CommandTypes.Configure,
arguments: { preferences: { allowRenameOfImportPath: true } },
});
session.executeCommandSeq<ts.server.protocol.RenameRequest>({
command: ts.server.protocol.CommandTypes.Rename,
arguments: protocolFileLocationFromSubstring(bTs, "a.js"),
});
baselineTsserverLogs("rename", "rename TS file with js extension", session);
});
});