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