forked from eslint/eslint
/
no-fallthrough.js
219 lines (212 loc) · 8.11 KB
/
no-fallthrough.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
212
213
214
215
216
217
218
219
/**
* @fileoverview Tests for no-fallthrough rule.
* @author Matt DuVall<http://mattduvall.com/>
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-fallthrough"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const errorsDefault = [{
messageId: "default",
type: "SwitchCase"
}];
const ruleTester = new RuleTester();
ruleTester.run("no-fallthrough", rule, {
valid: [
"switch(foo) { case 0: a(); /* falls through */ case 1: b(); }",
"switch(foo) { case 0: a()\n /* falls through */ case 1: b(); }",
"switch(foo) { case 0: a(); /* fall through */ case 1: b(); }",
"switch(foo) { case 0: a(); /* fallthrough */ case 1: b(); }",
"switch(foo) { case 0: a(); /* FALLS THROUGH */ case 1: b(); }",
"switch(foo) { case 0: { a(); /* falls through */ } case 1: b(); }",
"switch(foo) { case 0: { a()\n /* falls through */ } case 1: b(); }",
"switch(foo) { case 0: { a(); /* fall through */ } case 1: b(); }",
"switch(foo) { case 0: { a(); /* fallthrough */ } case 1: b(); }",
"switch(foo) { case 0: { a(); /* FALLS THROUGH */ } case 1: b(); }",
"switch(foo) { case 0: { a(); } /* falls through */ case 1: b(); }",
"switch(foo) { case 0: { a(); /* falls through */ } /* comment */ case 1: b(); }",
"switch(foo) { case 0: { /* falls through */ } case 1: b(); }",
"function foo() { switch(foo) { case 0: a(); return; case 1: b(); }; }",
"switch(foo) { case 0: a(); throw 'foo'; case 1: b(); }",
"while (a) { switch(foo) { case 0: a(); continue; case 1: b(); } }",
"switch(foo) { case 0: a(); break; case 1: b(); }",
"switch(foo) { case 0: case 1: a(); break; case 2: b(); }",
"switch(foo) { case 0: case 1: break; case 2: b(); }",
"switch(foo) { case 0: case 1: break; default: b(); }",
"switch(foo) { case 0: case 1: a(); }",
"switch(foo) { case 0: case 1: a(); break; }",
"switch(foo) { case 0: case 1: break; }",
"switch(foo) { case 0:\n case 1: break; }",
"switch(foo) { case 0: // comment\n case 1: break; }",
"function foo() { switch(foo) { case 0: case 1: return; } }",
"function foo() { switch(foo) { case 0: {return;}\n case 1: {return;} } }",
"switch(foo) { case 0: case 1: {break;} }",
"switch(foo) { }",
"switch(foo) { case 0: switch(bar) { case 2: break; } /* falls through */ case 1: break; }",
"function foo() { switch(foo) { case 1: return a; a++; }}",
"switch (foo) { case 0: a(); /* falls through */ default: b(); /* comment */ }",
"switch (foo) { case 0: a(); /* falls through */ default: /* comment */ b(); }",
"switch (foo) { case 0: if (a) { break; } else { throw 0; } default: b(); }",
"switch (foo) { case 0: try { break; } finally {} default: b(); }",
"switch (foo) { case 0: try {} finally { break; } default: b(); }",
"switch (foo) { case 0: try { throw 0; } catch (err) { break; } default: b(); }",
"switch (foo) { case 0: do { throw 0; } while(a); default: b(); }",
{
code: "switch(foo) { case 0: a(); /* no break */ case 1: b(); }",
options: [{
commentPattern: "no break"
}]
},
{
code: "switch(foo) { case 0: a(); /* no break: need to execute b() */ case 1: b(); }",
options: [{
commentPattern: "no break:\\s?\\w+"
}]
},
{
code: "switch(foo) { case 0: a();\n// need to execute b(), so\n// falling through\n case 1: b(); }",
options: [{
commentPattern: "falling through"
}]
},
{
code: "switch(foo) { case 0: a(); /* break omitted */ default: b(); /* comment */ }",
options: [{
commentPattern: "break omitted"
}]
},
{
code: "switch(foo) { case 0: a(); /* caution: break is omitted intentionally */ case 1: b(); /* break omitted */ default: c(); }",
options: [{
commentPattern: "break[\\s\\w]+omitted"
}]
}
],
invalid: [
{
code: "switch(foo) { case 0: a();\ncase 1: b() }",
errors: [
{
messageId: "case",
type: "SwitchCase",
line: 2,
column: 1
}
]
},
{
code: "switch(foo) { case 0: a();\ndefault: b() }",
errors: [
{
messageId: "default",
type: "SwitchCase",
line: 2,
column: 1
}
]
},
{
code: "switch(foo) { case 0: a(); default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: if (a) { break; } default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: try { throw 0; } catch (err) {} default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: while (a) { break; } default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: do { break; } while (a); default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0:\n\n default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: {} default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: a(); { /* falls through */ } default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: { /* falls through */ } a(); default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: if (a) { /* falls through */ } default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: { { /* falls through */ } } default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: { /* comment */ } default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0:\n // comment\n default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: a(); /* falling through */ default: b() }",
errors: errorsDefault
},
{
code: "switch(foo) { case 0: a();\n/* no break */\ncase 1: b(); }",
options: [{
commentPattern: "break omitted"
}],
errors: [
{
messageId: "case",
type: "SwitchCase",
line: 3,
column: 1
}
]
},
{
code: "switch(foo) { case 0: a();\n/* no break */\n/* todo: fix readability */\ndefault: b() }",
options: [{
commentPattern: "no break"
}],
errors: [
{
messageId: "default",
type: "SwitchCase",
line: 4,
column: 1
}
]
},
{
code: "switch(foo) { case 0: { a();\n/* no break */\n/* todo: fix readability */ }\ndefault: b() }",
options: [{
commentPattern: "no break"
}],
errors: [
{
messageId: "default",
type: "SwitchCase",
line: 4,
column: 1
}
]
}
]
});