I've got a problem with ESLint
Here is my function:
$productItem.filter(function (i, el) {
return el.getBoundingClientRect().top < evt.clientY
}).last()
.after($productItemFull)
And Here is what ESLint tell me:
warning Missing function expression name func-names
error Unexpected function expression prefer-arrow-callback
How to solve this error?
I've got a problem with ESLint
Here is my function:
$productItem.filter(function (i, el) {
return el.getBoundingClientRect().top < evt.clientY
}).last()
.after($productItemFull)
And Here is what ESLint tell me:
warning Missing function expression name func-names
error Unexpected function expression prefer-arrow-callback
How to solve this error?
Share edited Jul 6, 2016 at 11:21 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Jul 6, 2016 at 10:26 StanouuStanouu 1301 silver badge8 bronze badges2 Answers
Reset to default 7It is basically saying to use Arrow function syntax in the filter
callback function.
$productItem.filter((i, el) => el.getBoundingClientRect().top < evt.clientY)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.last()
.after($productItemFull);
Here's what ESLINT documentation for prefer-arrow-callback
says
Arrow functions are suited to callbacks, because:
this
keywords in arrow functions bind to the upper scope’s.The notation of the arrow function is shorter than function expression’s.
And, in following cases the error will be thrown
/*eslint prefer-arrow-callback: "error"*/ foo(function(a) { return a; }); foo(function() { return this.a; }.bind(this));
Your code is same same as the first snippet. So, the error prefer-arrow-callback
is shown by ESLint.
To solve the error, you can
use Arrow function syntax(as shown above)
Suppress the error by using options and named function
/*eslint prefer-arrow-callback: ["error", { "allowNamedFunctions": true }]*/ foo(function bar() {});
to solve "unexpected function expression. (prefer-allow-callback)" error in eslint write this code in the beginning of your code:
/*eslint prefer-arrow-callback: 0*/