-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
no-constant-condition.js
160 lines (144 loc) · 8.9 KB
/
no-constant-condition.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @fileoverview Tests for no-constant-condition rule.
* @author Christian Schulz <http://rndm.de>
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-constant-condition"),
RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run("no-constant-condition", rule, {
valid: [
"if(a);",
"if(a == 0);",
"if(a = f());",
"if(1, a);",
"if ('every' in []);",
"while(~!a);",
"while(a = b);",
"for(;x < 10;);",
"for(;;);",
"do{ }while(x)",
"q > 0 ? 1 : 2;",
"while(x += 3) {}",
// #5228, typeof conditions
"if(typeof x === 'undefined'){}",
"if(a === 'str' && typeof b){}",
"typeof a == typeof b",
"typeof 'a' === 'string'|| typeof b === 'string'",
// #5726, void conditions
"if (void a || a);",
"if (a || void a);",
// #5693
"if(xyz === 'str1' && abc==='str2'){}",
"if(xyz === 'str1' || abc==='str2'){}",
"if(xyz === 'str1' || abc==='str2' && pqr === 5){}",
"if(typeof abc === 'string' && abc==='str2'){}",
"if(false || abc==='str'){}",
"if(true && abc==='str'){}",
"if(typeof 'str' && abc==='str'){}",
"if(abc==='str' || false || def ==='str'){}",
"if(true && abc==='str' || def ==='str'){}",
"if(true && typeof abc==='string'){}",
// { checkLoops: false }
{ code: "while(true);", options: [{ checkLoops: false }] },
{ code: "for(;true;);", options: [{ checkLoops: false }] },
{ code: "do{}while(true)", options: [{ checkLoops: false }] },
{ code: "function* foo(){while(true){yield 'foo';}}" },
{ code: "function* foo(){for(;true;){yield 'foo';}}" },
{ code: "function* foo(){do{yield 'foo';}while(true)}" },
{ code: "function* foo(){while (true) { while(true) {yield;}}}" },
{ code: "function* foo() {for (; yield; ) {}}" },
{ code: "function* foo() {for (; ; yield) {}}" },
{ code: "function* foo() {while (true) {function* foo() {yield;}yield;}}" },
{ code: "function* foo() { for (let x = yield; x < 10; x++) {yield;}yield;}" },
{ code: "function* foo() { for (let x = yield; ; x++) { yield; }}" }
],
invalid: [
{ code: "for(;true;);", errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }] },
{ code: "do{}while(true)", errors: [{ message: "Unexpected constant condition.", type: "DoWhileStatement" }] },
{ code: "do{}while(t = -2)", errors: [{ message: "Unexpected constant condition.", type: "DoWhileStatement" }] },
{ code: "true ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "ConditionalExpression" }] },
{ code: "q = 0 ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "ConditionalExpression" }] },
{ code: "(q = 0) ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "ConditionalExpression" }] },
{ code: "if(-2);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(true);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if({});", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(0 < 1);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(0 || 1);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(a, 1);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "while([]);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "while(~!0);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "while(x = 1);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "while(function(){});", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "while(true);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "while(() => {});", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
// #5228 , typeof conditions
{ code: "if(typeof x){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(typeof 'abc' === 'string'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(a = typeof b){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(a, typeof b){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(typeof 'a' == 'string' || typeof 'b' == 'string'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "while(typeof x){}", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
// #5726, void conditions
{ code: "if(1 || void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(y = void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(x, void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(void x === void y);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(void x && a);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(a && void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
// #5693
{ code: "if(false && abc==='str'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(true || abc==='str'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(abc==='str' || true){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(abc==='str' || true || def ==='str'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(false || true){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(typeof abc==='str' || true){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{
code: "function* foo(){while(true){} yield 'foo';}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
},
{
code: "function* foo(){while(true){if (true) {yield 'foo';}}}",
errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }]
},
{
code: "function* foo(){while(true){yield 'foo';} while(true) {}}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
},
{
code: "var a = function* foo(){while(true){} yield 'foo';}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
},
{
code: "while (true) { function* foo() {yield;}}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
},
{
code: "function* foo(){if (true) {yield 'foo';}}",
errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }]
},
{
code: "function* foo() {for (let foo = yield; true;) {}}",
errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }]
},
{
code: "function* foo() {for (foo = yield; true;) {}}",
errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }]
},
{
code: "function foo() {while (true) {function* bar() {while (true) {yield;}}}}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
},
{
code: "function* foo() { for (let foo = 1 + 2 + 3 + (yield); true; baz) {}}",
errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }]
}
]
});