Skip to content

Commit

Permalink
Prevent self-imports on "Move to File" refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eloyrobillard committed Mar 15, 2024
1 parent 8f531ff commit 8e69f41
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ClassDeclaration,
codefix,
combinePaths,
Comparison,
concatenate,
contains,
createModuleSpecifierResolutionHost,
Expand Down Expand Up @@ -52,12 +53,14 @@ import {
getDirectoryPath,
getLocaleSpecificMessage,
getModifiers,
getNormalizedAbsolutePath,
getPropertySymbolFromBindingElement,
getQuotePreference,
getRangesWhere,
getRefactorContextSpan,
getRelativePathFromFile,
getSourceFileOfNode,
getStringComparer,
getSynthesizedDeepClone,
getTokenAtPosition,
getUniqueName,
Expand Down Expand Up @@ -429,7 +432,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);
Expand Down Expand Up @@ -581,7 +589,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) {
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/moveToFile_noSelfImports1.ts
Original file line number Diff line number Diff line change
@@ -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" }
});
20 changes: 20 additions & 0 deletions tests/cases/fourslash/moveToFile_noSelfImports2.ts
Original file line number Diff line number Diff line change
@@ -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" }
});

0 comments on commit 8e69f41

Please sign in to comment.