Skip to content

Bug: organizeImports Removes All Imports from the File #1624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wizardnet972 opened this issue Mar 28, 2025 · 0 comments
Open

Bug: organizeImports Removes All Imports from the File #1624

wizardnet972 opened this issue Mar 28, 2025 · 0 comments

Comments

@wizardnet972
Copy link

Description

When calling sourceFile.organizeImports(), all imports are removed, even those that might be necessary.

Reproduction

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)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant