Add a --fix option for prefer-arrow-callback
See original GitHub issueI’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
, ornew.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 usesuper
,arguments
, ornew.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 includesthis
without being explicitly bound, the problem should not get fixed, since there is no way to be sure of the correctthis
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
, ornew.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 byprefer-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:
- Created 7 years ago
- Comments:7 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Note that this is only a massive fix if
.bind(this)
is used; otherwise, the fix would just replacefunction (a, b, c)
with(a, b, c) =>
and leave the rest of the function untouched.I’ll 👍 this!