diff --git a/src/services/refactors/helpers.ts b/src/services/refactors/helpers.ts index 8fc99f2794e..71885488c63 100644 --- a/src/services/refactors/helpers.ts +++ b/src/services/refactors/helpers.ts @@ -75,18 +75,18 @@ export function addTargetFileImports( * 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). * So in that case, fall back to copying the import verbatim. */ - importsToCopy.forEach(([isValidTypeOnlyUseSite, declaration], symbol) => { - const targetSymbol = skipAlias(symbol, checker); - if (checker.isUnknownSymbol(targetSymbol)) { - importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement))); - } - else if (targetSymbol.parent === undefined && (targetSymbol.flags & SymbolFlags.Module)) { - Debug.assert(declaration !== undefined, "expected module symbol to have a declaration"); - importAdder.addImportForModuleSymbol(symbol, isValidTypeOnlyUseSite, declaration); - } - else { - importAdder.addImportFromExportedSymbol(targetSymbol, isValidTypeOnlyUseSite, declaration); - } + importsToCopy.forEach(([isValidTypeOnlyUseSite, declaration], symbol) => { + const targetSymbol = skipAlias(symbol, checker); + if (checker.isUnknownSymbol(targetSymbol)) { + importAdder.addVerbatimImport(Debug.checkDefined(declaration ?? findAncestor(symbol.declarations?.[0], isAnyImportOrRequireStatement))); + } + else if (targetSymbol.parent === undefined && (targetSymbol.flags & SymbolFlags.Module)) { + Debug.assert(declaration !== undefined, "expected module symbol to have a declaration"); + importAdder.addImportForModuleSymbol(symbol, isValidTypeOnlyUseSite, declaration); + } + else { + importAdder.addImportFromExportedSymbol(targetSymbol, isValidTypeOnlyUseSite, declaration); + } }); addImportsForMovedSymbols(targetFileImportsFromOldFile, oldFile.fileName, importAdder, program); diff --git a/tests/cases/fourslash/moveToNewFileReactComponent.ts b/tests/cases/fourslash/moveToNewFileReactComponent.ts deleted file mode 100644 index d57027447d2..00000000000 --- a/tests/cases/fourslash/moveToNewFileReactComponent.ts +++ /dev/null @@ -1,39 +0,0 @@ -/// - -// @jsx: react - -// @Filename: /Component.tsx -////import React from 'react'; -////import { useState } from 'react'; -//// -////export const ComponentA = () => { -//// const [count, setCount] = useState(0); -//// return
setCount(count + 1)}>Component A: {count}
; -////}; -//// -////[|export const ComponentB = () => { -//// const [text, setText] = useState('Hello'); -//// return
setText(e.target.value)} />{text}
; -////};|] - -verify.moveToNewFile({ - newFileContents: { - "/Component.tsx": -`import React from 'react'; -import { useState } from 'react'; - -export const ComponentA = () => { - const [count, setCount] = useState(0); - return
setCount(count + 1)}>Component A: {count}
; -};`, - - "/ComponentB.tsx": -`import React from 'react'; -import { useState } from 'react'; - -export const ComponentB = () => { - const [text, setText] = useState('Hello'); - return
setText(e.target.value)} />{text}
; -};`, - }, -}); \ No newline at end of file diff --git a/tests/cases/fourslash/moveToNewFileSymbolWithoutParent.ts b/tests/cases/fourslash/moveToNewFileSymbolWithoutParent.ts new file mode 100644 index 00000000000..163a4a4aa19 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFileSymbolWithoutParent.ts @@ -0,0 +1,20 @@ +/// + +// Test for move to new file with symbols that don't have a parent but aren't modules +// This reproduces the scenario that caused the debug assertion failure + +// @Filename: /a.ts +////export const someVar = 42; +////[|export const anotherVar = 24;|] + +verify.moveToNewFile({ + newFileContents: { + "/a.ts": +`export const someVar = 42; +`, + + "/anotherVar.ts": +`export const anotherVar = 24; +`, + }, +}); \ No newline at end of file