Is it possible to remove unused imports with Prettier?
Asked Answered
A

5

29

I have an Expo React Native app that has a ton of unused imports in components.

I have Prettier setup for code formatting, is it possible to configure Prettier to also remove any unused imports across the project?

I can't see anything in the docs and I don't know if there is a way to add extra eslint plugins to Prettier.

Apodal answered 26/6, 2022 at 10:45 Comment(2)
Does this answer your question? Is there a way to remove unused imports and declarations from Angular 2+?Shellfish
@abolfazlshamsollahi No it doesn't because I don't use VSCodeApodal
S
20

I don't think this is supported by default.

But you can use this prettier-plugin https://www.npmjs.com/package/prettier-plugin-organize-imports

Steffen answered 26/6, 2022 at 12:20 Comment(1)
Great tip! Very simple to install and no configuration needed. Thanks!Adalai
J
40

You can use VS Code .vscode/settings.json to organize imports.

"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
  "source.organizeImports": true
}
Justification answered 2/4, 2023 at 22:22 Comment(1)
2024 UPDATE: As per VS Code, "source.organizeImports": true changed to "source.organizeImports": "explicit" and instead of false now it's never. check the link for more details.Kopaz
S
20

I don't think this is supported by default.

But you can use this prettier-plugin https://www.npmjs.com/package/prettier-plugin-organize-imports

Steffen answered 26/6, 2022 at 12:20 Comment(1)
Great tip! Very simple to install and no configuration needed. Thanks!Adalai
R
2
 "editor.codeActionsOnSave": {
    "source.organizeImports": "always",
    "source.fixAll": "explicit"
  },

You need to add these lines in your settings.json

Robyn answered 8/4 at 11:45 Comment(0)
H
0

Building off what @QuickSort mentioned above, this is what we did using the Prettier sdk:

prettier.format(text, {
    plugins: ['prettier-plugin-organize-imports']
  })
Hangman answered 21/4 at 20:22 Comment(0)
T
0

works with yarn

As @QuickSort mentioned, you can use prettier-plugin-organize-imports. Install plugin, add this to your .prettierrc.cjs file:

module.exports = {
    plugins: [require.resolve('prettier-plugin-organize-imports')]
};

and run prettier.

Transmigrant answered 28/7 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.