Index

no-arrow-condition

在需要测试条件的地方禁止使用箭头函数。

🌐 Disallows arrow functions where test conditions are expected.

箭头函数(=>)在语法上类似于一些比较运算符(><<=>=)。此规则会警告在期望条件的地方使用箭头函数语法。即使箭头函数的参数被括号封装,此规则仍会发出警告。

🌐 Arrow functions (=>) are similar in syntax to some comparison operators (>, <, <=, and >=). This rule warns against using the arrow function syntax in places where a condition is expected. Even if the arguments of the arrow function are wrapped with parens, this rule still warns about it.

这是一个例子,其中 => 的使用很可能是一个拼写错误:

🌐 Here’s an example where the usage of => is most likely a typo:

// This is probably a typo
if (a => 1) {}
// And should instead be
if (a >= 1) {}

也有一些情况,=> 的用法可能存在歧义,应当重写以更清楚地显示作者的意图:

🌐 There are also cases where the usage of => can be ambiguous and should be rewritten to more clearly show the author’s intent:

// The intent is not clear
var x = a => 1 ? 2 : 3
// Did the author mean this
var x = function (a) { return a >= 1 ? 2 : 3 }
// Or this
var x = a <= 1 ? 2 : 3

规则详情

🌐 Rule Details

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

/*eslint no-arrow-condition: "error"*/

if (a => 1) {}
while (a => 1) {}
for (var a = 1; a => 10; a++) {}
a => 1 ? 2 : 3
(a => 1) ? 2 : 3
var x = a => 1 ? 2 : 3
var x = (a) => 1 ? 2 : 3

版本

此规则是在 ESLint v1.8.0 中引入,并在 v2.0.0-beta.3 中删除。