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.

Add a --fix option for prefer-arrow-callback

See original GitHub issue

I’m using Node v6.4.0 and ESLint v3.4.0.

It would be useful to have an autofix option for the prefer-arrow-callback rule. The fixer would attempt to convert regular callback functions to arrow functions.

  • If the function does not use this, super, arguments, or new.target, and it is not explicitly bound, it can be replaced naively:
/* eslint prefer-arrow-callback: ["error"] */

[1, 2, 3].map(function(a, b, c) {
  return a * 2;
});

// gets fixed to:

[1, 2, 3].map((a, b, c) => {
  return a * 2;
});
  • If the function is explicitly bound with .bind(this), and it does not use super, arguments, or new.target, the binding can be removed and the function can be replaced:
/* eslint prefer-arrow-callback: ["error"] */

[1, 2, 3].map(function(a, b, c) {
  return a * 2;
}.bind(this));

// gets fixed to:

[1, 2, 3].map((a, b, c) => {
  return a * 2;
});

// ---

[1, 2, 3].map(function(a, b, c) {
  return a * this.foo;
}.bind(this));

// gets fixed to:

[1, 2, 3].map((a, b, c) => {
  return a * this.foo;
});

  • If the option {allowUnboundThis: false} is used, and the function includes this without being explicitly bound, the problem should not get fixed, since there is no way to be sure of the correct this value:
/* eslint prefer-arrow-callback: ["error", {allowUnboundThis: false}] */
[1, 2, 3].map(function(a, b, c) {
  return a * this.foo;
});

// (Reports an error, but is not autofixed)
  • If the function uses super, arguments, or new.target, the rule does not report any problems, so nothing gets fixed.
/* eslint prefer-arrow-callback: ["error"] */

[1, 2, 3].map(function(a, b, c) {
  return arguments[0] * 5;
});

// (no errors are reported, so no fixes are performed)
  • To avoid SyntaxErrors, the fixer should not fix anything unless ES6 parsing is enabled.

Other things to consider:

  • If the {allowNamedFunctions: false} option is used and a function has a name, should the function still be changed when autofixing? (This doesn’t break recursive functions, since those are ignored by prefer-arrow-callback anyway.) I think the fixer should still fix these functions; if people want to allow named functions, then they should set {allowNamedFunctions: true}.

I would be willing to add this if the issue is accepted.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Aug 28, 2016

but this would be a massive fix (in terms of characters of source code affected per fix)

Note that this is only a massive fix if .bind(this) is used; otherwise, the fix would just replace function (a, b, c) with (a, b, c) => and leave the rest of the function untouched.

0reactions
vitorbalcommented, Aug 29, 2016

I’ll 👍 this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-arrow-callback - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
ESLint prefer-arrow-callback error - Stack Overflow
It is basically saying to use Arrow function syntax in the filter callback function. $productItem.filter((i, el) => el.
Read more >
eslint-plugin-prefer-arrow-functions - npm
Convert functions to arrow functions. Latest version: 3.1.4, last published: a year ago. Start using eslint-plugin-prefer-arrow-functions in ...
Read more >
prefer-arrow-callback - ESLint Config
This rule locates function expressions used as callbacks or function arguments. An error will be produced for any that could be replaced by...
Read more >
Arrow function expressions - JavaScript - MDN Web Docs
An arrow function expression is a compact alternative to a traditional function ... To fix this, wrap the object literal in parentheses:.
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