useArrowFunction
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/complexity/useArrowFunction¥Diagnostic Category:
lint/complexity/useArrowFunction -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
¥Inspired from
prefer-arrow-callback
-
¥How to configure
{ "linter": { "rules": { "complexity": { "useArrowFunction": "error" } } }}¥Description
在函数表达式上使用箭头函数。
¥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
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 │
¥Valid
const f = function() { return this.prop;}忽略命名函数表达式:
¥Named function expressions are ignored:
const z = function z() { return 0;}引用 参数对象 的函数将被忽略,因为箭头函数无法访问参数对象。
¥Functions that reference the arguments object are ignored because the arguments object is not available to arrow functions.
const q = function () { return arguments[0];}声明 this 类型的函数表达式也会被忽略:
¥Function expressions that declare the type of this are also ignored:
const z = function(this: A): number { return 0;}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号