prettier-plugin-organize-imports: 3.0.1 incorrectly deleting required imports from typescript files

Hi,

The 3.0.1 update seems to be incorrectly deleting required imports.

The following code demonstrates the issue:

If I have TypeScript:

import { NormalizedCacheObject } from 'apollo-cache-inmemory'
import AWSAppSyncClient from 'aws-appsync'

export class Foo {
  public constructor(private readonly appSyncClient: AWSAppSyncClient<NormalizedCacheObject>) {}
  
  getClient(): any {
    return this.appSyncClient
  }
}

with package.json

{
  "name": "scratch",
  "version": "1.0.0",
  "main": "index.js",
  "license": "UNLICENSED",
  "private": true,
  "scripts": {
    "prettier": "prettier --list-different \"**/*.{json,js,ts,yml,}\""
  },
  "resolutions": {
    "apollo-cache-inmemory": "^1.6.6"
  },
  "devDependencies": {
    "prettier": "^2.7.1",
    "prettier-plugin-organize-imports": "^3.0.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "apollo-cache-inmemory": "^1.6.6",
    "aws-appsync": "^4.1.7"
  }
}

then the import:

import AWSAppSyncClient from 'aws-appsync'

is flagged as needing to be deleted.

If I use 3.0.0 then the import is not flagged.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 18 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for replying, the performance regression is being tracked in #67 now. Should be able to get a fix out today.

We have a similar issue when trying to upgrade from 3.0.0 to 3.0.1. Here is a minimal reproduction:

  1. Create an app using npx create-react-app organize-bug --template typescript

  2. Install prettier: npm i prettier prettier-plugin-organize-imports

  3. src/models/Tip.ts

export type Tip = {
  text: string
}
  1. src/App.tsx
import { Tip as TipModel } from "./models/Tip"

export const Tip = () => {
    const logTip = (tip: TipModel) => console.log(tip)
    logTip({text: "hello world"})
}
  1. Running prettier with prettier-plugin-organize-imports removes import { Tip as TipModel } from "./models/Tip":
npx prettier --check .
Checking formatting...
[warn] src/App.tsx
[warn] Code style issues found in the above file. Forgot to run Prettier?

My issue is completely resolved with good performance with 3.0.3 - thamks @simonhaenisch !

Btw @askielboe, to not get the React import removed as unused, you’ll have to change the jsx option in tsconfig to react (see TS handbook). Or, if you leave it as react-jsx, it’s actually fine to have the import removed because it’s really not needed. I’ve tried and it builds and works fine without the React import.

Will do on monday