Skip to content
10 changes: 8 additions & 2 deletions src/services/refactors/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ export function addTargetFileImports(
importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement)));
}
else if (targetSymbol.parent === undefined) {
Debug.assert(declaration !== undefined, "expected module symbol to have a declaration");
importAdder.addImportForModuleSymbol(symbol, isValidTypeOnlyUseSite, declaration);
if (targetSymbol.flags & SymbolFlags.Module) {
Debug.assert(declaration !== undefined, "expected module symbol to have a declaration");
importAdder.addImportForModuleSymbol(symbol, isValidTypeOnlyUseSite, declaration);
}
else {
// If the target symbol has no parent but isn't a module, fall back to verbatim import
importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement)));
}
}
else {
importAdder.addImportFromExportedSymbol(targetSymbol, isValidTypeOnlyUseSite, declaration);
Expand Down
27 changes: 27 additions & 0 deletions tests/cases/fourslash/moveToNewFileSymbolWithoutParent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path='fourslash.ts' />

// Test case to reproduce the debug assertion failure
// When moving symbols that don't have a parent but aren't modules
// This reproduces the scenario with symbols exported separately from declaration

// @Filename: /lib.ts
////const Component = function() { return "component"; };
////export { Component };

// @Filename: /main.ts
////import { Component } from "./lib";
////[|function useComponent() {
//// return Component();
////}|]

verify.moveToNewFile({
newFileContents: {
"/main.ts": ``,
"/useComponent.ts": `import { Component } from "./lib";

function useComponent() {
return Component();
}
`
}
});
Loading