tslint autofix is not removing unused imports

Created on 4 Dec 2018  路  7Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__:5.11.0
  • __TypeScript version__:3.2.1
  • __Running TSLint via__: (pick one) CLI

TypeScript code being linted

Following is an unused import

import Actions from "../Actions/ActionTypeKeys"; 

with tslint.json configuration:
I didn't add no-unused-variable here since it was deprecated. I instead have this option in my tsconfig.json

   "noUnusedLocals": true /* Report errors on unused locals. */,

Actual behavior

When I run tslint -p ./tsconfig.json -c ./tslint.json --fix it doesn't autofix or even report the unused import in my file. Though the VSCode editor shows this as being an unused import:
image

Expected behavior

When I run tslint -p ./tsconfig.json -c ./tslint.json --fix it should auto remove the unused import.

External Question

Most helpful comment

the tslint --fix command doesn't remove the unused import

This is good and expected. TSLint and TypeScript are two separate tools. TypeScript is the language that provides syntax & type checking, compilation, and language services; TSLint uses APIs provided by TypeScript to run additional analysis. TypeScript features such as these suggested auto-fixes are managed completely within TypeScript. TSLint doesn't have any knowledge of them: running tslint --fix just fixes _TSLint_'s detected issues, not _TypeScript_'s.

How can I automate removal of such unused imports then?

Excellent question! I don't know that such a tool exists right now (which is part of why some folks are suggesting TSLint's no-unused-variable shouldn't be deprecated: #4100).

If you're looking for a way to apply all of TypeScript's suggestions across many files automatically, that's tracked in the TypeScript repo at https://github.com/Microsoft/TypeScript/issues/19255. As of https://github.com/Microsoft/TypeScript/issues/14549 there's a way to apply all in a file, but nothing yet for applying all in _all_ files.

If you have other questions or comments on this I'm happy to chat on https://gitter.im/palantir/tslint or Twitter!

All 7 comments

Hi @ajain17!

I didn't add no-unused-variable here since it was deprecated. I instead have this option in my tsconfig.json

  "noUnusedLocals": true /* Report errors on unused locals. */,

Yup, since you're not using the deprecated TSLint rule and relying on TypeScript's noUnusedLocals, TypeScript is what should be removing those imports. VS Code should give you a suggested action with the lightbulb to remove them:

image

This is powered by TypeScript, so if it's not working for you, you'll want to file an issue on them. Thanks!

@JoshuaKGoldberg the lightbulb option is there but the tslint --fix command doesn't remove the unused import. Is that the expected behavior while using the tsconfig? How can I automate removal of such unused imports then?

the tslint --fix command doesn't remove the unused import

This is good and expected. TSLint and TypeScript are two separate tools. TypeScript is the language that provides syntax & type checking, compilation, and language services; TSLint uses APIs provided by TypeScript to run additional analysis. TypeScript features such as these suggested auto-fixes are managed completely within TypeScript. TSLint doesn't have any knowledge of them: running tslint --fix just fixes _TSLint_'s detected issues, not _TypeScript_'s.

How can I automate removal of such unused imports then?

Excellent question! I don't know that such a tool exists right now (which is part of why some folks are suggesting TSLint's no-unused-variable shouldn't be deprecated: #4100).

If you're looking for a way to apply all of TypeScript's suggestions across many files automatically, that's tracked in the TypeScript repo at https://github.com/Microsoft/TypeScript/issues/19255. As of https://github.com/Microsoft/TypeScript/issues/14549 there's a way to apply all in a file, but nothing yet for applying all in _all_ files.

If you have other questions or comments on this I'm happy to chat on https://gitter.im/palantir/tslint or Twitter!

@JoshuaKGoldberg Since Microsoft/TypeScript#14549 is fixed now, there is typescript support for those auto fixing. Is there a change we could integrate it into tslint?

@scott-ho unlikely that that API would change much here. #4100's discussion is on how to enable using language service APIs altogether; the new API in that issue is another example of those.

@JoshuaKGoldberg

I agree with the other users here- the current way things are is absolutely _not good._

running ts-lint with the --fix option should remove unused imports.

@JimLynchCodes TSLint is deprecated, see #4534. Any feature requests you have for a lint rule to remove unused imports should be raised in typescript-eslint or eslint-plugin-import.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrand01 picture mrand01  路  3Comments

DanielKucal picture DanielKucal  路  3Comments

ghost picture ghost  路  3Comments

denkomanceski picture denkomanceski  路  3Comments

rajinder-yadav picture rajinder-yadav  路  3Comments