Skip to content

Commit dbfbc0e

Browse files
Revert code changes and create test case that triggers original assertion
Co-authored-by: RyanCavanaugh <[email protected]>
1 parent be81535 commit dbfbc0e

2 files changed

Lines changed: 32 additions & 27 deletions

File tree

src/services/refactors/helpers.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +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 && (targetSymbol.flags & SymbolFlags.Module)) {
84-
Debug.assert(declaration !== undefined, "expected module symbol to have a declaration");
85-
importAdder.addImportForModuleSymbol(symbol, isValidTypeOnlyUseSite, declaration);
86-
}
87-
else {
88-
importAdder.addImportFromExportedSymbol(targetSymbol, isValidTypeOnlyUseSite, declaration);
89-
}
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+
}
9096
});
9197

9298
addImportsForMovedSymbols(targetFileImportsFromOldFile, oldFile.fileName, importAdder, program);
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/// <reference path='fourslash.ts'/>
22

3-
// Test for move to new file with symbols that don't have a parent but aren't modules
4-
// This reproduces the scenario that caused the debug assertion failure
3+
// Test for the debug assertion failure with symbols exported separately from declaration
4+
// This reproduces the issue reported in #62029
55

6-
// @Filename: /a.ts
7-
////export const someVar = 42;
8-
////[|export const anotherVar = 24;|]
6+
// @Filename: /bar.ts
7+
////class Bar {}
8+
////
9+
////export default Bar;
910

10-
verify.moveToNewFile({
11-
newFileContents: {
12-
"/a.ts":
13-
`export const someVar = 42;
14-
`,
11+
// @Filename: /foo.ts
12+
////import Bar from './bar';
13+
////
14+
////[|function makeBar() {
15+
//// return new Bar();
16+
////}|]
1517

16-
"/anotherVar.ts":
17-
`export const anotherVar = 24;
18-
`,
19-
},
20-
});
18+
// Check that the refactor is available
19+
verify.applicableRefactorAvailableAtMarker("Move to a new file");

0 commit comments

Comments
 (0)