You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I want to remove some invalid imports caused by code move and reimport from the new correct modules. Removing imports and fixMissingImports both works itself but combining them together would raise manipulation error in the following case.
Error:
Manipulation error: Error replacing tree: The children of the old and new trees were expected to have the same count (2:4).
Version: 12.2.0
To Reproduce
import{Project}from"ts-morph";constproject=newProject();project.createSourceFile("service.ts",`export const a = 100;`);project.createSourceFile("service.interface.ts",`export interface IA {}export class ClassB { public errorCode: string;}`);project.createSourceFile("service.mock.ts",`import { IA,} from "./service.interface";import { ClassB } from "./service";export class ClassC extends ClassB {}export class ClassA implements IA {}`);constqueue=[];project.getSourceFiles().forEach((sourceFile)=>{returnsourceFile.getImportDeclarations().forEach((declaration)=>{declaration.getNamedImports().forEach((namedImport)=>{if(!declaration.getModuleSpecifierValue().startsWith(".")){return;}constsourceFileImported=declaration.getModuleSpecifierSourceFile();if(sourceFileImported===undefined||!hasNamedExport(sourceFileImported,namedImport.getName())){queue.push(()=>{namedImport.remove();if(declaration.getNamedImports().length===0){declaration.remove();}declaration.getSourceFile().fixMissingImports();returndeclaration.getSourceFile().getFilePath();});}});});});constoptimizedFiles=queue.map((fn)=>fn());project.saveSync();console.log(optimizedFiles.join("\n"));functionhasNamedExport(sourceFile,namedExport: string): boolean{return[...sourceFile.getExportedDeclarations().keys()].some((name: string)=>name===namedExport);}
Expected behavior
It should work the same as I execute remove imports and fixMissingImports separately.
The text was updated successfully, but these errors were encountered:
Describe the bug
I want to remove some invalid imports caused by code move and reimport from the new correct modules. Removing imports and fixMissingImports both works itself but combining them together would raise manipulation error in the following case.
Error:
![image](https://user-images.githubusercontent.com/10512841/196725426-01ad9dda-09f1-45c2-9ff7-fc22ff20634d.png)
Manipulation error: Error replacing tree: The children of the old and new trees were expected to have the same count (2:4).
Version: 12.2.0
To Reproduce
Expected behavior
It should work the same as I execute remove imports and fixMissingImports separately.
The text was updated successfully, but these errors were encountered: