@@ -74,12 +74,11 @@ ruleTester.run("no-constant-condition", rule, {
74
74
"if(true && typeof abc==='string'){}" ,
75
75
76
76
// #11181, string literals
77
- "if('str' || a){}" ,
78
77
"if('str1' && a){}" ,
79
78
"if(a && 'str'){}" ,
80
- "if('str' || abc==='str'){}" ,
81
79
82
80
// #11306
81
+ "if ((foo || true) === 'baz') {}" ,
83
82
"if ((foo || 'bar') === 'baz') {}" ,
84
83
"if ((foo || 'bar') !== 'baz') {}" ,
85
84
"if ((foo || 'bar') == 'baz') {}" ,
@@ -90,6 +89,19 @@ ruleTester.run("no-constant-condition", rule, {
90
89
"if ((foo || 233) <= 666) {}" ,
91
90
"if ((key || 'k') in obj) {}" ,
92
91
"if ((foo || {}) instanceof obj) {}" ,
92
+ "if ((foo || 'bar' || 'bar') === 'bar');" ,
93
+ {
94
+ code : "if ((foo || 1n) === 'baz') {}" ,
95
+ parserOptions : { ecmaVersion : 11 }
96
+ } ,
97
+ {
98
+ code : "if (a && 0n || b);" ,
99
+ parserOptions : { ecmaVersion : 11 }
100
+ } ,
101
+ {
102
+ code : "if(1n && a){};" ,
103
+ parserOptions : { ecmaVersion : 11 }
104
+ } ,
93
105
94
106
// #12225
95
107
"if ('' + [y] === '' + [ty]) {}" ,
@@ -121,18 +133,22 @@ ruleTester.run("no-constant-condition", rule, {
121
133
{ code : "for(;`foo`;);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
122
134
{ code : "for(;`foo${bar}`;);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
123
135
{ code : "do{}while(true)" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
136
+ { code : "do{}while('1')" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
137
+ { code : "do{}while(0)" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
124
138
{ code : "do{}while(t = -2)" , errors : [ { messageId : "unexpected" , type : "AssignmentExpression" } ] } ,
125
139
{ code : "do{}while(``)" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
126
140
{ code : "do{}while(`foo`)" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
127
141
{ code : "do{}while(`foo${bar}`)" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
128
142
{ code : "true ? 1 : 2;" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
143
+ { code : "1 ? 1 : 2;" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
129
144
{ code : "q = 0 ? 1 : 2;" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
130
145
{ code : "(q = 0) ? 1 : 2;" , errors : [ { messageId : "unexpected" , type : "AssignmentExpression" } ] } ,
131
146
{ code : "`` ? 1 : 2;" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
132
147
{ code : "`foo` ? 1 : 2;" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
133
148
{ code : "`foo${bar}` ? 1 : 2;" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
134
149
{ code : "if(-2);" , errors : [ { messageId : "unexpected" , type : "UnaryExpression" } ] } ,
135
150
{ code : "if(true);" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
151
+ { code : "if(1);" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
136
152
{ code : "if({});" , errors : [ { messageId : "unexpected" , type : "ObjectExpression" } ] } ,
137
153
{ code : "if(0 < 1);" , errors : [ { messageId : "unexpected" , type : "BinaryExpression" } ] } ,
138
154
{ code : "if(0 || 1);" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
@@ -143,6 +159,7 @@ ruleTester.run("no-constant-condition", rule, {
143
159
{ code : "if(`${'bar'}`);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
144
160
{ code : "if(`${'bar' + `foo`}`);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
145
161
{ code : "if(`foo${false || true}`);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
162
+ { code : "if(`foo${0 || 1}`);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
146
163
{ code : "if(`foo${bar}`);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
147
164
{ code : "if(`${bar}foo`);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
148
165
@@ -152,6 +169,7 @@ ruleTester.run("no-constant-condition", rule, {
152
169
{ code : "while(x = 1);" , errors : [ { messageId : "unexpected" , type : "AssignmentExpression" } ] } ,
153
170
{ code : "while(function(){});" , errors : [ { messageId : "unexpected" , type : "FunctionExpression" } ] } ,
154
171
{ code : "while(true);" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
172
+ { code : "while(1);" , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
155
173
{ code : "while(() => {});" , errors : [ { messageId : "unexpected" , type : "ArrowFunctionExpression" } ] } ,
156
174
{ code : "while(`foo`);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
157
175
{ code : "while(``);" , errors : [ { messageId : "unexpected" , type : "TemplateLiteral" } ] } ,
@@ -180,12 +198,15 @@ ruleTester.run("no-constant-condition", rule, {
180
198
// #5693
181
199
{ code : "if(false && abc==='str'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
182
200
{ code : "if(true || abc==='str'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
201
+ { code : "if(1 || abc==='str'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
183
202
{ code : "if(abc==='str' || true){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
184
203
{ code : "if(abc==='str' || true || def ==='str'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
185
204
{ code : "if(false || true){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
186
205
{ code : "if(typeof abc==='str' || true){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
187
206
188
207
// #11181, string literals
208
+ { code : "if('str' || a){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
209
+ { code : "if('str' || abc==='str'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
189
210
{ code : "if('str1' || 'str2'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
190
211
{ code : "if('str1' && 'str2'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
191
212
{ code : "if(abc==='str' || 'str'){}" , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] } ,
@@ -272,6 +293,17 @@ ruleTester.run("no-constant-condition", rule, {
272
293
{
273
294
code : "if ([,] + ''){}" ,
274
295
errors : [ { messageId : "unexpected" , type : "BinaryExpression" } ]
275
- }
296
+ } ,
297
+
298
+ // #13238
299
+ { code : "if(/foo/ui);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
300
+ { code : "if(0n);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
301
+ { code : "if(0b0n);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
302
+ { code : "if(0o0n);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
303
+ { code : "if(0x0n);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
304
+ { code : "if(0b1n);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
305
+ { code : "if(0o1n);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
306
+ { code : "if(0x1n);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "Literal" } ] } ,
307
+ { code : "if(0x1n || foo);" , parserOptions : { ecmaVersion : 11 } , errors : [ { messageId : "unexpected" , type : "LogicalExpression" } ] }
276
308
]
277
309
} ) ;
0 commit comments