]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # disallow bitwise operators (no-bitwise) |
2 | ||
3 | The use of bitwise operators in JavaScript is very rare and often `&` or `|` is simply a mistyped `&&` or `||`, which will lead to unexpected behavior. | |
4 | ||
5 | ```js | |
6 | var x = y | z; | |
7 | ``` | |
8 | ||
9 | ## Rule Details | |
10 | ||
11 | This rule disallows bitwise operators. | |
12 | ||
13 | Examples of **incorrect** code for this rule: | |
14 | ||
15 | ```js | |
16 | /*eslint no-bitwise: "error"*/ | |
17 | ||
18 | var x = y | z; | |
19 | ||
20 | var x = y & z; | |
21 | ||
22 | var x = y ^ z; | |
23 | ||
24 | var x = ~ z; | |
25 | ||
26 | var x = y << z; | |
27 | ||
28 | var x = y >> z; | |
29 | ||
30 | var x = y >>> z; | |
31 | ||
32 | x |= y; | |
33 | ||
34 | x &= y; | |
35 | ||
36 | x ^= y; | |
37 | ||
38 | x <<= y; | |
39 | ||
40 | x >>= y; | |
41 | ||
42 | x >>>= y; | |
43 | ``` | |
44 | ||
45 | Examples of **correct** code for this rule: | |
46 | ||
47 | ```js | |
48 | /*eslint no-bitwise: "error"*/ | |
49 | ||
50 | var x = y || z; | |
51 | ||
52 | var x = y && z; | |
53 | ||
54 | var x = y > z; | |
55 | ||
56 | var x = y < z; | |
57 | ||
58 | x += y; | |
59 | ``` | |
60 | ||
61 | ## Options | |
62 | ||
63 | This rule has an object option: | |
64 | ||
65 | * `"allow"`: Allows a list of bitwise operators to be used as exceptions. | |
66 | * `"int32Hint"`: Allows the use of bitwise OR in `|0` pattern for type casting. | |
67 | ||
68 | ### allow | |
69 | ||
70 | Examples of **correct** code for this rule with the `{ "allow": ["~"] }` option: | |
71 | ||
72 | ```js | |
73 | /*eslint no-bitwise: ["error", { "allow": ["~"] }] */ | |
74 | ||
75 | ~[1,2,3].indexOf(1) === -1; | |
76 | ``` | |
77 | ||
78 | ### int32Hint | |
79 | ||
80 | Examples of **correct** code for this rule with the `{ "int32Hint": true }` option: | |
81 | ||
82 | ```js | |
83 | /*eslint no-bitwise: ["error", { "int32Hint": true }] */ | |
84 | ||
85 | var b = a|0; | |
86 | ``` |