kcmr / vsce-remove-unused-imports

VS Code extension to remove unused ES6 imports inside JavaScript and TypeScript files
https://marketplace.visualstudio.com/items?itemName=kuscamara.remove-unused-imports
MIT License
16 stars 5 forks source link

Extension removes global React from imports #19

Open muerwre opened 2 years ago

muerwre commented 2 years ago

In order to write React components you need to import React from react.

This extension removes it, when optimizing imports.

kcmr commented 2 years ago

I need to detect if you are using the new JSX transform that doesn't require importing React or not. The native "Organize Imports" from VS Code detects this when you are in a project, so I suppose it is possible. Another option would be to add a new setting to exclude some imports that could be set per project. Let me think about this.

In the meantime you can use React in your code (React.useState, etc.) as a temporary workaround to prevent this behavior.

Sanford284 commented 2 years ago

I have the same question. Maybe add a new setting to exclude some words is a better way :)

the temporary way I used is creating a global snipptes.

kcmr commented 2 years ago

I started working on this but I haven't had time to finish. My idea for this is to work the same than native VS Code Organize Imports, that is React import is removed or not depending on the jsx field in jsconfig.json or tsconfig.json of the project. See this issue.

So, if you have react as the value for jsx in compilerOptions, the import won't be removed:

{
  "compilerOptions": {
    "jsx": "react"
  }
}

For me, this is the most convenient because the desired behavior depends on the project settings (if you are using or not the new JSX transform). If the behavior depends on an extension setting, then you'll probably need to add a workspace settings per project (.vscode/settings.json). The other way is transparent for the user and takes advantage of the existing compilation settings for other tools (tsc).

savissimo commented 1 year ago

Hello! Any news on this?

kcmr commented 1 year ago

@savissimo sorry, not yet. I'll have some free time in August to continue working on this. I'm also open to accept collaborations to fix this issue.

timwhite06 commented 8 months ago

Would it be possible to have a .config.json to include imports that you do not want automatically deleting?