arrow-body-style
箭头函数体周围需要大括号
此规则报告的一些问题可通过 --fix
命令行选项自动修复
箭头函数的函数体有两种语法形式。它们可以用块体(用大括号表示)() => { ... }
或用单个表达式 () => ...
定义,其值被隐式返回。
¥Arrow functions have two syntactic forms for their function bodies. They may be defined with a block body (denoted by curly braces) () => { ... }
or with a single expression () => ...
, whose value is implicitly returned.
规则详情
¥Rule Details
此规则可以强制或禁止在箭头函数体周围使用大括号。
¥This rule can enforce or disallow the use of braces around arrow function body.
选项
¥Options
该规则有一个或两个选项。第一个是字符串,可以是:
¥The rule takes one or two options. The first is a string, which can be:
-
"always"
在函数体周围强制使用大括号¥
"always"
enforces braces around the function body -
"as-needed"
强制不强制可以省略大括号(默认)¥
"as-needed"
enforces no braces where they can be omitted (default) -
"never"
强制函数体周围没有大括号(将箭头函数限制为返回表达式的角色)¥
"never"
enforces no braces around the function body (constrains arrow functions to the role of returning an expression)
当第一个选项为 "as-needed"
时,第二个是用于更细粒度配置的对象。目前,唯一可用的选项是 requireReturnForObjectLiteral
,一个布尔属性。默认为 false
。如果设置为 true
,它需要大括号和对象字面量的显式返回。
¥The second one is an object for more fine-grained configuration when the first option is "as-needed"
. Currently, the only available option is requireReturnForObjectLiteral
, a boolean property. It’s false
by default. If set to true
, it requires braces and an explicit return for object literals.
"arrow-body-style": ["error", "always"]
always
使用 "always"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "always"
option:
/*eslint arrow-body-style: ["error", "always"]*/
let foo = () => 0;
使用 "always"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "always"
option:
/*eslint arrow-body-style: ["error", "always"]*/
let foo = () => {
return 0;
};
let bar = (retv, name) => {
retv[name] = true;
return retv;
};
as-needed
使用默认 "as-needed"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default "as-needed"
option:
/*eslint arrow-body-style: ["error", "as-needed"]*/
let foo = () => {
return 0;
};
let bar = () => {
return {
bar: {
foo: 1,
bar: 2,
}
};
};
使用默认 "as-needed"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default "as-needed"
option:
/*eslint arrow-body-style: ["error", "as-needed"]*/
let foo1 = () => 0;
let foo2 = (retv, name) => {
retv[name] = true;
return retv;
};
let foo3 = () => ({
bar: {
foo: 1,
bar: 2,
}
});
let foo4 = () => { bar(); };
let foo5 = () => {};
let foo6 = () => { /* do nothing */ };
let foo7 = () => {
// do nothing.
};
let foo8 = () => ({ bar: 0 });
requireReturnForObjectLiteral
此选项仅在与
"as-needed"
选项一起使用时适用。¥This option is only applicable when used in conjunction with the
"as-needed"
option.
使用 { "requireReturnForObjectLiteral": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "requireReturnForObjectLiteral": true }
option:
/*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
let foo = () => ({});
let bar = () => ({ bar: 0 });
使用 { "requireReturnForObjectLiteral": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "requireReturnForObjectLiteral": true }
option:
/*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
let foo = () => {};
let bar = () => { return { bar: 0 }; };
never
使用 "never"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "never"
option:
/*eslint arrow-body-style: ["error", "never"]*/
let foo = () => {
return 0;
};
let bar = (retv, name) => {
retv[name] = true;
return retv;
};
使用 "never"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "never"
option:
/*eslint arrow-body-style: ["error", "never"]*/
let foo = () => 0;
let bar = () => ({ foo: 0 });
版本
此规则是在 ESLint v1.8.0 中引入。