Skip to content

Commit ed65054

Browse files
Fix debug assertion failure in move to file refactor for symbols exported separately
Co-authored-by: RyanCavanaugh <[email protected]>
1 parent dbfbc0e commit ed65054

2 files changed

Lines changed: 19 additions & 37 deletions

File tree

src/services/refactors/helpers.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,25 @@ export function addTargetFileImports(
7575
* but sometimes it fails because of unresolved imports from files, or when a source file is not available for the target file (in this case when creating a new file).
7676
* So in that case, fall back to copying the import verbatim.
7777
*/
78-
importsToCopy.forEach(([isValidTypeOnlyUseSite, declaration], symbol) => {
79-
const targetSymbol = skipAlias(symbol, checker);
80-
if (checker.isUnknownSymbol(targetSymbol)) {
81-
importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement)));
82-
}
83-
else if (targetSymbol.parent === undefined) {
84-
if (targetSymbol.flags & SymbolFlags.Module) {
85-
Debug.assert(declaration !== undefined, "expected module symbol to have a declaration");
86-
importAdder.addImportForModuleSymbol(symbol, isValidTypeOnlyUseSite, declaration);
87-
}
88-
else {
89-
// For symbols without a parent that aren't modules, fall back to verbatim import
90-
importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement)));
91-
}
92-
}
93-
else {
94-
importAdder.addImportFromExportedSymbol(targetSymbol, isValidTypeOnlyUseSite, declaration);
95-
}
78+
importsToCopy.forEach(([isValidTypeOnlyUseSite, declaration], symbol) => {
79+
const targetSymbol = skipAlias(symbol, checker);
80+
if (checker.isUnknownSymbol(targetSymbol)) {
81+
importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement)));
82+
}
83+
else if (targetSymbol.parent === undefined) {
84+
Debug.assert(declaration !== undefined, "expected module symbol to have a declaration");
85+
const aliasedSymbol = checker.getAliasedSymbol(symbol);
86+
if (aliasedSymbol.flags & SymbolFlags.Module) {
87+
importAdder.addImportForModuleSymbol(symbol, isValidTypeOnlyUseSite, declaration);
88+
}
89+
else {
90+
// If the aliased symbol is not a module, fall back to verbatim import
91+
importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement)));
92+
}
93+
}
94+
else {
95+
importAdder.addImportFromExportedSymbol(targetSymbol, isValidTypeOnlyUseSite, declaration);
96+
}
9697
});
9798

9899
addImportsForMovedSymbols(targetFileImportsFromOldFile, oldFile.fileName, importAdder, program);

tests/cases/fourslash/moveToNewFileSymbolWithoutParent.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)