]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-cond-assign.md
af0454884c5aa7001ccf4d8a32d804a34d323113
[pve-eslint.git] / eslint / docs / src / rules / no-cond-assign.md
1 ---
2 title: no-cond-assign
3 layout: doc
4 rule_type: problem
5 related_rules:
6 - no-extra-parens
7 ---
8
9
10
11 In conditional statements, it is very easy to mistype a comparison operator (such as `==`) as an assignment operator (such as `=`). For example:
12
13 ```js
14 // Check the user's job title
15 if (user.jobTitle = "manager") {
16 // user.jobTitle is now incorrect
17 }
18 ```
19
20 There are valid reasons to use assignment operators in conditional statements. However, it can be difficult to tell whether a specific assignment was intentional.
21
22 ## Rule Details
23
24 This rule disallows ambiguous assignment operators in test conditions of `if`, `for`, `while`, and `do...while` statements.
25
26 ## Options
27
28 This rule has a string option:
29
30 * `"except-parens"` (default) allows assignments in test conditions *only if* they are enclosed in parentheses (for example, to allow reassigning a variable in the test of a `while` or `do...while` loop)
31 * `"always"` disallows all assignments in test conditions
32
33 ### except-parens
34
35 Examples of **incorrect** code for this rule with the default `"except-parens"` option:
36
37 ::: incorrect
38
39 ```js
40 /*eslint no-cond-assign: "error"*/
41
42 // Unintentional assignment
43 var x;
44 if (x = 0) {
45 var b = 1;
46 }
47
48 // Practical example that is similar to an error
49 function setHeight(someNode) {
50 "use strict";
51 do {
52 someNode.height = "100px";
53 } while (someNode = someNode.parentNode);
54 }
55 ```
56
57 :::
58
59 Examples of **correct** code for this rule with the default `"except-parens"` option:
60
61 ::: correct
62
63 ```js
64 /*eslint no-cond-assign: "error"*/
65
66 // Assignment replaced by comparison
67 var x;
68 if (x === 0) {
69 var b = 1;
70 }
71
72 // Practical example that wraps the assignment in parentheses
73 function setHeight(someNode) {
74 "use strict";
75 do {
76 someNode.height = "100px";
77 } while ((someNode = someNode.parentNode));
78 }
79
80 // Practical example that wraps the assignment and tests for 'null'
81 function setHeight(someNode) {
82 "use strict";
83 do {
84 someNode.height = "100px";
85 } while ((someNode = someNode.parentNode) !== null);
86 }
87 ```
88
89 :::
90
91 ### always
92
93 Examples of **incorrect** code for this rule with the `"always"` option:
94
95 ::: incorrect
96
97 ```js
98 /*eslint no-cond-assign: ["error", "always"]*/
99
100 // Unintentional assignment
101 var x;
102 if (x = 0) {
103 var b = 1;
104 }
105
106 // Practical example that is similar to an error
107 function setHeight(someNode) {
108 "use strict";
109 do {
110 someNode.height = "100px";
111 } while (someNode = someNode.parentNode);
112 }
113
114 // Practical example that wraps the assignment in parentheses
115 function setHeight(someNode) {
116 "use strict";
117 do {
118 someNode.height = "100px";
119 } while ((someNode = someNode.parentNode));
120 }
121
122 // Practical example that wraps the assignment and tests for 'null'
123 function setHeight(someNode) {
124 "use strict";
125 do {
126 someNode.height = "100px";
127 } while ((someNode = someNode.parentNode) !== null);
128 }
129 ```
130
131 :::
132
133 Examples of **correct** code for this rule with the `"always"` option:
134
135 ::: correct
136
137 ```js
138 /*eslint no-cond-assign: ["error", "always"]*/
139
140 // Assignment replaced by comparison
141 var x;
142 if (x === 0) {
143 var b = 1;
144 }
145 ```
146
147 :::