Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unused-variable: shouldn't warn about all params in some cases #1337

Closed
ChrisMBarr opened this issue Jun 24, 2016 · 8 comments
Closed

no-unused-variable: shouldn't warn about all params in some cases #1337

ChrisMBarr opened this issue Jun 24, 2016 · 8 comments

Comments

@ChrisMBarr
Copy link
Contributor

Bug Report

  • TSLint version: 3.11.0
  • TypeScript version: 1.8.10
  • Running TSLint via: gulp-tslint via Visual Studio 2015

TypeScript code being linted

this.$scope.$on("$myEvent", (event: angular.IAngularEvent, customerNumber: string) => {
    this.getCustomerInfo(customerNumber);
});

with tslint.json configuration:

"no-unused-variable": [ true, "check-parameters" ],

Actual behavior

The event parameter is reported as unused, which is technically correct... but it has to be there so I can access the 2nd parameter, which I need.

Right now I'm needing to put comments like // tslint:disable-next-line:no-unused-variable all over the place.

Expected behavior

Since I only need to use the 2nd parameter, it seems dumb to warn me about the first parameter not being used since this is how the framework I'm using works.

@jkillian
Copy link
Contributor

jkillian commented Jun 24, 2016

You can use the "ignore-pattern" option in this case to specify a regex that corresponds to variables that are intentionally unused. For example,

"no-unused-variable": [true, {"ignore-pattern": "^_"}]

would ignore all variables that start with an underscore and not report errors when they are unused.

Going to close this issue because "ignore-pattern" is designed exactly for this scenario!

@ChrisMBarr
Copy link
Contributor Author

so I'd have to go through my code and rename variables based on their parameter order just to satisfy this rule?

@jkillian
Copy link
Contributor

Yep, but it's not that unusual: starting unused variables with an underscore is a standard convention in languages with stricter compilers, like Go and Rust, as far as I know.

@ChrisMBarr
Copy link
Contributor Author

So if I were to rename that first var to _event and later on it was used by someone who was not aware of this convention, would that be flagged?

@jkillian
Copy link
Contributor

Good question - I believe the rule only works one way here. TSLint won't complain if you use a variable that meets the "ignore-pattern" option

@ChrisMBarr
Copy link
Contributor Author

Perhaps that could be a configurable option to add to this rule?

@marcoturi
Copy link

to me it is a bug, tslint should not throw error in this case.

@adidahiya
Copy link
Contributor

@marcoturi no-unused-variable is deprecated in favor of compiler options and we plan to remove it in the next major version, see #1481. I filed #1851 to address the sparse documentation around this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants