]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # require spacing around infix operators (space-infix-ops) |
2 | ||
3 | While formatting preferences are very personal, a number of style guides require spaces around operators, such as: | |
4 | ||
5 | ```js | |
6 | var sum = 1 + 2; | |
7 | ``` | |
8 | ||
9 | The proponents of these extra spaces believe it make the code easier to read and can more easily highlight potential errors, such as: | |
10 | ||
11 | ```js | |
12 | var sum = i+++2; | |
13 | ``` | |
14 | ||
15 | While this is valid JavaScript syntax, it is hard to determine what the author intended. | |
16 | ||
17 | ## Rule Details | |
18 | ||
19 | This rule is aimed at ensuring there are spaces around infix operators. | |
20 | ||
21 | ## Options | |
22 | ||
23 | This rule accepts a single options argument with the following defaults: | |
24 | ||
25 | ```json | |
26 | "space-infix-ops": ["error", { "int32Hint": false }] | |
27 | ``` | |
28 | ||
29 | ### `int32Hint` | |
30 | ||
31 | Set the `int32Hint` option to `true` (default is `false`) to allow write `a|0` without space. | |
32 | ||
33 | ```js | |
34 | var foo = bar|0; // `foo` is forced to be signed 32 bit integer | |
35 | ``` | |
36 | ||
37 | Examples of **incorrect** code for this rule: | |
38 | ||
39 | ```js | |
40 | /*eslint space-infix-ops: "error"*/ | |
41 | /*eslint-env es6*/ | |
42 | ||
43 | a+b | |
44 | ||
45 | a+ b | |
46 | ||
47 | a +b | |
48 | ||
49 | a?b:c | |
50 | ||
51 | const a={b:1}; | |
52 | ||
53 | var {a=0}=bar; | |
54 | ||
55 | function foo(a=0) { } | |
56 | ``` | |
57 | ||
58 | Examples of **correct** code for this rule: | |
59 | ||
60 | ```js | |
61 | /*eslint space-infix-ops: "error"*/ | |
62 | /*eslint-env es6*/ | |
63 | ||
64 | a + b | |
65 | ||
66 | a + b | |
67 | ||
68 | a ? b : c | |
69 | ||
70 | const a = {b:1}; | |
71 | ||
72 | var {a = 0} = bar; | |
73 | ||
74 | function foo(a = 0) { } | |
75 | ``` | |
76 | ||
77 | ## When Not To Use It | |
78 | ||
79 | You can turn this rule off if you are not concerned with the consistency of spacing around infix operators. |