tslint-consistent-codestyle icon indicating copy to clipboard operation
tslint-consistent-codestyle copied to clipboard

no-unused: option to add matcher to ignore

Open nathanmarks opened this issue 8 years ago • 3 comments
trafficstars

Just a suggestion... similar to no-unused-variable:

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

This allows you to use things like _ for array destructuring in parameters to skip no-unused for them.

nathanmarks avatar Sep 07 '17 16:09 nathanmarks

I thought about allowing underscore prefixed variables in array destructuring (similar to object destructuring). But in array destructuring you can simply omit the variable completely. That's why I decided not to allow them:

const [_one, two, _three, _four, five] = arr;
console.log(two, five);
// can be simplified to
const [, two, , five] = arr;
console.log(two, five);

I'd like to hear your opinion why this is not the way to go for you.

ajafff avatar Sep 07 '17 16:09 ajafff

I'm interested in this. When I destructure an array I like to name all the values even if I don't use them, for clarity. Example:

function findByKey(haystack: Array<[string, string]>, needle: string) {
  const [_key, value] = haystack.find(([k, _v]) => k === needle);
  return value;
}

A version where you just omit the unused bindings is not as clear:

function findByKey(haystack: Array<[string, string]>, needle: string) {
  const [, value] = haystack.find(([k]) => k === needle);
  return value;
}

Funnily enough, naming-convention can already do this indirectly by setting an unused regex with an underscore, but that feels too rube-goldberg for me to feel okay introducing it at work.

whatisaphone avatar Feb 19 '19 00:02 whatisaphone

So the proposal here is to add an option to ignore all underscore-prefixed variables, parameters and destructuring bindings?

IMO that sounds reasonable. If someone wants to contribute, please send a PR.

ajafff avatar Feb 19 '19 18:02 ajafff