fix(54283): Provide better UX when an invalid file is selected for 'move to file' refactoring (#54300)

This commit is contained in:
Oleksandr T
2023-05-18 20:36:34 +03:00
committed by GitHub
parent 174599c554
commit ce1c97f4f0
11 changed files with 176 additions and 30 deletions

View File

@@ -53,7 +53,9 @@ import {
getRelativePathFromFile,
getSynthesizedDeepClone,
getUniqueName,
hasJSFileExtension,
hasSyntacticModifier,
hasTSFileExtension,
hostGetCanonicalFileName,
Identifier,
ImportDeclaration,
@@ -162,8 +164,12 @@ registerRefactor(refactorNameForMoveToFile, {
Debug.assert(actionName === refactorNameForMoveToFile, "Wrong refactor invoked");
const statements = Debug.checkDefined(getStatementsToMove(context));
Debug.assert(interactiveRefactorArguments, "No interactive refactor arguments available");
const edits = textChanges.ChangeTracker.with(context, t => doChange(context, context.file, interactiveRefactorArguments.targetFile, context.program, statements, t, context.host, context.preferences));
return { edits, renameFilename: undefined, renameLocation: undefined };
const targetFile = interactiveRefactorArguments.targetFile;
if (hasJSFileExtension(targetFile) || hasTSFileExtension(targetFile)) {
const edits = textChanges.ChangeTracker.with(context, t => doChange(context, context.file, interactiveRefactorArguments.targetFile, context.program, statements, t, context.host, context.preferences));
return { edits, renameFilename: undefined, renameLocation: undefined };
}
return { edits: [], renameFilename: undefined, renameLocation: undefined, notApplicableReason: getLocaleSpecificMessage(Diagnostics.Cannot_move_to_file_selected_file_is_invalid) };
}
});

View File

@@ -1002,6 +1002,7 @@ export interface RefactorEditInfo {
renameFilename?: string;
renameLocation?: number;
commands?: CodeActionCommand[];
notApplicableReason?: string;
}
export type RefactorTriggerReason = "implicit" | "invoked";