]>
Commit | Line | Data |
---|---|---|
f2a92ac6 DC |
1 | --- |
2 | title: logical-assignment-operators | |
3 | rule_type: suggestion | |
4 | --- | |
5 | ||
6 | ES2021 introduces the assignment operator shorthand for the logical operators `||`, `&&` and `??`. | |
7 | Before, this was only allowed for mathematical operations such as `+` or `*` (see the rule [operator-assignment](./operator-assignment)). | |
8 | The shorthand can be used if the assignment target and the left expression of a logical expression are the same. | |
9 | For example `a = a || b` can be shortened to `a ||= b`. | |
10 | ||
11 | ## Rule Details | |
12 | ||
13 | This rule requires or disallows logical assignment operator shorthand. | |
14 | ||
15 | ### Options | |
16 | ||
17 | This rule has a string and an object option. | |
18 | String option: | |
19 | ||
20 | * `"always"` (default) | |
21 | * `"never"` | |
22 | ||
23 | Object option (only available if string option is set to `"always"`): | |
24 | ||
25 | * `"enforceForIfStatements": false`(default) Do *not* check for equivalent `if` statements | |
26 | * `"enforceForIfStatements": true` Check for equivalent `if` statements | |
27 | ||
28 | #### always | |
29 | ||
30 | Examples of **incorrect** code for this rule with the default `"always"` option: | |
31 | ||
32 | ::: incorrect | |
33 | ||
34 | ```js | |
35 | /*eslint logical-assignment-operators: ["error", "always"]*/ | |
36 | ||
37 | a = a || b | |
38 | a = a && b | |
39 | a = a ?? b | |
40 | a || (a = b) | |
41 | a && (a = b) | |
42 | a ?? (a = b) | |
43 | ``` | |
44 | ||
45 | ::: | |
46 | ||
47 | Examples of **correct** code for this rule with the default `"always"` option: | |
48 | ||
49 | ::: correct | |
50 | ||
51 | ```js | |
52 | /*eslint logical-assignment-operators: ["error", "always"]*/ | |
53 | ||
54 | a = b | |
55 | a += b | |
56 | a ||= b | |
57 | a = b || c | |
58 | a || (b = c) | |
59 | ||
60 | if (a) a = b | |
61 | ``` | |
62 | ||
63 | ::: | |
64 | ||
65 | #### never | |
66 | ||
67 | Examples of **incorrect** code for this rule with the `"never"` option: | |
68 | ||
69 | ::: incorrect | |
70 | ||
71 | ```js | |
72 | /*eslint logical-assignment-operators: ["error", "never"]*/ | |
73 | ||
74 | a ||= b | |
75 | a &&= b | |
76 | a ??= b | |
77 | ``` | |
78 | ||
79 | ::: | |
80 | ||
81 | Examples of **correct** code for this rule with the `"never"` option: | |
82 | ||
83 | ::: correct | |
84 | ||
85 | ```js | |
86 | /*eslint logical-assignment-operators: ["error", "never"]*/ | |
87 | ||
88 | a = a || b | |
89 | a = a && b | |
90 | a = a ?? b | |
91 | ``` | |
92 | ||
93 | ::: | |
94 | ||
95 | #### enforceForIfStatements | |
96 | ||
97 | This option checks for additional patterns with if statements which could be expressed with the logical assignment operator. | |
98 | ||
99 | ::: incorrect | |
100 | ||
101 | Examples of **incorrect** code for this rule with the `["always", { enforceForIfStatements: true }]` option: | |
102 | ||
103 | ```js | |
104 | /*eslint logical-assignment-operators: ["error", "always", { enforceForIfStatements: true }]*/ | |
105 | ||
106 | if (a) a = b // <=> a &&= b | |
107 | if (!a) a = b // <=> a ||= b | |
108 | ||
109 | if (a == null) a = b // <=> a ??= b | |
110 | if (a === null || a === undefined) a = b // <=> a ??= b | |
111 | ``` | |
112 | ||
113 | ::: | |
114 | ||
115 | Examples of **correct** code for this rule with the `["always", { enforceForIfStatements: true }]` option: | |
116 | ||
117 | ::: correct | |
118 | ||
119 | ```js | |
120 | /*eslint logical-assignment-operators: ["error", "always", { enforceForIfStatements: true }]*/ | |
121 | ||
122 | if (a) b = c | |
123 | if (a === 0) a = b | |
124 | ``` | |
125 | ||
126 | ::: | |
127 | ||
128 | ## When Not To Use It | |
129 | ||
130 | Use of logical operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis. |