Bug: `organizeImports` Removes All Imports from the File
- Dominant language
- TypeScript
- Stars
- 6.2k
- Forks
- 238
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
#### Description
When calling `sourceFile.organizeImports()`, all imports are removed, even those that might be necessary.
#### Reproduction
```ts
import { Project, SourceFile } from 'ts-morph';
console.clear();
const project = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
strict: true,
target: 3, // ES2015
},
skipFileDependencyResolution: true,
});
const sourceText = `
import { Unused } from 'unused-module';
class MyComponent {
property: string = "hello";
method() {
console.log(property);
}
}
`;
const sourceFile = project.createSourceFile('MyComponent.ts', sourceText);
sourceFile.organizeImports();
console.log(sourceFile.getFullText());
```
#### Expected Behavior
Only unused imports should be removed, while necessary imports should be retained.
#### Actual Behavior
All imports are removed, even if they might be needed.
#### StackBlitz Reproduction
[[StackBlitz Example](https://stackblitz.com/edit/vitejs-vite-nqlj93qx?file=src%2Fmain.ts)](https://stackblitz.com/edit/vitejs-vite-nqlj93qx?file=src%2Fmain.ts)
#### Environment
- **ts-morph version**: ^25.0.1
- **TypeScript version**: ~5.7.2
#### Additional Context
This issue might be related to `skipFileDependencyResolution: true`, but even with it set to `false`, the problem persists.
Contributor guide
Research direction
Start with the sourceFile.organizeImports() entry point and run the TypeScript reproduction from the issue, comparing behavior with skipFileDependencyResolution enabled and disabled. Trace why the shown import is removed and verify the completed behavior against the expected rule that unused imports are removed while necessary imports remain.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100