]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-constant-condition | |
8f9d1d4d DC |
3 | rule_type: problem |
4 | related_rules: | |
5 | - no-constant-binary-expression | |
6 | --- | |
7 | ||
8 | ||
eb39fafa DC |
9 | |
10 | A constant expression (for example, a literal) as a test condition might be a typo or development trigger for a specific behavior. For example, the following code looks as if it is not ready for production. | |
11 | ||
12 | ```js | |
13 | if (false) { | |
14 | doSomethingUnfinished(); | |
15 | } | |
16 | ``` | |
17 | ||
18 | ## Rule Details | |
19 | ||
20 | This rule disallows constant expressions in the test condition of: | |
21 | ||
22 | * `if`, `for`, `while`, or `do...while` statement | |
23 | * `?:` ternary expression | |
24 | ||
25 | Examples of **incorrect** code for this rule: | |
26 | ||
8f9d1d4d DC |
27 | ::: incorrect |
28 | ||
eb39fafa DC |
29 | ```js |
30 | /*eslint no-constant-condition: "error"*/ | |
31 | ||
32 | if (false) { | |
33 | doSomethingUnfinished(); | |
34 | } | |
35 | ||
36 | if (void x) { | |
37 | doSomethingUnfinished(); | |
38 | } | |
39 | ||
456be15e TL |
40 | if (x &&= false) { |
41 | doSomethingNever(); | |
42 | } | |
43 | ||
34eeec05 TL |
44 | if (class {}) { |
45 | doSomethingAlways(); | |
46 | } | |
47 | ||
48 | if (new Boolean(x)) { | |
49 | doSomethingAlways(); | |
50 | } | |
51 | ||
8f9d1d4d DC |
52 | if (Boolean(1)) { |
53 | doSomethingAlways(); | |
54 | } | |
55 | ||
56 | if (undefined) { | |
57 | doSomethingUnfinished(); | |
58 | } | |
59 | ||
456be15e TL |
60 | if (x ||= true) { |
61 | doSomethingAlways(); | |
62 | } | |
63 | ||
eb39fafa DC |
64 | for (;-2;) { |
65 | doSomethingForever(); | |
66 | } | |
67 | ||
68 | while (typeof x) { | |
69 | doSomethingForever(); | |
70 | } | |
71 | ||
72 | do { | |
73 | doSomethingForever(); | |
74 | } while (x = -1); | |
75 | ||
76 | var result = 0 ? a : b; | |
f2a92ac6 DC |
77 | |
78 | if(input === "hello" || "bye"){ | |
79 | output(input); | |
80 | } | |
eb39fafa DC |
81 | ``` |
82 | ||
8f9d1d4d DC |
83 | ::: |
84 | ||
eb39fafa DC |
85 | Examples of **correct** code for this rule: |
86 | ||
8f9d1d4d DC |
87 | ::: correct |
88 | ||
eb39fafa DC |
89 | ```js |
90 | /*eslint no-constant-condition: "error"*/ | |
91 | ||
92 | if (x === 0) { | |
93 | doSomething(); | |
94 | } | |
95 | ||
96 | for (;;) { | |
97 | doSomethingForever(); | |
98 | } | |
99 | ||
100 | while (typeof x === "undefined") { | |
101 | doSomething(); | |
102 | } | |
103 | ||
104 | do { | |
105 | doSomething(); | |
106 | } while (x); | |
107 | ||
108 | var result = x !== 0 ? a : b; | |
f2a92ac6 DC |
109 | |
110 | if(input === "hello" || input === "bye"){ | |
111 | output(input); | |
112 | } | |
eb39fafa DC |
113 | ``` |
114 | ||
8f9d1d4d DC |
115 | ::: |
116 | ||
eb39fafa DC |
117 | ## Options |
118 | ||
119 | ### checkLoops | |
120 | ||
121 | Set to `true` by default. Setting this option to `false` allows constant expressions in loops. | |
122 | ||
123 | Examples of **correct** code for when `checkLoops` is `false`: | |
124 | ||
8f9d1d4d DC |
125 | ::: correct |
126 | ||
eb39fafa DC |
127 | ```js |
128 | /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ | |
129 | ||
130 | while (true) { | |
131 | doSomething(); | |
132 | if (condition()) { | |
133 | break; | |
134 | } | |
135 | }; | |
136 | ||
137 | for (;true;) { | |
138 | doSomething(); | |
139 | if (condition()) { | |
140 | break; | |
141 | } | |
142 | }; | |
143 | ||
144 | do { | |
145 | doSomething(); | |
146 | if (condition()) { | |
147 | break; | |
148 | } | |
149 | } while (true) | |
150 | ``` | |
8f9d1d4d DC |
151 | |
152 | ::: |