Skip to content

Commit 6eb7cb8

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

2 files changed

Lines changed: 45 additions & 19 deletions

File tree

src/services/refactors/helpers.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,24 @@ 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-
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-
}
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+
// If the target symbol has no parent but isn't a module, 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+
}
9796
});
9897

9998
addImportsForMovedSymbols(targetFileImportsFromOldFile, oldFile.fileName, importAdder, program);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// Test case to reproduce the debug assertion failure
4+
// When moving symbols that don't have a parent but aren't modules
5+
// This reproduces the scenario with symbols exported separately from declaration
6+
7+
// @Filename: /lib.ts
8+
////const Component = function() { return "component"; };
9+
////export { Component };
10+
11+
// @Filename: /main.ts
12+
////import { Component } from "./lib";
13+
////[|function useComponent() {
14+
//// return Component();
15+
////}|]
16+
17+
verify.moveToNewFile({
18+
newFileContents: {
19+
"/main.ts": ``,
20+
"/useComponent.ts": `import { Component } from "./lib";
21+
22+
function useComponent() {
23+
return Component();
24+
}
25+
`
26+
}
27+
});

0 commit comments

Comments
 (0)