]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/logical-assignment-operators.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / logical-assignment-operators.md
CommitLineData
f2a92ac6
DC
1---
2title: logical-assignment-operators
3rule_type: suggestion
4---
5
6ES2021 introduces the assignment operator shorthand for the logical operators `||`, `&&` and `??`.
7Before, this was only allowed for mathematical operations such as `+` or `*` (see the rule [operator-assignment](./operator-assignment)).
8The shorthand can be used if the assignment target and the left expression of a logical expression are the same.
9For example `a = a || b` can be shortened to `a ||= b`.
10
11## Rule Details
12
13This rule requires or disallows logical assignment operator shorthand.
14
15### Options
16
17This rule has a string and an object option.
18String option:
19
20* `"always"` (default)
21* `"never"`
22
23Object 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
30Examples 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
37a = a || b
38a = a && b
39a = a ?? b
40a || (a = b)
41a && (a = b)
42a ?? (a = b)
43```
44
45:::
46
47Examples 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
54a = b
55a += b
56a ||= b
57a = b || c
58a || (b = c)
59
60if (a) a = b
61```
62
63:::
64
65#### never
66
67Examples of **incorrect** code for this rule with the `"never"` option:
68
69::: incorrect
70
71```js
72/*eslint logical-assignment-operators: ["error", "never"]*/
73
74a ||= b
75a &&= b
76a ??= b
77```
78
79:::
80
81Examples of **correct** code for this rule with the `"never"` option:
82
83::: correct
84
85```js
86/*eslint logical-assignment-operators: ["error", "never"]*/
87
88a = a || b
89a = a && b
90a = a ?? b
91```
92
93:::
94
95#### enforceForIfStatements
96
97This option checks for additional patterns with if statements which could be expressed with the logical assignment operator.
98
99::: incorrect
100
101Examples 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
106if (a) a = b // <=> a &&= b
107if (!a) a = b // <=> a ||= b
108
109if (a == null) a = b // <=> a ??= b
110if (a === null || a === undefined) a = b // <=> a ??= b
111```
112
113:::
114
115Examples 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
122if (a) b = c
123if (a === 0) a = b
124```
125
126:::
127
128## When Not To Use It
129
130Use 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.