π Disallow using the this argument in array methods.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
The rule disallows using the thisArg argument in array methods:
- If the callback is an arrow function or a bound function, the
thisArgwon't affect it. - If you intend to use a custom
thisin the callback, it's better to use the variable directly or usecallback.bind(thisArg).
This rule checks the following array methods that accept thisArg:
Array.from()Array.fromAsync()Array#every()Array#filter()Array#find()Array#findLast()Array#findIndex()Array#findLastIndex()Array#flatMap()Array#forEach()Array#map()Array#some()
This rule is fixable when the callback is an arrow function, the thisArg argument has no side effect, and the fix can be applied without removing comments.
A receiver known not to be an array, such as a Set or a custom type that declares a same-named method, is ignored. Unknown receivers are still reported. A typed array is reported, since it takes the same thisArg parameter.
// β
const foo = bar.find(element => isUnicorn(element), baz);
// β
const foo = bar.find(element => isUnicorn(element));// β
const foo = bar.map(function (element) {
return this.unicorn(element);
}, baz);
// β
const foo = bar.map(element => {
return baz.unicorn(element);
});
// β
const foo = bar.map(function (element) {
return this.unicorn(element);
}.bind(baz));