useConsistentArrowReturn
¥Summary
-
规则生效日期:
v2.2.3¥Rule available since:
v2.2.3 -
诊断类别:
lint/nursery/useConsistentArrowReturn¥Diagnostic Category:
lint/nursery/useConsistentArrowReturn -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
与
arrow-body-style相同¥Same as
arrow-body-style
-
¥How to configure
{ "linter": { "rules": { "nursery": { "useConsistentArrowReturn": "error" } } }}¥Description
强制箭头函数体保持一致。
¥Enforce consistent arrow function bodies.
此规则强制要求当函数体仅包含一个 return 语句时,必须使用不带函数体代码块的箭头函数。以下情况不报告:
¥This rule enforces the use of arrow functions with no body block when the function body consists of a single return statement. This rule does not report when:
-
函数体包含指令(例如
"use strict"),或者¥the function body contains directives (e.g.
"use strict"), or -
正文(或其子元素)包含注释,或者
¥the body (or its descendants) contain comments, or
-
单个
return没有参数 (return;)。¥the single
returnhas no argument (return;).
该修复会在需要时(例如对象字面量和序列表达式)将表达式用括号括起来以确保正确性。
¥The fix wraps expressions in parentheses when required for correctness (e.g. object literals and sequence expressions).
¥Examples
¥Invalid
const bar = () => { return { bar: { foo: 1, bar: 2, } }; };code-block.js:1:14 lint/nursery/useConsistentArrowReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The body of this arrow function contains a single return statement.
> 1 │ const bar = () => {
│ ^^^^^^^
> 2 │ return {
> 3 │ bar: {
> 4 │ foo: 1,
> 5 │ bar: 2,
> 6 │ }
> 7 │ };
> 8 │ };
│ ^
9 │
ℹ Safe fix: Remove the return statement.
1 │ - ·const·bar·=·()·=>·{
2 │ - ·····return·{
1 │ + ·const·bar·=·()·=>·({
3 2 │ bar: {
4 3 │ foo: 1,
5 4 │ bar: 2,
6 5 │ }
7 │ - ·····};
8 │ - ·};
6 │ + ·····});
9 7 │
¥Valid
const foo = () => 0;const bar = () => { "use strict"; return 1 }const baz = () => { /* intentional */ return x }const qux = () => ({ a: 1 }) // already concise with parens¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号