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