question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: No `this` argument in array methods when using arrow functions

See original GitHub issue

Prevent specifying the this argument to array methods when using an arrow function, since it has no effect.

Applies to the following instance methods:

map
filter
some
find
forEach
findIndex
every

And also Array.from.

Only Array.from can be safely auto-fixed. The others should use suggestions.

Fail

array.filter(x => x === 1, this);

Pass

array.filter(x => x === 1);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
fiskercommented, Jan 28, 2021

A more generic solution, enfore array method arguments length, takes a number or range.

{
	slice: [1],      // At least one argument, forbid `.slice()` to copy array
	reduce: 2,       // Enfore inital value for `.reduce()`
	map: 1,          // For the example in the topic, forbid use this argument
	concat: [0, 5],  // Allow concat at most 5 arrays (just an example)
}

This rule may not only work for array, we can also make it work for any method.

For example:

// eslint-xxx {"is": 2, "parserInt": 2}

Object.is(foo) // Fail, missing the second value to compare
parseInt(foo)  // Fail, missing the radix
1reaction
sindresorhuscommented, Jan 22, 2021

How about no-array-method-this-argument?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Arrow function expressions - JavaScript - MDN Web Docs
An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate ...
Read more >
Javascript Arrays And Arrow Functions - w Blog
Since Arrow functions lack of the prototype property, they cannot be used as constructors. Depending on how functions have been declared, they ...
Read more >
3 Scenarios Where You Shouldn't Use Arrow Functions | by Tom
Here are 3 scenarios where arrow functions should not be used. ... Since the arrow function has a short syntax, it's inviting to...
Read more >
Arrow Functions · Styleguide JavaScript
If the function body consists of a single statement returning an expression without side effects, omit the braces and use the implicit return....
Read more >
Why shouldn't JSX props use arrow functions or bind?
Using an inline arrow function will cause PureComponent s, and components that use shallowCompare in the shouldComponentUpdate method to rerender anyway. Since ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found