forked from swc-project/swc
/
exhaustiveSwitchStatements1.1.normal.js
239 lines (239 loc) · 4.89 KB
/
exhaustiveSwitchStatements1.1.normal.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//// [exhaustiveSwitchStatements1.ts]
function f1(x) {
if (!!true) {
switch(x){
case 1:
return "a";
case 2:
return "b";
}
x; // Unreachable
} else {
throw 0;
}
}
function f2(x) {
var z;
switch(x){
case 1:
z = 10;
break;
case 2:
z = 20;
break;
}
z; // Definitely assigned
}
function f3(x) {
switch(x){
case 1:
return 10;
case 2:
return 20;
// Default considered reachable to allow defensive coding
default:
throw new Error("Bad input");
}
}
var // Repro from #11572
E;
(function(E) {
E[E["A"] = 0] = "A";
E[E["B"] = 1] = "B";
})(E || (E = {}));
function f(e) {
switch(e){
case E.A:
return 0;
case E.B:
return 1;
}
}
function g(e) {
if (!true) return -1;
else switch(e){
case E.A:
return 0;
case E.B:
return 1;
}
}
function area(s) {
var area;
switch(s.kind){
case "square":
area = s.size * s.size;
break;
case "rectangle":
area = s.width * s.height;
break;
case "circle":
area = Math.PI * s.radius * s.radius;
break;
case "triangle":
area = Math.sqrt(3) / 4 * s.side * s.side;
break;
}
return area;
}
function areaWrapped(s) {
var area;
area = function() {
switch(s.kind){
case "square":
return s.size * s.size;
case "rectangle":
return s.width * s.height;
case "circle":
return Math.PI * s.radius * s.radius;
case "triangle":
return Math.sqrt(3) / 4 * s.side * s.side;
}
}();
return area;
}
var // Repro from #13241
MyEnum;
(function(MyEnum) {
MyEnum[MyEnum["A"] = 0] = "A";
MyEnum[MyEnum["B"] = 1] = "B";
})(MyEnum || (MyEnum = {}));
function thisGivesError(e) {
var s;
switch(e){
case MyEnum.A:
s = "it was A";
break;
case MyEnum.B:
s = "it was B";
break;
}
return s;
}
function good1(e) {
var s;
switch(e){
case MyEnum.A:
s = "it was A";
break;
case MyEnum.B:
s = "it was B";
break;
default:
s = "it was something else";
break;
}
return s;
}
function good2(e) {
switch(e){
case MyEnum.A:
return "it was A";
case MyEnum.B:
return "it was B";
}
}
var // Repro from #18362
Level;
(function(Level) {
Level[Level["One"] = 0] = "One";
Level[Level["Two"] = 1] = "Two";
})(Level || (Level = {}));
var doSomethingWithLevel = function(level) {
var next;
switch(level){
case Level.One:
next = Level.Two;
break;
case Level.Two:
next = Level.One;
break;
}
return next;
};
function withDefault(s1, s2) {
switch(s1.kind){
case "square":
return "1";
case "circle":
switch(s2.kind){
case "square":
return "2";
case "circle":
return "3";
default:
return "never";
}
}
}
function withoutDefault(s1, s2) {
switch(s1.kind){
case "square":
return "1";
case "circle":
switch(s2.kind){
case "square":
return "2";
case "circle":
return "3";
}
}
}
// Repro from #20823
function test4(value) {
var x;
switch(value){
case 1:
x = "one";
break;
case 2:
x = "two";
break;
}
return x;
}
var // Repro from #34661
Animal;
(function(Animal) {
Animal[Animal["DOG"] = 0] = "DOG";
Animal[Animal["CAT"] = 1] = "CAT";
})(Animal || (Animal = {}));
function expression() {
var _zoo_animal;
switch((_zoo_animal = zoo === null || zoo === void 0 ? void 0 : zoo.animal) !== null && _zoo_animal !== void 0 ? _zoo_animal : Animal.DOG){
case Animal.DOG:
return Animal.DOG;
case Animal.CAT:
return Animal.CAT;
}
}
// Repro from #34840
function foo() {
var foo = 0;
while(true){
var stats = foo;
switch(stats){
case 1:
break;
case 2:
break;
}
}
}
function ff(o, k) {
switch(k){
case "c":
k = "a";
}
k === "c"; // Error
return o[k];
}
function f35431(a) {
switch(a.kind){
case "abc":
case "def":
return;
default:
a.kind; // Error expected
}
}