版本

arrow-parens

要求箭头函数参数周围使用圆括号

🔧 可修复

此规则报告的某些问题可以通过 --fix 命令行 选项自动修复

重要提示

此规则在 ESLint v8.53.0 中已弃用。请使用 @stylistic/eslint-plugin-js 中的相应规则

了解更多

当箭头函数只有一个参数时,可以省略圆括号。在所有其他情况下,参数必须用圆括号括起来。此规则强制箭头函数中圆括号使用的一致性。

规则详情

此规则强制箭头函数参数周围使用圆括号,而无需考虑参数的个数。例如

// Bad
a => {}

// Good
(a) => {}

遵循这种风格将帮助您找到箭头函数 (=>),这些函数可能在预期进行比较(例如 >=)时被错误地包含在条件中。

// Bad
if (a => 2) {
}

// Good
if (a >= 2) {
}

该规则还可以配置为不鼓励在不需要圆括号时使用它们

// Bad
(a) => {}

// Good
a => {}

选项

此规则有一个字符串选项和一个对象选项。

字符串选项包括

  • "always"(默认)要求在所有情况下参数周围都使用圆括号。
  • "as-needed" 强制在可以省略圆括号的地方不使用圆括号。

"as-needed" 选项变体的对象属性

  • "requireForBlockBody": true 修改了 as-needed 规则,以便在函数体位于指令块中(用大括号包围)时要求使用圆括号。

always

使用默认 "always" 选项时,此规则的错误代码示例

在 Playground 中打开
/*eslint arrow-parens: ["error", "always"]*/

a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => a);
a(foo => { if (true) {} });

使用默认 "always" 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint arrow-parens: ["error", "always"]*/

() => {};
(a) => {};
(a) => a;
(a) => {'\n'}
a.then((foo) => {});
a.then((foo) => { if (true) {} });

If 语句

此选项的优点之一是它可以防止在条件语句中错误地使用箭头函数

var a = 1;
var b = 2;
// ...
if (a => b) {
 console.log('bigger');
} else {
 console.log('smaller');
}
// outputs 'bigger', not smaller as expected

if 语句的内容是一个箭头函数,而不是比较。

如果箭头函数是有意的,则应将其括在圆括号中以消除歧义。

var a = 1;
var b = 0;
// ...
if ((a) => b) {
 console.log('truthy value returned');
} else {
 console.log('falsy value returned');
}
// outputs 'truthy value returned'

以下是此行为的另一个示例

var a = 1, b = 2, c = 3, d = 4;
var f = a => b ? c: d;
// f = ?

f 是一个箭头函数,它接受 a 作为参数,并返回 b ? c: d 的结果。

应该像这样重写

var a = 1, b = 2, c = 3, d = 4;
var f = (a) => b ? c: d;

as-needed

使用 "as-needed" 选项时,此规则的错误代码示例

在 Playground 中打开
/*eslint arrow-parens: ["error", "as-needed"]*/

(a) => {};
(a) => a;
(a) => {'\n'};
a.then((foo) => {});
a.then((foo) => a);
a((foo) => { if (true) {} });
const f = /** @type {number} */(a) => a + a;
const g = /* comment */ (a) => a + a;
const h = (a) /* comment */ => a + a;

使用 "as-needed" 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint arrow-parens: ["error", "as-needed"]*/

() => {};
a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;
const f = (/** @type {number} */a) => a + a;
const g = (/* comment */ a) => a + a;
const h = (a /* comment */) => a + a;

requireForBlockBody

使用 { "requireForBlockBody": true } 选项时,此规则的错误代码示例

在 Playground 中打开
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/

(a) => a;
a => {};
a => {'\n'};
a.map((x) => x * x);
a.map(x => {
  return x * x;
});
a.then(foo => {});

使用 { "requireForBlockBody": true } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/

(a) => {};
(a) => {'\n'};
a => ({});
() => {};
a => a;
a.then((foo) => {});
a.then((foo) => { if (true) {} });
a((foo) => { if (true) {} });
(a, b, c) => a;
(a = 10) => a;
([a, b]) => a;
({a, b}) => a;

版本

此规则在 ESLint v1.0.0-rc-1 中引入。

延伸阅读

资源

更改语言