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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
describe("basic usage", () => {
test("disposes after block exit", () => {
let disposed = false;
let inBlock = false;
{
expect(disposed).toBeFalse();
using a = { [Symbol.dispose]() { disposed = true; } };
inBlock = true;
expect(disposed).toBeFalse();
}
expect(inBlock).toBeTrue();
expect(disposed).toBeTrue();
});
test("disposes in reverse order after block exit", () => {
const disposed = [];
{
expect(disposed).toHaveLength(0);
using a = { [Symbol.dispose]() { disposed.push('a'); } };
using b = { [Symbol.dispose]() { disposed.push('b'); } };
expect(disposed).toHaveLength(0);
}
expect(disposed).toEqual(['b', 'a']);
});
test("disposes in reverse order after block exit even in same declaration", () => {
const disposed = [];
{
expect(disposed).toHaveLength(0);
using a = { [Symbol.dispose]() { disposed.push('a'); } },
b = { [Symbol.dispose]() { disposed.push('b'); } };
expect(disposed).toHaveLength(0);
}
expect(disposed).toEqual(['b', 'a']);
});
});
describe("behavior with exceptions", () => {
function ExpectedError(name) { this.name = name; }
test("is run even after throw", () => {
let disposed = false;
let inBlock = false;
let inCatch = false;
try {
expect(disposed).toBeFalse();
using a = { [Symbol.dispose]() { disposed = true; } };
inBlock = true;
expect(disposed).toBeFalse();
throw new ExpectedError();
expect().fail();
} catch (e) {
expect(disposed).toBeTrue();
expect(e).toBeInstanceOf(ExpectedError);
inCatch = true;
}
expect(disposed).toBeTrue();
expect(inBlock).toBeTrue();
expect(inCatch).toBeTrue();
});
test("throws error if dispose method does", () => {
let disposed = false;
let endOfTry = false;
let inCatch = false;
try {
expect(disposed).toBeFalse();
using a = { [Symbol.dispose]() {
disposed = true;
throw new ExpectedError();
} };
expect(disposed).toBeFalse();
endOfTry = true;
} catch (e) {
expect(disposed).toBeTrue();
expect(e).toBeInstanceOf(ExpectedError);
inCatch = true;
}
expect(disposed).toBeTrue();
expect(endOfTry).toBeTrue();
expect(inCatch).toBeTrue();
});
test("if block and using throw get suppressed error", () => {
let disposed = false;
let inCatch = false;
try {
expect(disposed).toBeFalse();
using a = { [Symbol.dispose]() {
disposed = true;
throw new ExpectedError('dispose');
} };
expect(disposed).toBeFalse();
throw new ExpectedError('throw');
} catch (e) {
expect(disposed).toBeTrue();
expect(e).toBeInstanceOf(SuppressedError);
expect(e.error).toBeInstanceOf(ExpectedError);
expect(e.error.name).toBe('dispose');
expect(e.suppressed).toBeInstanceOf(ExpectedError);
expect(e.suppressed.name).toBe('throw');
inCatch = true;
}
expect(disposed).toBeTrue();
expect(inCatch).toBeTrue();
});
test("multiple throwing disposes give suppressed error", () => {
let inCatch = false;
try {
{
using a = { [Symbol.dispose]() {
throw new ExpectedError('a');
} };
using b = { [Symbol.dispose]() {
throw new ExpectedError('b');
} };
}
expect().fail();
} catch (e) {
expect(e).toBeInstanceOf(SuppressedError);
expect(e.error).toBeInstanceOf(ExpectedError);
expect(e.error.name).toBe('a');
expect(e.suppressed).toBeInstanceOf(ExpectedError);
expect(e.suppressed.name).toBe('b');
inCatch = true;
}
expect(inCatch).toBeTrue();
});
test("3 throwing disposes give chaining suppressed error", () => {
let inCatch = false;
try {
{
using a = { [Symbol.dispose]() {
throw new ExpectedError('a');
} };
using b = { [Symbol.dispose]() {
throw new ExpectedError('b');
} };
using c = { [Symbol.dispose]() {
throw new ExpectedError('c');
} };
}
expect().fail();
} catch (e) {
expect(e).toBeInstanceOf(SuppressedError);
expect(e.error).toBeInstanceOf(ExpectedError);
expect(e.error.name).toBe('a');
expect(e.suppressed).toBeInstanceOf(SuppressedError);
const inner = e.suppressed;
expect(inner.error).toBeInstanceOf(ExpectedError);
expect(inner.error.name).toBe('b');
expect(inner.suppressed).toBeInstanceOf(ExpectedError);
expect(inner.suppressed.name).toBe('c');
inCatch = true;
}
expect(inCatch).toBeTrue();
});
test("normal error and multiple disposing erorrs give chaining suppressed errors", () => {
let inCatch = false;
try {
using a = { [Symbol.dispose]() {
throw new ExpectedError('a');
} };
using b = { [Symbol.dispose]() {
throw new ExpectedError('b');
} };
throw new ExpectedError('top');
} catch (e) {
expect(e).toBeInstanceOf(SuppressedError);
expect(e.error).toBeInstanceOf(ExpectedError);
expect(e.error.name).toBe('a');
expect(e.suppressed).toBeInstanceOf(SuppressedError);
const inner = e.suppressed;
expect(inner.error).toBeInstanceOf(ExpectedError);
expect(inner.error.name).toBe('b');
expect(inner.suppressed).toBeInstanceOf(ExpectedError);
expect(inner.suppressed.name).toBe('top');
inCatch = true;
}
expect(inCatch).toBeTrue();
});
});
describe("works in a bunch of scopes", () => {
test("works in block", () => {
let dispose = false;
expect(dispose).toBeFalse();
{
expect(dispose).toBeFalse();
using a = { [Symbol.dispose]() { dispose = true; } }
expect(dispose).toBeFalse();
}
expect(dispose).toBeTrue();
});
test("works in static class block", () => {
let dispose = false;
expect(dispose).toBeFalse();
class A {
static {
expect(dispose).toBeFalse();
using a = { [Symbol.dispose]() { dispose = true; } }
expect(dispose).toBeFalse();
}
}
expect(dispose).toBeTrue();
});
test("works in function", () => {
let dispose = [];
function f(val) {
const disposeLength = dispose.length;
using a = { [Symbol.dispose]() { dispose.push(val); } }
expect(dispose.length).toBe(disposeLength);
}
expect(dispose).toEqual([]);
f(0);
expect(dispose).toEqual([0]);
f(1);
expect(dispose).toEqual([0, 1]);
});
test("switch block is treated as full block in function", () => {
let disposeFull = [];
let disposeInner = false;
function pusher(val) {
return {
val,
[Symbol.dispose]() { disposeFull.push(val); }
};
}
switch (2) {
case 3:
using notDisposed = { [Symbol.dispose]() { expect().fail("not-disposed 1"); } };
case 2:
expect(disposeFull).toEqual([]);
using a = pusher('a');
expect(disposeFull).toEqual([]);
using b = pusher('b');
expect(disposeFull).toEqual([]);
expect(b.val).toBe('b');
expect(disposeInner).toBeFalse();
// fallthrough
case 1: {
expect(disposeFull).toEqual([]);
expect(disposeInner).toBeFalse();
using inner = { [Symbol.dispose]() { disposeInner = true; } }
expect(disposeInner).toBeFalse();
}
expect(disposeInner).toBeTrue();
using c = pusher('c');
expect(c.val).toBe('c');
break;
case 0:
using notDisposed2 = { [Symbol.dispose]() { expect().fail("not-disposed 2"); } };
}
expect(disposeInner).toBeTrue();
expect(disposeFull).toEqual(['c', 'b', 'a']);
});
});
describe("invalid using bindings", () => {
test("nullish values do not throw", () => {
using a = null, b = undefined;
expect(a).toBeNull();
expect(b).toBeUndefined();
});
test("non-object throws", () => {
[0, "a", true, NaN, 4n, Symbol.dispose].forEach(value => {
expect(() => {
using v = value;
}).toThrowWithMessage(TypeError, "is not an object");
});
});
test("object without dispose throws", () => {
expect(() => {
using a = {};
}).toThrowWithMessage(TypeError, "does not have dispose method");
});
test("object with non callable dispose throws", () => {
[0, "a", true, NaN, 4n, Symbol.dispose, [], {}].forEach(value => {
expect(() => {
using a = { [Symbol.dispose]: value };
}).toThrowWithMessage(TypeError, "is not a function");
});
});
});
describe("using is still a valid variable name", () => {
test("var", () => {
"use strict";
var using = 1;
expect(using).toBe(1);
});
test("const", () => {
"use strict";
const using = 1;
expect(using).toBe(1);
});
test("let", () => {
"use strict";
let using = 1;
expect(using).toBe(1);
});
test("using", () => {
"use strict";
using using = null;
expect(using).toBeNull();
});
test("function", () => {
"use strict";
function using() { return 1; }
expect(using()).toBe(1);
});
});
describe("syntax errors / werid artifacts which remain valid", () => {
test("no patterns in using", () => {
expect("using {a} = {}").not.toEval();
expect("using a, {a} = {}").not.toEval();
expect("using a = null, [b] = [null]").not.toEval();
});
test("using with array pattern is valid array access", () => {
const using = [0, 9999];
const a = 1;
expect(eval("using [a] = 1")).toBe(1);
expect(using[1]).toBe(1);
expect(eval("using [{a: a}, a] = 2")).toBe(2);
expect(using[1]).toBe(2);
expect(eval("using [a, a] = 3")).toBe(3);
expect(using[1]).toBe(3);
expect(eval("using [[a, a], a] = 4")).toBe(4);
expect(using[1]).toBe(4);
expect(eval("using [2, 1, a] = 5")).toBe(5);
expect(using[1]).toBe(5);
});
test("declaration without initializer", () => {
expect("using a").not.toEval();
});
test("no repeat declarations in single using", () => {
expect("using a = null, a = null;").not.toEval();
});
test("cannot have a using declaration named let", () => {
expect("using let = null").not.toEval();
});
});
|