Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(56127): TypeScript "Move to File" refactor causes self import #56294

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function doChange(context: RefactorContext, oldFile: SourceFile, targetFile: str
else {
const targetSourceFile = Debug.checkDefined(program.getSourceFile(targetFile));
const importAdder = codefix.createImportAdder(targetSourceFile, context.program, context.preferences, context.host);
getNewStatementsAndRemoveFromOldFile(oldFile, targetSourceFile, getUsageInfo(oldFile, toMove.all, checker, getExistingImports(targetSourceFile, checker)), changes, toMove, program, host, preferences, importAdder);
getNewStatementsAndRemoveFromOldFile(oldFile, targetSourceFile, getUsageInfo(oldFile, toMove.all, checker, getExistingLocals(targetSourceFile, toMove.all, checker)), changes, toMove, program, host, preferences, importAdder);
}
}

Expand Down Expand Up @@ -996,7 +996,7 @@ function isPureImport(node: Node): boolean {
}

/** @internal */
export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[], checker: TypeChecker, existingTargetImports: ReadonlySet<Symbol> = new Set()): UsageInfo {
export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[], checker: TypeChecker, existingTargetLocals: ReadonlySet<Symbol> = new Set()): UsageInfo {
const movedSymbols = new Set<Symbol>();
const oldImportsNeededByTargetFile = new Map<Symbol, /*isValidTypeOnlyUseSite*/ boolean>();
const targetFileImportsFromOldFile = new Set<Symbol>();
Expand All @@ -1020,7 +1020,7 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
if (!symbol.declarations) {
return;
}
if (existingTargetImports.has(skipAlias(symbol, checker))) {
if (existingTargetLocals.has(skipAlias(symbol, checker))) {
unusedImportsFromOldFile.add(symbol);
return;
}
Expand Down Expand Up @@ -1243,8 +1243,8 @@ function getOverloadRangeToMove(sourceFile: SourceFile, statement: Statement) {
return undefined;
}

function getExistingImports(sourceFile: SourceFile, checker: TypeChecker) {
const imports = new Set<Symbol>();
function getExistingLocals(sourceFile: SourceFile, statements: readonly Statement[], checker: TypeChecker) {
const existingLocals = new Set<Symbol>();
for (const moduleSpecifier of sourceFile.imports) {
const declaration = importFromModuleSpecifier(moduleSpecifier);
if (
Expand All @@ -1254,18 +1254,27 @@ function getExistingImports(sourceFile: SourceFile, checker: TypeChecker) {
for (const e of declaration.importClause.namedBindings.elements) {
const symbol = checker.getSymbolAtLocation(e.propertyName || e.name);
if (symbol) {
imports.add(skipAlias(symbol, checker));
existingLocals.add(skipAlias(symbol, checker));
}
}
}
if (isVariableDeclarationInitializedToRequire(declaration.parent) && isObjectBindingPattern(declaration.parent.name)) {
for (const e of declaration.parent.name.elements) {
const symbol = checker.getSymbolAtLocation(e.propertyName || e.name);
if (symbol) {
imports.add(skipAlias(symbol, checker));
existingLocals.add(skipAlias(symbol, checker));
}
}
}
}
return imports;

for (const statement of statements) {
forEachReference(statement, checker, s => {
const symbol = skipAlias(s, checker);
if (symbol.valueDeclaration && getSourceFileOfNode(symbol.valueDeclaration) === sourceFile) {
existingLocals.add(symbol);
}
});
}
return existingLocals;
}
38 changes: 38 additions & 0 deletions tests/cases/fourslash/moveToFile_existingImports5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference path='fourslash.ts' />

// @filename: /a.ts
////export class Bar {}
////export class Baz {}

// @filename: /b.ts
////import { Bar, Baz } from "./a";
////
////[|const Foo = {
//// bar: Bar,
//// baz: Baz,
////}|]
////
////export function fn(name: keyof typeof Foo) {
//// return Foo[name];
////}

verify.moveToFile({
newFileContents: {
"/a.ts":
`export class Bar {}
export class Baz {}

export const Foo = {
bar: Bar,
baz: Baz,
};
`,
"/b.ts":
`import { Foo } from "./a";

export function fn(name: keyof typeof Foo) {
return Foo[name];
}`,
},
interactiveRefactorArguments: { targetFile: "/a.ts" },
});
Loading