10

How do i remove un-used imports from all my files in Visual Studio Code, my warning console looks like this

Warning Console

I have tried useing shift+alt+o but that only removes imports from the current file

Gama11
  • 31,714
  • 9
  • 78
  • 100
Malik Bagwala
  • 2,695
  • 7
  • 23
  • 38
  • You haven't tried going through it manually and fixing each issue? – rrd Jul 25 '19 at 11:29
  • Similar question:- https://stackoverflow.com/questions/46722701/is-there-a-way-to-remove-unused-imports-and-declarations-from-angular-2 – Aman Gupta Jul 25 '19 at 11:35
  • @AmanGupta Like i said `shift+alt+o` only removes the unused imports from the **Current** file – Malik Bagwala Jul 25 '19 at 11:54
  • @rrd it is really not feasable when you have like 20 different files, a lot of time is wasted over a period of time – Malik Bagwala Jul 25 '19 at 11:55
  • 1
    Even going through 100 files and pressing shift-alt-o is unlikely to take more than 5 minutes, and you only need to do it once ... not sure what the problem is ... ? – Jed Richards Jul 25 '19 at 13:02

1 Answers1

8

There is an eslint plugin for that. It works fine with me.

It's called eslint-plugin-unused-imports.

In the file for eslint configuration:

"plugins": ["unused-imports"],
"rules": {
    "no-unused-vars": "off", // or "@typescript-eslint/no-unused-vars": "off",
    "unused-imports/no-unused-imports": "error",
    "unused-imports/no-unused-vars": [
        "warn",
        { "vars": "all", "varsIgnorePattern": "^_", "args": "after-used", "argsIgnorePattern": "^_" }
    ]
}

Then, on your vscode config:

"editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
},

I hope it helps.

leotuna
  • 378
  • 3
  • 11