useArrowFunction
Diagnostic Category: lint/complexity/useArrowFunction
Since: v1.0.0
Sources:
- Inspired from:
prefer-arrow-callback
Use arrow functions over function expressions.
An arrow function expression is a compact alternative to a regular function expression,
with an important distinction:
this
is not bound to the arrow function. It inherits this
from its parent scope.
This rule proposes turning all function expressions that are not generators (function*
) and don’t use this
into arrow functions.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:11 lint/complexity/useArrowFunction FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This function expression can be turned into an arrow function.
> 1 │ const z = function() {
│ ^^^^^^^^^^^^
> 2 │ return 0;
> 3 │ }
│ ^
4 │
ℹ Function expressions that don’t use this can be turned into arrow functions.
ℹ Safe fix: Use an arrow function instead.
1 │ - const·z·=·function()·{
2 │ - ····return·0;
3 │ - }
1 │ + const·z·=·()·=>·0
4 2 │
code-block.js:1:24 lint/complexity/useArrowFunction FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This function expression can be turned into an arrow function.
> 1 │ const delegatedFetch = async function(url) {
│ ^^^^^^^^^^^^^^^^^^^^^
> 2 │ return await fetch(url);
> 3 │ }
│ ^
4 │
ℹ Function expressions that don’t use this can be turned into arrow functions.
ℹ Safe fix: Use an arrow function instead.
1 │ - const·delegatedFetch·=·async·function(url)·{
2 │ - ····return·await·fetch(url);
3 │ - }
1 │ + const·delegatedFetch·=·async·(url)·=>·await·fetch(url)
4 2 │
Valid
Section titled ValidNamed function expressions are ignored:
Function expressions that declare the type of this
are also ignored: