Tslint: Start to deprecate no-unused-variable rule

Created on 13 Aug 2016  ·  30Comments  ·  Source: palantir/tslint

I just noticed these new compiler checks in v2.0: --noUnusedParameters and --noUnusedLocals. These essentially make the no-unused-variable rule obsolete. Since it's a very core rule, I don't think we should remove it right away.

Proposal:

  • remove no-unused-variable from tslint:latest & tslint:recommended in v4.0
  • mark the rule as deprecated in v4.0

    • maybe in the failure message?

  • remove the rule implementation completely in TSLint v5.0
Won't Fix Breaking Change

Most helpful comment

A small feedback. Having these rules in tslint is really convenient as you get the linter warning, but your code still compiles. I, personally, find it quite annoying to have the compiler failures because of unused variable, parameter or import especially when performing a quick code change like refactoring or trying out an idea.

All 30 comments

A good feature here is that the compiler provides an opt-out: give a variable name a leading underscore and the compiler won't flag it. This should make it easier for people who want to enable the complier checks but may have an edge-case or two which they don't want to change.

@JKillian That's only applicable to parameters, right?

@glen-84 - good catch, I think you're correct.

@adidahiya just to note that --noUnusedLocals and no-unused-variable operate quite differently.

The --noUnusedLocals checks class properties as well it seems (at TS version 2.0.2).

@charsleysa yeah, the implementation is slightly different. I think the compiler options are better than the TSLint rule, which is why I want to favor it. As noted in the original issue post, we'll remove it slowly from TSLint core.

I think you should remove it in version 4. There should be many preview releases where all the developers should see the deprecation warning. And you have removed other rules in favor of compiler options in anyway.

PD: In dev-1 release, I think this is as a recommended settings.

Just as a bit of feedback, I hit this issue tonight and even after finding this issue it took me a bit to understand that the --noUnusedLocals and --noUnusedParameters were tsc configs and not tslint. It might be useful to clarify that in the error message.

@ryanguill thanks for the feedback. I committed a change that clarifies the message

A small feedback. Having these rules in tslint is really convenient as you get the linter warning, but your code still compiles. I, personally, find it quite annoying to have the compiler failures because of unused variable, parameter or import especially when performing a quick code change like refactoring or trying out an idea.

The compiler handle this in a better way I think, and I support you in the way that it should not quite the compilation because of this, but if you use something like webpack, for example, the errors are going to be threw but the compilation won't stop.

To @zubivan

Agree. I prefer keeping this warning in the lint stage. My workflow is:

any file/code changes => run ts-compile => run tests => run ts-lint

I can make any dirty code change quickly and test the results first, and then fulfill the guard from tslint. Forcing you writing clean code before any testing is really annoying.

To @michaeljota

Although I prefer to show the warning after testing, is there any webpack example to achieve the goal?

I wonder if TSLint could just run the compiler, and parse and output its warnings as TSLint warnings? his would free TSLint from having to keep an implementation of the rule maintained but would let users to still be able to use the useful disable / ignore TSLint comments amongst other advantages.

@xareelee in my profile you can see an Angular 2 setup with TS compilation without throwing.

I don't understand why these are part of the compiler. They're purely stylistic, not affecting the correctness of the code, so they seem more natural as linter options. I too want the ability to override these warnings in certain cases, which I can do with tslint but not with tsc.

Why these are part of the compiler now, it's not something to discuss here. I don't think they will readd this rule, for reasons already exposed.

For everyone else searching for a solution on this because they don't want to use the compiler options due to the fact that they would prevent fast local dev (commenting out some code, etc. would fail the transpilation), you can do something like this:

const Rule = require('tslint/lib/rules/noUnusedVariableRule').Rule;

// Hide deprecation message
Rule.metadata.deprecationMessage = '';
Rule.metadata.ruleName = 'no-unused-variable-wrapped';

module.exports.Rule = Rule;

in your own rule noUnusedVariableWrapped.js file and then enable it via "no-unused-variable-wrapped": true.

I'd like to note that one thing lost with depending on the compiler rule and not the tslint rule is the ability to autofix and remove unused imports / variables.

Looks like there's a lot of interest in keeping this rule around. I went ahead and merged #2256, which un-deprecates the rule. It still won't be encouraged in the built-in configs, but it'll be available on an opt-in basis.

Seems like if you your tsconfig has "jsx": "react" then tslint should throw an error for missing the react import.

@ccorcos "missing import" is an entirely different error from "no unused variable". tsc ought to help you out there.

sounds good. im just not seeing this error in VSCode... :/

Hi @adidahiya ,

Thanks for taking this rule back. Question is, when will be released? As I can see is not yet included in 5.1.0.

@javier-tarazaga it is there in v5, just as it's always been. it's not enabled in the tslint:recommended configuration though.

Hi @adidahiya,

Uhm weird. When I include the rule in version of tslint 5.1.0 I get the following error,

Could not find implementations for the following rules specified in the configuration:
    no-unused-vars
Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.
If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.

That's because you used the wrong file name. It's no-unused-variable, not
no-unused-vars

Am 02.05.2017 09:33 schrieb "Javier Tarazaga" notifications@github.com:

Hi @adidahiya https://github.com/adidahiya,

Uhm weird. When I include the rule in version of tslint 5.1.0 I get the
following error,

Could not find implementations for the following rules specified in the configuration:
no-unused-vars
Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.
If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/palantir/tslint/issues/1481#issuecomment-298527837,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ALaeKKYydi8vQ8MAhxOwBk3uL02jgzKNks5r1txLgaJpZM4JjuQh
.

Thanks for the heads up! Now I am facing #2650 so I cannot really verify it.

I installed https://github.com/blakeembrey/tslint-config-standard
Is there anyway to get rid of these warnings, I'm not sure if is related to standard or just tslint

> [email protected] lint /Users/nikos/WebstormProjects/common
> tslint 'src/**/*.ts' 'test/**/*.ts'

Warning: The 'await-promise' rule requires type information.
Warning: The 'no-unused-variable' rule requires type information.
Warning: The 'no-use-before-declare' rule requires type information.
Warning: The 'return-undefined' rule requires type information.
Warning: The 'no-floating-promises' rule requires type information.
Warning: The 'no-unnecessary-qualifier' rule requires type information.
Warning: The 'no-unnecessary-type-assertion' rule requires type information.
Warning: The 'strict-type-predicates' rule requires type information.

@QuantumInformation Have you tried using --project?

Yeah that works thx

🤖 Beep boop! 👉 TSLint is deprecated 👈 and you should switch to typescript-eslint! 🤖

🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielKucal picture DanielKucal  ·  3Comments

CSchulz picture CSchulz  ·  3Comments

dashmug picture dashmug  ·  3Comments

avanderhoorn picture avanderhoorn  ·  3Comments

cateyes99 picture cateyes99  ·  3Comments