]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-arrow-condition.md
6783bdedabd164a1df65249da97c2ab146a339b2
[pve-eslint.git] / eslint / docs / src / rules / no-arrow-condition.md
1 ---
2 title: no-arrow-condition
3 layout: doc
4
5 related_rules:
6 - arrow-parens
7 - no-confusing-arrow
8 - no-constant-condition
9 ---
10
11 Disallows arrow functions where test conditions are expected.
12
13 (removed) This rule was **removed** in ESLint v2.0 and **replaced** by a combination of the [no-confusing-arrow](no-confusing-arrow) and [no-constant-condition](no-constant-condition) rules.
14
15 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.
16
17 Here's an example where the usage of `=>` is most likely a typo:
18
19 ```js
20 // This is probably a typo
21 if (a => 1) {}
22 // And should instead be
23 if (a >= 1) {}
24 ```
25
26 There are also cases where the usage of `=>` can be ambiguous and should be rewritten to more clearly show the author's intent:
27
28 ```js
29 // The intent is not clear
30 var x = a => 1 ? 2 : 3
31 // Did the author mean this
32 var x = function (a) { return a >= 1 ? 2 : 3 }
33 // Or this
34 var x = a <= 1 ? 2 : 3
35 ```
36
37 ## Rule Details
38
39 Examples of **incorrect** code for this rule:
40
41 :::incorrect
42
43 ```js
44 /*eslint no-arrow-condition: "error"*/
45 /*eslint-env es6*/
46
47 if (a => 1) {}
48 while (a => 1) {}
49 for (var a = 1; a => 10; a++) {}
50 a => 1 ? 2 : 3
51 (a => 1) ? 2 : 3
52 var x = a => 1 ? 2 : 3
53 var x = (a) => 1 ? 2 : 3
54 ```
55
56 :::