]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-confusing-arrow.md
dccf11e9f1b86dbb815125563be11cbf687f0041
[pve-eslint.git] / eslint / docs / rules / no-confusing-arrow.md
1 # Disallow arrow functions where they could be confused with comparisons (no-confusing-arrow)
2
3 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.
4
5 Here's an example where the usage of `=>` could be confusing:
6
7 ```js
8 // The intent is not clear
9 var x = a => 1 ? 2 : 3;
10 // Did the author mean this
11 var x = function (a) { return 1 ? 2 : 3 };
12 // Or this
13 var x = a <= 1 ? 2 : 3;
14 ```
15
16 ## Rule Details
17
18 Examples of **incorrect** code for this rule:
19
20 ```js
21 /*eslint no-confusing-arrow: "error"*/
22 /*eslint-env es6*/
23
24 var x = a => 1 ? 2 : 3;
25 var x = (a) => 1 ? 2 : 3;
26 ```
27
28 Examples of **correct** code for this rule:
29
30 ```js
31 /*eslint no-confusing-arrow: "error"*/
32 /*eslint-env es6*/
33
34 var x = a => (1 ? 2 : 3);
35 var x = (a) => (1 ? 2 : 3);
36 var x = a => { return 1 ? 2 : 3; };
37 var x = (a) => { return 1 ? 2 : 3; };
38 ```
39
40 ## Options
41
42 This rule accepts a single options argument with the following defaults:
43
44 ```json
45 {
46 "rules": {
47 "no-confusing-arrow": ["error", {"allowParens": true}]
48 }
49 }
50 ```
51
52 `allowParens` is a boolean setting that can be `true`(default) or `false`:
53
54 1. `true` relaxes the rule and accepts parenthesis as a valid "confusion-preventing" syntax.
55 2. `false` warns even if the expression is wrapped in parenthesis
56
57 Examples of **incorrect** code for this rule with the `{"allowParens": false}` option:
58
59 ```js
60 /*eslint no-confusing-arrow: ["error", {"allowParens": false}]*/
61 /*eslint-env es6*/
62 var x = a => (1 ? 2 : 3);
63 var x = (a) => (1 ? 2 : 3);
64 ```
65
66 ## Related Rules
67
68 * [no-constant-condition](no-constant-condition.md)
69 * [arrow-parens](arrow-parens.md)