跳转到内容

useArrowFunction

Diagnostic Category: lint/complexity/useArrowFunction

Since: v1.0.0

Sources:

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.

const z = function() {
return 0;
}
code-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

const delegatedFetch = async function(url) {
return await fetch(url);
}
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

const f = function() {
return this.prop;
}

Named function expressions are ignored:

const z = function z() {
return 0;
}

Function expressions that declare the type of this are also ignored:

const z = function(this: A): number {
return 0;
}