Skip to content

Allow imports with the same names as global types to be moved by refactoring #60410

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

Closed
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
4 changes: 2 additions & 2 deletions src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
const unusedImportsFromOldFile = new Set<Symbol>();
for (const statement of toMove) {
forEachReference(statement, checker, enclosingRange, (symbol, isValidTypeOnlyUseSite) => {
if (!symbol.declarations || isGlobalType(checker, symbol)) {
if (!symbol.declarations) {
return;
}
if (existingTargetLocals.has(skipAlias(symbol, checker))) {
Expand All @@ -900,7 +900,7 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
tryCast(decl, (d): d is codefix.ImportOrRequireAliasDeclaration => isImportSpecifier(d) || isImportClause(d) || isNamespaceImport(d) || isImportEqualsDeclaration(d) || isBindingElement(d) || isVariableDeclaration(d)),
]);
}
else if (isTopLevelDeclaration(decl) && sourceFileOfTopLevelDeclaration(decl) === oldFile && !movedSymbols.has(symbol)) {
else if (!isGlobalType(checker, symbol) && isTopLevelDeclaration(decl) && sourceFileOfTopLevelDeclaration(decl) === oldFile && !movedSymbols.has(symbol)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is perfect but, at the very least, it's no worse than the previous version. The preceding branch already handles isInImport(decl) and those should always be moveable. So I just pushed the existing check further down the road so the preceding branch can kick in in the case that is being fixed here.

Copy link
Member

@iisaduan iisaduan Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewbranch What do you think of this vs #60173, I think it is the same bug

targetFileImportsFromOldFile.set(symbol, isValidTypeOnlyUseSite);
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/fourslash/moveToNewFile_importNameLikeGlobal1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />

// @lib: dom

// @Filename: /a.ts
//// export default class Event {}

// @Filename: /b.ts
//// import Event from './a.js';
//// [|export function test(test: Event): void {}|]

verify.noErrors();

verify.moveToNewFile({
newFileContents: {
"/b.ts": "",

"/test.ts":
`import Event from './a';

export function test(test: Event): void { }
`,
},
});