Fix move to file refactor issue with React components

Added proper check for SymbolFlags.Module before calling addImportForModuleSymbol to prevent debug assertion failure when moving non-module symbols like React components.

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-07-14 18:31:31 +00:00
parent 917e69c247
commit 05666fc9fb
2 changed files with 25 additions and 20 deletions

View File

@@ -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) {
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);

View File

@@ -4,31 +4,36 @@
// @Filename: /Component.tsx
////import React from 'react';
////import { useState } from 'react';
////
////export const ComponentA = () => {
//// return <div>Component A</div>;
//// const [count, setCount] = useState(0);
//// return <div onClick={() => setCount(count + 1)}>Component A: {count}</div>;
////};
////
////[|export const ComponentB = () => {
//// return <div>Component B</div>;
//// const [text, setText] = useState('Hello');
//// return <div><input value={text} onChange={(e) => setText(e.target.value)} /><span>{text}</span></div>;
////};|]
verify.moveToNewFile({
newFileContents: {
"/Component.tsx":
`import React from 'react';
import { useState } from 'react';
export const ComponentA = () => {
return <div>Component A</div>;
};
`,
const [count, setCount] = useState(0);
return <div onClick={() => setCount(count + 1)}>Component A: {count}</div>;
};`,
"/ComponentB.tsx":
`import React from 'react';
import { useState } from 'react';
export const ComponentB = () => {
return <div>Component B</div>;
};
`,
const [text, setText] = useState('Hello');
return <div><input value={text} onChange={(e) => setText(e.target.value)} /><span>{text}</span></div>;
};`,
},
});