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

no-unused-variable -- Only trigger on last function parameter? #1101

Closed
ghost opened this issue Apr 9, 2016 · 4 comments
Closed

no-unused-variable -- Only trigger on last function parameter? #1101

ghost opened this issue Apr 9, 2016 · 4 comments

Comments

@ghost
Copy link

ghost commented Apr 9, 2016

Regarding the "no-unused-variable" rule:

I'd love to keep this rule set to true, but it triggers too often in some patterns that I use often. Example below.

class ParentClass {
  constructor(callback:Function) {
    this.callback = callback;
  }
  name:string = 'parentName';
  title:string = 'parentTitle';
  body:string = 'parentBody';
  callback:Function;
  executeCallback():void {
    this.callback(this.name, this.title, this.body);
  }
  setCallback(callback:Function):void {
    this.callback = callback;
  }
}

function bodyLogger(name:string, title:string, body:string):void {
  console.log(body);
}

const instance = new ParentClass(bodyLogger);
instance.setCallback(bodyLogger);
instance.executeCallback();

The line function bodyLogger(name:string, title:string, body:string):void { triggers the "no-unused-variable" rule, because neither name, nor title are used in the function body. Since I use the body parameter, though, I can't just remove them.

I'd like for this pattern to be allowed somehow -- perhaps by adding an option to only trigger "no-unused-variable" on the last parameter of a function declaration?

@adidahiya
Copy link
Contributor

looks like you want the ignore-pattern option for the no-unused-variable rule (#981). you can use it to set up a naming scheme for variables in your code which are allowed to be unused.

@ghost
Copy link
Author

ghost commented Apr 9, 2016

Oh boy, I had overlooked just renaming the unused parameters as a workaround, that will definitely work. Goodness. Appreciate the response!

I still think it could be useful to have an option to turn the mentioned functionality on universally. Otherwise you're stuck renaming variables and potentially breaking naming conventions to avoid hitting the rule. Eslint has this functionality in their "no-unused-vars" rule as the "after-used" argument.

  • after-used only the last argument must be used. This allows you, for instance, to have two named parameters to a function and as long as you use the second argument, ESLint will not warn you about the first. This is the default setting.

https://github.com/eslint/eslint/blob/master/docs/rules/no-unused-vars.md

@orkisz
Copy link

orkisz commented Nov 14, 2016

after-used is a nice to have feature. Is there a plan to implement it?

@adidahiya
Copy link
Contributor

We have deprecated the no-unused-variable rule. Additional rule features are unlikely to get implemented. See https://github.com/palantir/tslint/releases/tag/4.0.0-dev.1 and the linked threads from #1481

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

No branches or pull requests

2 participants