/
no-unreachable.js
211 lines (200 loc) · 8.96 KB
/
no-unreachable.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @fileoverview Tests for no-unreachable rule.
* @author Joel Feenstra
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-unreachable"),
RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-unreachable", rule, {
valid: [
"function foo() { function bar() { return 1; } return bar(); }",
"function foo() { return bar(); function bar() { return 1; } }",
"function foo() { return x; var x; }",
"function foo() { var x = 1; var y = 2; }",
"function foo() { var x = 1; var y = 2; return; }",
"while (true) { switch (foo) { case 1: x = 1; x = 2;} }",
"while (true) { break; var x; }",
"while (true) { continue; var x, y; }",
"while (true) { throw 'message'; var x; }",
"while (true) { if (true) break; var x = 1; }",
"while (true) continue;",
"switch (foo) { case 1: break; var x; }",
"switch (foo) { case 1: break; var x; default: throw true; };",
{
code: "const arrow_direction = arrow => { switch (arrow) { default: throw new Error(); };}",
parserOptions: {
ecmaVersion: 6
}
},
"var x = 1; y = 2; throw 'uh oh'; var y;",
"function foo() { var x = 1; if (x) { return; } x = 2; }",
"function foo() { var x = 1; if (x) { } else { return; } x = 2; }",
"function foo() { var x = 1; switch (x) { case 0: break; default: return; } x = 2; }",
"function foo() { var x = 1; while (x) { return; } x = 2; }",
"function foo() { var x = 1; for (x in {}) { return; } x = 2; }",
"function foo() { var x = 1; try { return; } finally { x = 2; } }",
"function foo() { var x = 1; for (;;) { if (x) break; } x = 2; }",
"A: { break A; } foo()"
],
invalid: [
{ code: "function foo() { return x; var x = 1; }", errors: [{ message: "Unreachable code.", type: "VariableDeclaration" }] },
{ code: "function foo() { return x; var x, y = 1; }", errors: [{ message: "Unreachable code.", type: "VariableDeclaration" }] },
{ code: "while (true) { continue; var x = 1; }", errors: [{ message: "Unreachable code.", type: "VariableDeclaration" }] },
{ code: "function foo() { return; x = 1; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { throw error; x = 1; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "while (true) { break; x = 1; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "while (true) { continue; x = 1; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { switch (foo) { case 1: return; x = 1; } }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { switch (foo) { case 1: throw e; x = 1; } }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "while (true) { switch (foo) { case 1: break; x = 1; } }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "while (true) { switch (foo) { case 1: continue; x = 1; } }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "var x = 1; throw 'uh oh'; var y = 2;", errors: [{ message: "Unreachable code.", type: "VariableDeclaration" }] },
{ code: "function foo() { var x = 1; if (x) { return; } else { throw e; } x = 2; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { var x = 1; if (x) return; else throw -1; x = 2; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { var x = 1; try { return; } finally {} x = 2; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { var x = 1; try { } finally { return; } x = 2; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { var x = 1; do { return; } while (x); x = 2; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { var x = 1; while (x) { if (x) break; else continue; x = 2; } }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { var x = 1; for (;;) { if (x) continue; } x = 2; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{ code: "function foo() { var x = 1; while (true) { } x = 2; }", errors: [{ message: "Unreachable code.", type: "ExpressionStatement" }] },
{
code: "const arrow_direction = arrow => { switch (arrow) { default: throw new Error(); }; g() }",
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 1,
column: 86,
endLine: 1,
endColumn: 89
}
]
},
// Merge the warnings of continuous unreachable nodes.
{
code: `
function foo() {
return;
a(); // ← ERROR: Unreachable code. (no-unreachable)
b() // ↑ ';' token is included in the unreachable code, so this statement will be merged.
// comment
c(); // ↑ ')' token is included in the unreachable code, so this statement will be merged.
}
`,
errors: [
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 5,
column: 21,
endLine: 9,
endColumn: 25
}
]
},
{
code: `
function foo() {
return;
a();
if (b()) {
c()
} else {
d()
}
}
`,
errors: [
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 5,
column: 21,
endLine: 11,
endColumn: 22
}
]
},
{
code: `
function foo() {
if (a) {
return
b();
c();
} else {
throw err
d();
}
}
`,
errors: [
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 5,
column: 25,
endLine: 6,
endColumn: 29
},
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 9,
column: 25,
endLine: 9,
endColumn: 29
}
]
},
{
code: `
function foo() {
if (a) {
return
b();
c();
} else {
throw err
d();
}
e();
}
`,
errors: [
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 5,
column: 25,
endLine: 6,
endColumn: 29
},
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 9,
column: 25,
endLine: 9,
endColumn: 29
},
{
message: "Unreachable code.",
type: "ExpressionStatement",
line: 11,
column: 21,
endLine: 11,
endColumn: 25
}
]
}
]
});