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

Move to file: fix detection of references to globals that shouldn't be moved #60450

Merged
merged 10 commits into from
Nov 15, 2024
28 changes: 12 additions & 16 deletions src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
emptyArray,
EnumDeclaration,
escapeLeadingUnderscores,
every,
ExportDeclaration,
ExportKind,
Expression,
Expand Down Expand Up @@ -885,24 +886,23 @@ 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))) {
unusedImportsFromOldFile.add(symbol);
return;
}
for (const decl of symbol.declarations) {
if (isInImport(decl)) {
const prevIsTypeOnly = oldImportsNeededByTargetFile.get(symbol);
oldImportsNeededByTargetFile.set(symbol, [
prevIsTypeOnly === undefined ? isValidTypeOnlyUseSite : prevIsTypeOnly && isValidTypeOnlyUseSite,
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)) {
targetFileImportsFromOldFile.set(symbol, isValidTypeOnlyUseSite);
}
const importedDeclaration = find(symbol.declarations, isInImport);
if (importedDeclaration) {
const prevIsTypeOnly = oldImportsNeededByTargetFile.get(symbol);
oldImportsNeededByTargetFile.set(symbol, [
prevIsTypeOnly === undefined ? isValidTypeOnlyUseSite : prevIsTypeOnly && isValidTypeOnlyUseSite,
tryCast(importedDeclaration, (d): d is codefix.ImportOrRequireAliasDeclaration => isImportSpecifier(d) || isImportClause(d) || isNamespaceImport(d) || isImportEqualsDeclaration(d) || isBindingElement(d) || isVariableDeclaration(d)),
]);
}
else if (!movedSymbols.has(symbol) && every(symbol.declarations, decl => isTopLevelDeclaration(decl) && sourceFileOfTopLevelDeclaration(decl) === oldFile)) {
targetFileImportsFromOldFile.set(symbol, isValidTypeOnlyUseSite);
}
});
}
Expand Down Expand Up @@ -946,10 +946,6 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
}
}

function isGlobalType(checker: TypeChecker, symbol: Symbol) {
return !!checker.resolveName(symbol.name, /*location*/ undefined, SymbolFlags.Type, /*excludeGlobals*/ false);
}

function makeUniqueFilename(proposedFilename: string, extension: string, inDirectory: string, host: LanguageServiceHost): string {
let newFilename = proposedFilename;
for (let i = 1;; i++) {
Expand Down
Loading