fix typescript/54492: check if file rename changes extension (#200220)

check if file extension has been changed
This commit is contained in:
Isabel Duan 2023-12-12 17:23:10 -08:00 committed by GitHub
parent b5b6a80c1b
commit 948f6a1aaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -85,7 +85,7 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
}
if (renameInfo.fileToRename) {
const edits = await this.renameFile(renameInfo.fileToRename, newName, token);
const edits = await this.renameFile(renameInfo.fileToRename, renameInfo.fullDisplayName, newName, token);
if (edits) {
return edits;
} else {
@ -170,13 +170,17 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
private async renameFile(
fileToRename: string,
fullDisplayName: string,
newName: string,
token: vscode.CancellationToken,
): Promise<vscode.WorkspaceEdit | undefined> {
// Make sure we preserve file extension if none provided
// Make sure we preserve file extension if extension is unchanged or none provided
if (!path.extname(newName)) {
newName += path.extname(fileToRename);
}
else if (path.extname(newName) === path.extname(fullDisplayName)) {
newName = newName.slice(0, newName.length - path.extname(newName).length) + path.extname(fileToRename);
}
const dirname = path.dirname(fileToRename);
const newFilePath = path.join(dirname, newName);