useArrowFunction
诊断类别:lint/complexity/useArrowFunction
¥Diagnostic Category: lint/complexity/useArrowFunction
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
-
¥Inspired from:
prefer-arrow-callback
在函数表达式上使用箭头函数。
¥Use arrow functions over function expressions.
箭头函数表达式是正则函数表达式的紧凑替代,但有一个重要区别:this
不绑定到箭头函数。它从其父范围继承 this
。
¥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.
此规则建议将所有不是生成器(function*
)且不使用 this
的函数表达式转换为箭头函数。
¥This rule proposes turning all function expressions that are not generators (function*
) and don’t use this
into arrow functions.
¥Examples
¥Invalid
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 │
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
忽略命名函数表达式:
¥Named function expressions are ignored:
声明 this
类型的函数表达式也会被忽略:
¥Function expressions that declare the type of this
are also ignored:
¥Related links