arrow-parens
箭头函数参数需要括号
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
箭头函数只有一个参数时可以省略括号。在所有其他情况下,参数必须用括号括起来。此规则强制在箭头函数中一致地使用括号。
规则详情
此规则在箭头函数参数周围强制使用括号,而不考虑数量。例如:
/*eslint-env es6*/
// Bad
a => {}
// Good
(a) => {}
遵循这种风格将帮助您找到箭头函数 (=>
),当以 >=
之类的比较为目的时,这些箭头函数可能会被错误地包含在条件中。
/*eslint-env es6*/
// Bad
if (a => 2) {
}
// Good
if (a >= 2) {
}
XpuFlBw6NF0zBMhCpmatwkGio4+AyXLy7aZIoW2rN/F+a1UEt2ucIo5JCOTrSEHkrI0ayz5NyHWFvnFdTlEv2g==
/*eslint-env es6*/
// Bad
(a) => {}
// Good
a => {}
选项
FCjx99K/4aI1xO9VjYxFd4tsm9OTQKveZuvQ7xgbcj2Z+RSWt5f1gl9EEz0ahsqWZiGKN1VIz+UDadLu1UJEWw==
2FKeCwRi48MyVF6DOWYB5b6fwS9ofR5/6OWB4UuEKr8=
- iN7HFpq7PDDJV2rLwQqvh0b/k1mYt5zuJyxl0Gu3axmckzpsFmzXqowC7wTzgfuJHG1sPNJ3a3/LZ/LuTnmmI3EkQAqirCg6671aXfyM6AkWq4wA2gNwfEzX9moKddicCBpGW/VHT90sahe6VEdwNcONAvWY9XR7e5Fq/OjUZNDmg/RvyPCZG4fkVCV/VYwjVcJyiq0XwzDz8JRArxOe7e8OCyRxBLLalGT9bk9wmOfNhKn/0cjU+KSOpG1/90WB
RHcFcqcdjTptb3VLhipq+FbijyMY7LKSldeWqna+uuyrnrqB+qnh/vITDyCmnfLhz4MfpnbwsyolliA60y1Q2Q==
- tmVEwfLvOQIG7vRLgoCrNUuoJ/uQEIScQv/5Mmp2uM8hIUWwRKef0pXFDAlXvF6TN2aVvVD87KcSIXknKxdDicUiYd0pY9EtxG3vfuIyXcDcz7kWiUu1agSPAq7aHp7kfCblD+MFFpy/K2ryyeOhsv73zX65jil4hwpBM2ZWuqtvJm95QGJkP4xpZYm8UbvjcqLGAVxwYMtllXxdzI1ZfymX/goj3eTIztW2afNXurE=
always
xpQlHQir/OjetBCVQdvE2QpWtE+PP888KcV7qrKw7NiHFtRsY4Dtq4g9gj+t2CaJ6KjhK7moeqESzOZI5aQFt5mVKPEpN/SpPBsJp3/6uNs=
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/
a => {};
a => a;
a => {'\n'};
a.then(foo => {});
a.then(foo => a);
a(foo => { if (true) {} });
xpQlHQir/OjetBCVQdvE2QpWtE+PP888KcV7qrKw7NiHFtRsY4Dtq4g9gj+t2CaJeEi9R23UumbdJ7uiZxti37LkOepzQ9oLEX2Ksm/9tKk=
/*eslint arrow-parens: ["error", "always"]*/
/*eslint-env es6*/
() => {};
(a) => {};
(a) => a;
(a) => {'\n'}
a.then((foo) => {});
a.then((foo) => { if (true) {} });
如果语句
EruYQcOIoEsihYjrf6h9U/Wdp2PlXLZlpdGOdSEhtClsiAhDFVIZzx60V4YZue+2/ps+1aMpY8cMHaDnNUGFbUZL31dQox/4h3XKVbUzC1d9x4HeveeLE808nbXbsRNc
/*eslint-env es6*/
var a = 1;
var b = 2;
// ...
if (a => b) {
console.log('bigger');
} else {
console.log('smaller');
}
// outputs 'bigger', not smaller as expected
A4rjSxLHVQ9pvBfzGSsW0YJMQQtCNJU2kPqTL9DUEwdlPsNVm9vaYJu9VFrG3CISgM1JTIhWo7Sk5zB/4yib+2gsiwyGBcXKK+Fw8G27azY=
LTEDBY8vPJjPo1uq7P8BlzAZrOyZyizfHCBeI0rML7YLqVLh1VTJ911IVAPMZTgz8NM4P1AD2rjnPRxt+wRpmMPFKKHEwT/Sl7yRscPLsqIVLyjDlT8mjtT1K/QcJcG2
/*eslint-env es6*/
var a = 1;
var b = 0;
// ...
if ((a) => b) {
console.log('truthy value returned');
} else {
console.log('falsy value returned');
}
// outputs 'truthy value returned'
2BENwWpSLxdgqVA/QgXqB04fExu7FSRFauGldqjFAlDhMqKfOdublBxD1IokYG8V
/*eslint-env es6*/
var a = 1, b = 2, c = 3, d = 4;
var f = a => b ? c: d;
// f = ?
lQbBAPbYXk7WS1kqwwQucfUpXpJXKsPq1uKCUYITpKyMrLvIFAqTfLCpPgK7r699DPoaMpvPnBH0YkyP4AyEf7mR94DyE6xJhDoT3PgD60v6LPxgs7jjwMF0t8HQKK+3Sbbn5gv07oR5DH0To3NGhbdt+Hy2WB6PjimP736k5Zk=
dUXTMCrJ8lP0buAurfHQNqCCzqHOOvUqWmuG3K28dmo=
/*eslint-env es6*/
var a = 1, b = 2, c = 3, d = 4;
var f = (a) => b ? c: d;
as-needed
gXTXeFewMhKUGG8XamsAXUQfw3N1Yf/XLMtILgfEmuWQuY4s+ObgQ1ZTgZnr2kujFNrLeHLVPHeGynbT4FDOO/m7RhD4eDw/MLNHLqWuJuM=
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/
(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;
gXTXeFewMhKUGG8XamsAXUQfw3N1Yf/XLMtILgfEmuWQuY4s+ObgQ1ZTgZnr2kujirZCG3mcDOPlst1B3NE5zxWf34UFQ5IAsUfXhpn+FOg=
/*eslint arrow-parens: ["error", "as-needed"]*/
/*eslint-env es6*/
() => {};
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
Nq6nsTV64KOarfGmmPOp+AJbTei6w7ss99vMeMqll4sfnQ6rKY8BfM7lK2oewgVcFJArapkRlGx7+DxcAj5vlfqb33dJRhCsYmwFQtP2q3Q=
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/
(a) => a;
a => {};
a => {'\n'};
a.map((x) => x * x);
a.map(x => {
return x * x;
});
a.then(foo => {});
Nq6nsTV64KOarfGmmPOp+AJbTei6w7ss99vMeMqll4sfnQ6rKY8BfM7lK2oewgVcphQM96MbLnmNvrnl6aqmHEQRozPiAonKm3zN3/Xiiew=
/*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/
/*eslint-env es6*/
(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;