arrow-body-style
箭头函数体周围需要大括号
箭头函数的函数体有两种语法形式。它们可以定义为 块 体(用大括号表示)() => { ... },也可以定义为单个表达式 () => ...,其值会被隐式返回。
🌐 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"强制在函数体周围使用大括号"as-needed"强制在可以省略大括号的地方不使用大括号(默认)"never"不强制在函数体周围使用大括号(限制箭头函数只能返回一个表达式)
第二个是一个对象,用于在第一个选项为 "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"]*/
const foo = () => 0;
使用 "always" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always" option:
/*eslint arrow-body-style: ["error", "always"]*/
const foo = () => {
return 0;
};
const 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"]*/
const foo = () => {
return 0;
};
const 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"]*/
const foo1 = () => 0;
const foo2 = (retv, name) => {
retv[name] = true;
return retv;
};
const foo3 = () => ({
bar: {
foo: 1,
bar: 2,
}
});
const foo4 = () => { bar(); };
const foo5 = () => {};
const foo6 = () => { /* do nothing */ };
const foo7 = () => {
// do nothing.
};
const foo8 = () => ({ bar: 0 });
requireReturnForObjectLiteral
该选项仅在与
"as-needed"选项结合使用时适用。
使用 { "requireReturnForObjectLiteral": true } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "requireReturnForObjectLiteral": true } option:
/*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/
const foo = () => ({});
const 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 }]*/
const foo = () => {};
const bar = () => { return { bar: 0 }; };
never
使用 "never" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "never" option:
/*eslint arrow-body-style: ["error", "never"]*/
const foo = () => {
return 0;
};
const 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"]*/
const foo = () => 0;
const bar = () => ({ foo: 0 });
版本
此规则是在 ESLint v1.8.0 中引入。