unicorn/no-array-method-this-argument
Disallow the thisArg argument of array methods such as map and filter
map, filter, and similar methods take a second argument that becomes this inside the callback. It comes after the callback, where it's easy to miss. With an arrow function it does nothing, because an arrow function keeps the this from the surrounding code.
The rule checks every, filter, find, findLast, findIndex, findLastIndex, flatMap, forEach, map, and some. It also checks the third argument of Array.from() and Array.fromAsync().
Configure#
Off by default. To turn it on:
[lint.rules]
"unicorn/no-array-method-this-argument" = "error" # or "warn"Examples#
const mine = items.filter(function (item) {Avoid using thisArg with array iteration methods
return item.owner === this.id;
}, user);const rows = items.map(table.renderRowAvoid using thisArg with array iteration methods, table);const cards = Array.from(nodes, function (node) {
return this.wrap(node);
}, viewAvoid using thisArg with array iteration methods);const mine = items.filter((item) => item.owner === user.id);
const rows = items.map((item) => table.renderRow(item));
const cards = Array.from(nodes, (node) => view.wrap(node));The rule skips a call if its first argument can't be a function. It also skips library methods with the same names, such as _.map(), R.map(), $.map(), and React.Children.map(), because their second argument means something else:
const match = collection.find("email", address);
const names = _.map(users, formatName);