fix(57451): Prevent self-imports when using the "Move to File" refactor (#57530)

This commit is contained in:
Eloy Robillard
2024-03-20 14:06:46 -07:00
committed by GitHub
parent 4ecadc6c03
commit ede8ad84b1
3 changed files with 49 additions and 2 deletions
+10 -2
View File
@@ -18,6 +18,7 @@ import {
ClassDeclaration,
codefix,
combinePaths,
Comparison,
concatenate,
contains,
createModuleSpecifierResolutionHost,
@@ -53,12 +54,14 @@ import {
getLineAndCharacterOfPosition,
getLocaleSpecificMessage,
getModifiers,
getNormalizedAbsolutePath,
getPropertySymbolFromBindingElement,
getQuotePreference,
getRangesWhere,
getRefactorContextSpan,
getRelativePathFromFile,
getSourceFileOfNode,
getStringComparer,
getSynthesizedDeepClone,
getTokenAtPosition,
getUniqueName,
@@ -434,7 +437,12 @@ export function updateImportsInOtherFiles(
};
deleteUnusedImports(sourceFile, importNode, changes, shouldMove); // These will be changed to imports from the new file
const pathToTargetFileWithExtension = resolvePath(getDirectoryPath(oldFile.path), targetFileName);
const pathToTargetFileWithExtension = resolvePath(getDirectoryPath(getNormalizedAbsolutePath(oldFile.fileName, program.getCurrentDirectory())), targetFileName);
// no self-imports
if (getStringComparer(!program.useCaseSensitiveFileNames())(pathToTargetFileWithExtension, sourceFile.fileName) === Comparison.EqualTo) return;
const newModuleSpecifier = getModuleSpecifier(program.getCompilerOptions(), sourceFile, sourceFile.fileName, pathToTargetFileWithExtension, createModuleSpecifierResolutionHost(program, host));
const newImportDeclaration = filterImport(importNode, makeStringLiteral(newModuleSpecifier, quotePreference), shouldMove);
if (newImportDeclaration) changes.insertNodeAfter(sourceFile, statement, newImportDeclaration);
@@ -586,7 +594,7 @@ export function makeImportOrRequire(
useEs6Imports: boolean,
quotePreference: QuotePreference,
): AnyImportOrRequireStatement | undefined {
const pathToTargetFile = resolvePath(getDirectoryPath(sourceFile.path), targetFileNameWithExtension);
const pathToTargetFile = resolvePath(getDirectoryPath(getNormalizedAbsolutePath(sourceFile.fileName, program.getCurrentDirectory())), targetFileNameWithExtension);
const pathToTargetFileWithCorrectExtension = getModuleSpecifier(program.getCompilerOptions(), sourceFile, sourceFile.fileName, pathToTargetFile, createModuleSpecifierResolutionHost(program, host));
if (useEs6Imports) {
@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
//@Filename: /bar.ts
////import { y } from "./a";
// @Filename: /a.ts
////[|export const y = 1;|]
verify.moveToFile({
newFileContents: {
"/a.ts": "",
"/bar.ts":
`
export const y = 1;
`,
},
interactiveRefactorArguments: { targetFile: "/bar.ts" }
});
@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />
//@Filename: /bar.ts
////import { y, z } from "./a";
// @Filename: /a.ts
////[|export const y = 1;|]
////export const z = 2;
verify.moveToFile({
newFileContents: {
"/a.ts": "export const z = 2;",
"/bar.ts":
`import { z } from "./a";
export const y = 1;
`,
},
interactiveRefactorArguments: { targetFile: "/bar.ts" }
});