noConfusingArrow (since v12.1.0)
Disallow arrow functions where they could be confused with comparisons.
Arrow functions (=>
) are similar in syntax to some comparison operators (>
, <
, <=
, and >=
). This rule warns against using the arrow function syntax in places where it could be confused with a comparison operator.
Source: https://eslint.org/docs/latest/rules/no-confusing-arrow
Examples
Invalid
var x = a => 1 ? 2 : 3;
nursery/noConfusingArrow.js:1:11 lint/nursery/noConfusingArrow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Fat arrows can be confused with some comparison operators (<, >, <=, >=).
> 1 │ var x = a => 1 ? 2 : 3;
│ ^^
2 │
var x = (a) => 1 ? 2 : 3;
nursery/noConfusingArrow.js:1:13 lint/nursery/noConfusingArrow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Fat arrows can be confused with some comparison operators (<, >, <=, >=).
> 1 │ var x = (a) => 1 ? 2 : 3;
│ ^^
2 │
Valid
var x = a => (1 ? 2 : 3);
var x = (a) => (1 ? 2 : 3);
var x = (a) => {
return 1 ? 2 : 3;
};
var x = a => { return 1 ? 2 : 3; };