Skip to content

Commit ede8ad8

Browse files
fix(57451): Prevent self-imports when using the "Move to File" refactor (#57530)
1 parent 4ecadc6 commit ede8ad8

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/services/refactors/moveToFile.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ClassDeclaration,
1919
codefix,
2020
combinePaths,
21+
Comparison,
2122
concatenate,
2223
contains,
2324
createModuleSpecifierResolutionHost,
@@ -53,12 +54,14 @@ import {
5354
getLineAndCharacterOfPosition,
5455
getLocaleSpecificMessage,
5556
getModifiers,
57+
getNormalizedAbsolutePath,
5658
getPropertySymbolFromBindingElement,
5759
getQuotePreference,
5860
getRangesWhere,
5961
getRefactorContextSpan,
6062
getRelativePathFromFile,
6163
getSourceFileOfNode,
64+
getStringComparer,
6265
getSynthesizedDeepClone,
6366
getTokenAtPosition,
6467
getUniqueName,
@@ -434,7 +437,12 @@ export function updateImportsInOtherFiles(
434437
};
435438
deleteUnusedImports(sourceFile, importNode, changes, shouldMove); // These will be changed to imports from the new file
436439

437-
const pathToTargetFileWithExtension = resolvePath(getDirectoryPath(oldFile.path), targetFileName);
440+
const pathToTargetFileWithExtension = resolvePath(getDirectoryPath(getNormalizedAbsolutePath(oldFile.fileName, program.getCurrentDirectory())), targetFileName);
441+
442+
// no self-imports
443+
444+
if (getStringComparer(!program.useCaseSensitiveFileNames())(pathToTargetFileWithExtension, sourceFile.fileName) === Comparison.EqualTo) return;
445+
438446
const newModuleSpecifier = getModuleSpecifier(program.getCompilerOptions(), sourceFile, sourceFile.fileName, pathToTargetFileWithExtension, createModuleSpecifierResolutionHost(program, host));
439447
const newImportDeclaration = filterImport(importNode, makeStringLiteral(newModuleSpecifier, quotePreference), shouldMove);
440448
if (newImportDeclaration) changes.insertNodeAfter(sourceFile, statement, newImportDeclaration);
@@ -586,7 +594,7 @@ export function makeImportOrRequire(
586594
useEs6Imports: boolean,
587595
quotePreference: QuotePreference,
588596
): AnyImportOrRequireStatement | undefined {
589-
const pathToTargetFile = resolvePath(getDirectoryPath(sourceFile.path), targetFileNameWithExtension);
597+
const pathToTargetFile = resolvePath(getDirectoryPath(getNormalizedAbsolutePath(sourceFile.fileName, program.getCurrentDirectory())), targetFileNameWithExtension);
590598
const pathToTargetFileWithCorrectExtension = getModuleSpecifier(program.getCompilerOptions(), sourceFile, sourceFile.fileName, pathToTargetFile, createModuleSpecifierResolutionHost(program, host));
591599

592600
if (useEs6Imports) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//@Filename: /bar.ts
4+
////import { y } from "./a";
5+
6+
// @Filename: /a.ts
7+
////[|export const y = 1;|]
8+
9+
verify.moveToFile({
10+
newFileContents: {
11+
"/a.ts": "",
12+
13+
"/bar.ts":
14+
`
15+
export const y = 1;
16+
`,
17+
},
18+
interactiveRefactorArguments: { targetFile: "/bar.ts" }
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//@Filename: /bar.ts
4+
////import { y, z } from "./a";
5+
6+
// @Filename: /a.ts
7+
////[|export const y = 1;|]
8+
////export const z = 2;
9+
10+
verify.moveToFile({
11+
newFileContents: {
12+
"/a.ts": "export const z = 2;",
13+
14+
"/bar.ts":
15+
`import { z } from "./a";
16+
export const y = 1;
17+
`,
18+
},
19+
interactiveRefactorArguments: { targetFile: "/bar.ts" }
20+
});

0 commit comments

Comments
 (0)