| 1 |
import * as evaluator from "../../_namespaces/evaluator"; |
| 2 |
import * as ts from "../../_namespaces/ts"; |
| 3 |
|
| 4 |
function FakeSuppressedError(error: any, suppressed: any) { |
| 5 |
return { error, suppressed }; |
| 6 |
} |
| 7 |
|
| 8 |
describe("unittests:: evaluation:: awaitUsingDeclarations", () => { |
| 9 |
it("'await using' in Block, normal completion (es2018)", async () => { |
| 10 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 11 |
export const output: any[] = []; |
| 12 |
|
| 13 |
const disposable = { |
| 14 |
async [Symbol.asyncDispose]() { |
| 15 |
output.push("disposed"); |
| 16 |
} |
| 17 |
}; |
| 18 |
|
| 19 |
function body() { |
| 20 |
output.push("body"); |
| 21 |
} |
| 22 |
|
| 23 |
export async function main() { |
| 24 |
output.push("before block"); |
| 25 |
{ |
| 26 |
output.push("enter block"); |
| 27 |
await using _ = disposable; |
| 28 |
body(); |
| 29 |
output.push("exit block"); |
| 30 |
} |
| 31 |
output.push("after block"); |
| 32 |
} |
| 33 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 34 |
|
| 35 |
await main(); |
| 36 |
|
| 37 |
assert.deepEqual(output, [ |
| 38 |
"before block", |
| 39 |
"enter block", |
| 40 |
"body", |
| 41 |
"exit block", |
| 42 |
"disposed", |
| 43 |
"after block" |
| 44 |
]); |
| 45 |
}); |
| 46 |
|
| 47 |
it("'await using' in Block, 'throw' in body (es2018)", async () => { |
| 48 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 49 |
export const output: any[] = []; |
| 50 |
|
| 51 |
const disposable = { |
| 52 |
async [Symbol.asyncDispose]() { |
| 53 |
output.push("disposed"); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
function body() { |
| 58 |
output.push("body"); |
| 59 |
throw "error"; |
| 60 |
} |
| 61 |
|
| 62 |
export async function main() { |
| 63 |
output.push("before try"); |
| 64 |
try { |
| 65 |
output.push("enter try"); |
| 66 |
await using _ = disposable; |
| 67 |
body(); |
| 68 |
output.push("exit try"); |
| 69 |
} |
| 70 |
catch (e) { |
| 71 |
output.push(e); |
| 72 |
} |
| 73 |
output.push("after try"); |
| 74 |
} |
| 75 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 76 |
|
| 77 |
await main(); |
| 78 |
|
| 79 |
assert.deepEqual(output, [ |
| 80 |
"before try", |
| 81 |
"enter try", |
| 82 |
"body", |
| 83 |
"disposed", |
| 84 |
"error", |
| 85 |
"after try" |
| 86 |
]); |
| 87 |
}); |
| 88 |
|
| 89 |
it("'await using' in Block, 'throw' in dispose (es2018)", async () => { |
| 90 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 91 |
export const output: any[] = []; |
| 92 |
|
| 93 |
const disposable = { |
| 94 |
async [Symbol.asyncDispose]() { |
| 95 |
output.push("disposed"); |
| 96 |
throw "error"; |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
function body() { |
| 101 |
output.push("body"); |
| 102 |
} |
| 103 |
|
| 104 |
export async function main() { |
| 105 |
output.push("before try"); |
| 106 |
try { |
| 107 |
output.push("enter try"); |
| 108 |
await using _ = disposable; |
| 109 |
body(); |
| 110 |
output.push("exit try"); |
| 111 |
} |
| 112 |
catch (e) { |
| 113 |
output.push(e); |
| 114 |
} |
| 115 |
output.push("after try"); |
| 116 |
} |
| 117 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 118 |
|
| 119 |
await main(); |
| 120 |
|
| 121 |
assert.deepEqual(output, [ |
| 122 |
"before try", |
| 123 |
"enter try", |
| 124 |
"body", |
| 125 |
"exit try", |
| 126 |
"disposed", |
| 127 |
"error", |
| 128 |
"after try" |
| 129 |
]); |
| 130 |
}); |
| 131 |
|
| 132 |
it("'await using' in Block, 'throw' in body and dispose (es2018)", async () => { |
| 133 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 134 |
export const output: any[] = []; |
| 135 |
|
| 136 |
const disposable = { |
| 137 |
async [Symbol.asyncDispose]() { |
| 138 |
output.push("disposed"); |
| 139 |
throw "dispose error"; |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
function body() { |
| 144 |
output.push("body"); |
| 145 |
throw "body error"; |
| 146 |
} |
| 147 |
|
| 148 |
export async function main() { |
| 149 |
output.push("before try"); |
| 150 |
try { |
| 151 |
output.push("enter try"); |
| 152 |
await using _ = disposable; |
| 153 |
body(); |
| 154 |
output.push("exit try"); |
| 155 |
} |
| 156 |
catch (e) { |
| 157 |
output.push(e); |
| 158 |
} |
| 159 |
output.push("after try"); |
| 160 |
} |
| 161 |
`, { target: ts.ScriptTarget.ES2018 }, { SuppressedError: FakeSuppressedError }); |
| 162 |
|
| 163 |
await main(); |
| 164 |
|
| 165 |
assert.deepEqual(output, [ |
| 166 |
"before try", |
| 167 |
"enter try", |
| 168 |
"body", |
| 169 |
"disposed", |
| 170 |
{ |
| 171 |
error: "dispose error", |
| 172 |
suppressed: "body error" |
| 173 |
}, |
| 174 |
"after try" |
| 175 |
]); |
| 176 |
}); |
| 177 |
|
| 178 |
it("'await using' in Block, 'throw' in body and dispose, no global SuppressedError (es2018)", async () => { |
| 179 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 180 |
export const output: any[] = []; |
| 181 |
|
| 182 |
const disposable = { |
| 183 |
async [Symbol.asyncDispose]() { |
| 184 |
output.push("disposed"); |
| 185 |
throw "dispose error"; |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
function body() { |
| 190 |
output.push("body"); |
| 191 |
throw "body error"; |
| 192 |
} |
| 193 |
|
| 194 |
export async function main() { |
| 195 |
output.push("before try"); |
| 196 |
try { |
| 197 |
output.push("enter try"); |
| 198 |
await using _ = disposable; |
| 199 |
body(); |
| 200 |
output.push("exit try"); |
| 201 |
} |
| 202 |
catch (e) { |
| 203 |
output.push(e); |
| 204 |
} |
| 205 |
output.push("after try"); |
| 206 |
} |
| 207 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 208 |
|
| 209 |
await main(); |
| 210 |
|
| 211 |
assert.deepEqual(output.slice(0, 4), [ |
| 212 |
"before try", |
| 213 |
"enter try", |
| 214 |
"body", |
| 215 |
"disposed"]); |
| 216 |
assert.instanceOf(output[4], Error); |
| 217 |
assert.strictEqual(output[4].name, "SuppressedError"); |
| 218 |
assert.strictEqual(output[4].error, "dispose error"); |
| 219 |
assert.strictEqual(output[4].suppressed, "body error"); |
| 220 |
assert.deepEqual(output.slice(5), [ |
| 221 |
"after try" |
| 222 |
]); |
| 223 |
}); |
| 224 |
|
| 225 |
it("'await using' in Block, 'return' in body (es2018)", async () => { |
| 226 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 227 |
export const output: any[] = []; |
| 228 |
|
| 229 |
const disposable = { |
| 230 |
async [Symbol.asyncDispose]() { |
| 231 |
output.push("disposed"); |
| 232 |
} |
| 233 |
}; |
| 234 |
|
| 235 |
function body() { |
| 236 |
output.push("body"); |
| 237 |
} |
| 238 |
|
| 239 |
export async function main() { |
| 240 |
output.push("before block"); |
| 241 |
{ |
| 242 |
output.push("enter block"); |
| 243 |
await using _ = disposable; |
| 244 |
body(); |
| 245 |
return; |
| 246 |
} |
| 247 |
output.push("after block"); |
| 248 |
} |
| 249 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 250 |
|
| 251 |
await main(); |
| 252 |
|
| 253 |
assert.deepEqual(output, [ |
| 254 |
"before block", |
| 255 |
"enter block", |
| 256 |
"body", |
| 257 |
"disposed" |
| 258 |
]); |
| 259 |
}); |
| 260 |
|
| 261 |
it("'await using' in Block, 'break' in body (es2018)", async () => { |
| 262 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 263 |
export const output: any[] = []; |
| 264 |
|
| 265 |
const disposable = { |
| 266 |
async [Symbol.asyncDispose]() { |
| 267 |
output.push("disposed"); |
| 268 |
} |
| 269 |
}; |
| 270 |
|
| 271 |
function body() { |
| 272 |
output.push("body"); |
| 273 |
} |
| 274 |
|
| 275 |
export async function main() { |
| 276 |
output.push("before block"); |
| 277 |
for (let i = 0; i < 2; i++) { |
| 278 |
output.push("enter block"); |
| 279 |
await using _ = disposable; |
| 280 |
body(); |
| 281 |
break; |
| 282 |
} |
| 283 |
output.push("after block"); |
| 284 |
} |
| 285 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 286 |
|
| 287 |
await main(); |
| 288 |
|
| 289 |
assert.deepEqual(output, [ |
| 290 |
"before block", |
| 291 |
"enter block", |
| 292 |
"body", |
| 293 |
"disposed", |
| 294 |
"after block" |
| 295 |
]); |
| 296 |
}); |
| 297 |
|
| 298 |
it("'await using' in Block, 'continue' in body (es2018)", async () => { |
| 299 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 300 |
export const output: any[] = []; |
| 301 |
|
| 302 |
const disposable = { |
| 303 |
async [Symbol.asyncDispose]() { |
| 304 |
output.push("disposed"); |
| 305 |
} |
| 306 |
}; |
| 307 |
|
| 308 |
function body() { |
| 309 |
output.push("body"); |
| 310 |
} |
| 311 |
|
| 312 |
export async function main() { |
| 313 |
output.push("before block"); |
| 314 |
for (let i = 0; i < 2; i++) { |
| 315 |
output.push("enter block"); |
| 316 |
await using _ = disposable; |
| 317 |
body(); |
| 318 |
continue; |
| 319 |
} |
| 320 |
output.push("after block"); |
| 321 |
} |
| 322 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 323 |
|
| 324 |
await main(); |
| 325 |
|
| 326 |
assert.deepEqual(output, [ |
| 327 |
"before block", |
| 328 |
"enter block", |
| 329 |
"body", |
| 330 |
"disposed", |
| 331 |
"enter block", |
| 332 |
"body", |
| 333 |
"disposed", |
| 334 |
"after block" |
| 335 |
]); |
| 336 |
}); |
| 337 |
|
| 338 |
it("'await using' in head of 'for', normal completion (es2018)", async () => { |
| 339 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 340 |
export const output: any[] = []; |
| 341 |
|
| 342 |
const disposable = { |
| 343 |
async [Symbol.asyncDispose]() { |
| 344 |
output.push("disposed"); |
| 345 |
} |
| 346 |
}; |
| 347 |
|
| 348 |
function body() { |
| 349 |
output.push("body"); |
| 350 |
} |
| 351 |
|
| 352 |
export async function main() { |
| 353 |
output.push("before loop"); |
| 354 |
let i = 0; |
| 355 |
for (await using _ = disposable; i < 2; i++) { |
| 356 |
output.push("enter loop"); |
| 357 |
body(); |
| 358 |
output.push("exit loop"); |
| 359 |
} |
| 360 |
output.push("after loop"); |
| 361 |
} |
| 362 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 363 |
|
| 364 |
await main(); |
| 365 |
|
| 366 |
assert.deepEqual(output, [ |
| 367 |
"before loop", |
| 368 |
"enter loop", |
| 369 |
"body", |
| 370 |
"exit loop", |
| 371 |
"enter loop", |
| 372 |
"body", |
| 373 |
"exit loop", |
| 374 |
"disposed", |
| 375 |
"after loop" |
| 376 |
]); |
| 377 |
}); |
| 378 |
|
| 379 |
it("'await using' in head of 'for', 'throw' in body (es2018)", async () => { |
| 380 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 381 |
export const output: any[] = []; |
| 382 |
|
| 383 |
const disposable = { |
| 384 |
async [Symbol.asyncDispose]() { |
| 385 |
output.push("disposed"); |
| 386 |
} |
| 387 |
}; |
| 388 |
|
| 389 |
function body() { |
| 390 |
output.push("body"); |
| 391 |
throw "error"; |
| 392 |
} |
| 393 |
|
| 394 |
export async function main() { |
| 395 |
output.push("before loop"); |
| 396 |
let i = 0; |
| 397 |
try { |
| 398 |
for (await using _ = disposable; i < 2; i++) { |
| 399 |
output.push("enter loop"); |
| 400 |
body(); |
| 401 |
output.push("exit loop"); |
| 402 |
} |
| 403 |
} catch (e) { |
| 404 |
output.push(e); |
| 405 |
} |
| 406 |
output.push("after loop"); |
| 407 |
} |
| 408 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 409 |
|
| 410 |
await main(); |
| 411 |
|
| 412 |
assert.deepEqual(output, [ |
| 413 |
"before loop", |
| 414 |
"enter loop", |
| 415 |
"body", |
| 416 |
"disposed", |
| 417 |
"error", |
| 418 |
"after loop" |
| 419 |
]); |
| 420 |
}); |
| 421 |
|
| 422 |
it("'await using' in head of 'for', 'throw' in dispose (es2018)", async () => { |
| 423 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 424 |
export const output: any[] = []; |
| 425 |
|
| 426 |
const disposable = { |
| 427 |
async [Symbol.asyncDispose]() { |
| 428 |
output.push("disposed"); |
| 429 |
throw "error"; |
| 430 |
} |
| 431 |
}; |
| 432 |
|
| 433 |
function body() { |
| 434 |
output.push("body"); |
| 435 |
} |
| 436 |
|
| 437 |
export async function main() { |
| 438 |
output.push("before loop"); |
| 439 |
let i = 0; |
| 440 |
try { |
| 441 |
for (await using _ = disposable; i < 2; i++) { |
| 442 |
output.push("enter loop"); |
| 443 |
body(); |
| 444 |
output.push("exit loop"); |
| 445 |
} |
| 446 |
} catch (e) { |
| 447 |
output.push(e); |
| 448 |
} |
| 449 |
output.push("after loop"); |
| 450 |
} |
| 451 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 452 |
|
| 453 |
await main(); |
| 454 |
|
| 455 |
assert.deepEqual(output, [ |
| 456 |
"before loop", |
| 457 |
"enter loop", |
| 458 |
"body", |
| 459 |
"exit loop", |
| 460 |
"enter loop", |
| 461 |
"body", |
| 462 |
"exit loop", |
| 463 |
"disposed", |
| 464 |
"error", |
| 465 |
"after loop" |
| 466 |
]); |
| 467 |
}); |
| 468 |
|
| 469 |
it("'await using' in head of 'for', 'throw' in body and dispose (es2018)", async () => { |
| 470 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 471 |
export const output: any[] = []; |
| 472 |
|
| 473 |
const disposable = { |
| 474 |
async [Symbol.asyncDispose]() { |
| 475 |
output.push("disposed"); |
| 476 |
throw "dispose error"; |
| 477 |
} |
| 478 |
}; |
| 479 |
|
| 480 |
function body() { |
| 481 |
output.push("body"); |
| 482 |
throw "body error"; |
| 483 |
} |
| 484 |
|
| 485 |
export async function main() { |
| 486 |
output.push("before loop"); |
| 487 |
let i = 0; |
| 488 |
try { |
| 489 |
for (await using _ = disposable; i < 2; i++) { |
| 490 |
output.push("enter loop"); |
| 491 |
body(); |
| 492 |
output.push("exit loop"); |
| 493 |
} |
| 494 |
} catch (e) { |
| 495 |
output.push(e); |
| 496 |
} |
| 497 |
output.push("after loop"); |
| 498 |
} |
| 499 |
`, { target: ts.ScriptTarget.ES2018 }, { SuppressedError: FakeSuppressedError }); |
| 500 |
|
| 501 |
await main(); |
| 502 |
|
| 503 |
assert.deepEqual(output, [ |
| 504 |
"before loop", |
| 505 |
"enter loop", |
| 506 |
"body", |
| 507 |
"disposed", |
| 508 |
{ |
| 509 |
error: "dispose error", |
| 510 |
suppressed: "body error" |
| 511 |
}, |
| 512 |
"after loop" |
| 513 |
]); |
| 514 |
}); |
| 515 |
|
| 516 |
it("'await using' in head of 'for', 'return' in body (es2018)", async () => { |
| 517 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 518 |
export const output: any[] = []; |
| 519 |
|
| 520 |
const disposable = { |
| 521 |
async [Symbol.asyncDispose]() { |
| 522 |
output.push("disposed"); |
| 523 |
} |
| 524 |
}; |
| 525 |
|
| 526 |
function body() { |
| 527 |
output.push("body"); |
| 528 |
} |
| 529 |
|
| 530 |
export async function main() { |
| 531 |
output.push("before loop"); |
| 532 |
let i = 0; |
| 533 |
for (await using _ = disposable; i < 2; i++) { |
| 534 |
output.push("enter loop"); |
| 535 |
body(); |
| 536 |
return; |
| 537 |
} |
| 538 |
output.push("after loop"); |
| 539 |
} |
| 540 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 541 |
|
| 542 |
await main(); |
| 543 |
|
| 544 |
assert.deepEqual(output, [ |
| 545 |
"before loop", |
| 546 |
"enter loop", |
| 547 |
"body", |
| 548 |
"disposed" |
| 549 |
]); |
| 550 |
}); |
| 551 |
|
| 552 |
it("'await using' in head of 'for', 'break' in body (es2018)", async () => { |
| 553 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 554 |
export const output: any[] = []; |
| 555 |
|
| 556 |
const disposable = { |
| 557 |
async [Symbol.asyncDispose]() { |
| 558 |
output.push("disposed"); |
| 559 |
} |
| 560 |
}; |
| 561 |
|
| 562 |
function body() { |
| 563 |
output.push("body"); |
| 564 |
} |
| 565 |
|
| 566 |
export async function main() { |
| 567 |
output.push("before loop"); |
| 568 |
let i = 0; |
| 569 |
for (await using _ = disposable; i < 2; i++) { |
| 570 |
output.push("enter loop"); |
| 571 |
body(); |
| 572 |
break; |
| 573 |
} |
| 574 |
output.push("after loop"); |
| 575 |
} |
| 576 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 577 |
|
| 578 |
await main(); |
| 579 |
|
| 580 |
assert.deepEqual(output, [ |
| 581 |
"before loop", |
| 582 |
"enter loop", |
| 583 |
"body", |
| 584 |
"disposed", |
| 585 |
"after loop" |
| 586 |
]); |
| 587 |
}); |
| 588 |
|
| 589 |
it("'await using' in head of 'for', 'continue' in body (es2018)", async () => { |
| 590 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 591 |
export const output: any[] = []; |
| 592 |
|
| 593 |
const disposable = { |
| 594 |
async [Symbol.asyncDispose]() { |
| 595 |
output.push("disposed"); |
| 596 |
} |
| 597 |
}; |
| 598 |
|
| 599 |
function body() { |
| 600 |
output.push("body"); |
| 601 |
} |
| 602 |
|
| 603 |
export async function main() { |
| 604 |
output.push("before loop"); |
| 605 |
let i = 0; |
| 606 |
for (await using _ = disposable; i < 2; i++) { |
| 607 |
output.push("enter loop"); |
| 608 |
body(); |
| 609 |
continue; |
| 610 |
} |
| 611 |
output.push("after loop"); |
| 612 |
} |
| 613 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 614 |
|
| 615 |
await main(); |
| 616 |
|
| 617 |
assert.deepEqual(output, [ |
| 618 |
"before loop", |
| 619 |
"enter loop", |
| 620 |
"body", |
| 621 |
"enter loop", |
| 622 |
"body", |
| 623 |
"disposed", |
| 624 |
"after loop" |
| 625 |
]); |
| 626 |
}); |
| 627 |
|
| 628 |
it("'await using' in head of 'for', multiple iterations (es2018)", async () => { |
| 629 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 630 |
export const output: any[] = []; |
| 631 |
|
| 632 |
const disposable = { |
| 633 |
async [Symbol.asyncDispose]() { |
| 634 |
output.push("disposed"); |
| 635 |
} |
| 636 |
}; |
| 637 |
|
| 638 |
function body() { |
| 639 |
output.push("body"); |
| 640 |
} |
| 641 |
|
| 642 |
export async function main() { |
| 643 |
output.push("before loop"); |
| 644 |
let i = 0; |
| 645 |
for (await using _ = disposable; i < 2; i++) { |
| 646 |
output.push("enter loop"); |
| 647 |
body(); |
| 648 |
output.push("exit loop"); |
| 649 |
} |
| 650 |
output.push("after loop"); |
| 651 |
} |
| 652 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 653 |
|
| 654 |
await main(); |
| 655 |
|
| 656 |
assert.deepEqual(output, [ |
| 657 |
"before loop", |
| 658 |
"enter loop", |
| 659 |
"body", |
| 660 |
"exit loop", |
| 661 |
"enter loop", |
| 662 |
"body", |
| 663 |
"exit loop", |
| 664 |
"disposed", |
| 665 |
"after loop" |
| 666 |
]); |
| 667 |
}); |
| 668 |
|
| 669 |
it("'await using' in head of 'for-of', normal completion (es2018)", async () => { |
| 670 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 671 |
export const output: any[] = []; |
| 672 |
|
| 673 |
function* g() { |
| 674 |
yield { |
| 675 |
async [Symbol.asyncDispose]() { |
| 676 |
output.push("a disposed"); |
| 677 |
} |
| 678 |
}; |
| 679 |
yield { |
| 680 |
async [Symbol.asyncDispose]() { |
| 681 |
output.push("b disposed"); |
| 682 |
} |
| 683 |
}; |
| 684 |
} |
| 685 |
|
| 686 |
function body() { |
| 687 |
output.push("body"); |
| 688 |
} |
| 689 |
|
| 690 |
export async function main() { |
| 691 |
output.push("before loop"); |
| 692 |
for (await using _ of g()) { |
| 693 |
output.push("enter loop"); |
| 694 |
body(); |
| 695 |
output.push("exit loop"); |
| 696 |
} |
| 697 |
output.push("after loop"); |
| 698 |
} |
| 699 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 700 |
|
| 701 |
await main(); |
| 702 |
|
| 703 |
assert.deepEqual(output, [ |
| 704 |
"before loop", |
| 705 |
"enter loop", |
| 706 |
"body", |
| 707 |
"exit loop", |
| 708 |
"a disposed", |
| 709 |
"enter loop", |
| 710 |
"body", |
| 711 |
"exit loop", |
| 712 |
"b disposed", |
| 713 |
"after loop" |
| 714 |
]); |
| 715 |
}); |
| 716 |
|
| 717 |
it("'await using' in head of 'for-of', 'throw' in body (es2018)", async () => { |
| 718 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 719 |
export const output: any[] = []; |
| 720 |
|
| 721 |
function* g() { |
| 722 |
yield { |
| 723 |
async [Symbol.asyncDispose]() { |
| 724 |
output.push("a disposed"); |
| 725 |
} |
| 726 |
}; |
| 727 |
yield { |
| 728 |
async [Symbol.asyncDispose]() { |
| 729 |
output.push("b disposed"); |
| 730 |
} |
| 731 |
}; |
| 732 |
} |
| 733 |
|
| 734 |
function body() { |
| 735 |
output.push("body"); |
| 736 |
throw "error"; |
| 737 |
} |
| 738 |
|
| 739 |
export async function main() { |
| 740 |
output.push("before loop"); |
| 741 |
try { |
| 742 |
for (await using _ of g()) { |
| 743 |
output.push("enter loop"); |
| 744 |
body(); |
| 745 |
output.push("exit loop"); |
| 746 |
} |
| 747 |
} |
| 748 |
catch (e) { |
| 749 |
output.push(e); |
| 750 |
} |
| 751 |
output.push("after loop"); |
| 752 |
} |
| 753 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 754 |
|
| 755 |
await main(); |
| 756 |
|
| 757 |
assert.deepEqual(output, [ |
| 758 |
"before loop", |
| 759 |
"enter loop", |
| 760 |
"body", |
| 761 |
"a disposed", |
| 762 |
"error", |
| 763 |
"after loop", |
| 764 |
]); |
| 765 |
}); |
| 766 |
|
| 767 |
it("'await using' in head of 'for-of', 'throw' in dispose (es2018)", async () => { |
| 768 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 769 |
export const output: any[] = []; |
| 770 |
|
| 771 |
function* g() { |
| 772 |
yield { |
| 773 |
async [Symbol.asyncDispose]() { |
| 774 |
output.push("a disposed"); |
| 775 |
throw "error"; |
| 776 |
} |
| 777 |
}; |
| 778 |
yield { |
| 779 |
async [Symbol.asyncDispose]() { |
| 780 |
output.push("b disposed"); |
| 781 |
} |
| 782 |
}; |
| 783 |
} |
| 784 |
|
| 785 |
function body() { |
| 786 |
output.push("body"); |
| 787 |
} |
| 788 |
|
| 789 |
export async function main() { |
| 790 |
output.push("before loop"); |
| 791 |
try { |
| 792 |
for (await using _ of g()) { |
| 793 |
output.push("enter loop"); |
| 794 |
body(); |
| 795 |
output.push("exit loop"); |
| 796 |
} |
| 797 |
} |
| 798 |
catch (e) { |
| 799 |
output.push(e); |
| 800 |
} |
| 801 |
output.push("after loop"); |
| 802 |
} |
| 803 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 804 |
|
| 805 |
await main(); |
| 806 |
|
| 807 |
assert.deepEqual(output, [ |
| 808 |
"before loop", |
| 809 |
"enter loop", |
| 810 |
"body", |
| 811 |
"exit loop", |
| 812 |
"a disposed", |
| 813 |
"error", |
| 814 |
"after loop", |
| 815 |
]); |
| 816 |
}); |
| 817 |
|
| 818 |
it("'await using' in head of 'for-of', 'throw' in body and dispose (es2018)", async () => { |
| 819 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 820 |
export const output: any[] = []; |
| 821 |
|
| 822 |
function* g() { |
| 823 |
yield { |
| 824 |
async [Symbol.asyncDispose]() { |
| 825 |
output.push("a disposed"); |
| 826 |
throw "dispose error"; |
| 827 |
} |
| 828 |
}; |
| 829 |
yield { |
| 830 |
async [Symbol.asyncDispose]() { |
| 831 |
output.push("b disposed"); |
| 832 |
} |
| 833 |
}; |
| 834 |
} |
| 835 |
|
| 836 |
function body() { |
| 837 |
output.push("body"); |
| 838 |
throw "body error"; |
| 839 |
} |
| 840 |
|
| 841 |
export async function main() { |
| 842 |
output.push("before loop"); |
| 843 |
try { |
| 844 |
for (await using _ of g()) { |
| 845 |
output.push("enter loop"); |
| 846 |
body(); |
| 847 |
output.push("exit loop"); |
| 848 |
} |
| 849 |
} |
| 850 |
catch (e) { |
| 851 |
output.push(e); |
| 852 |
} |
| 853 |
output.push("after loop"); |
| 854 |
} |
| 855 |
`, { target: ts.ScriptTarget.ES2018 }, { SuppressedError: FakeSuppressedError }); |
| 856 |
|
| 857 |
await main(); |
| 858 |
|
| 859 |
assert.deepEqual(output, [ |
| 860 |
"before loop", |
| 861 |
"enter loop", |
| 862 |
"body", |
| 863 |
"a disposed", |
| 864 |
{ |
| 865 |
error: "dispose error", |
| 866 |
suppressed: "body error" |
| 867 |
}, |
| 868 |
"after loop", |
| 869 |
]); |
| 870 |
}); |
| 871 |
|
| 872 |
it("'await using' in head of 'for-of', 'return' in body (es2018)", async () => { |
| 873 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 874 |
export const output: any[] = []; |
| 875 |
|
| 876 |
function* g() { |
| 877 |
yield { |
| 878 |
async [Symbol.asyncDispose]() { |
| 879 |
output.push("a disposed"); |
| 880 |
} |
| 881 |
}; |
| 882 |
yield { |
| 883 |
async [Symbol.asyncDispose]() { |
| 884 |
output.push("b disposed"); |
| 885 |
} |
| 886 |
}; |
| 887 |
} |
| 888 |
|
| 889 |
function body() { |
| 890 |
output.push("body"); |
| 891 |
} |
| 892 |
|
| 893 |
export async function main() { |
| 894 |
output.push("before loop"); |
| 895 |
for (await using _ of g()) { |
| 896 |
output.push("enter loop"); |
| 897 |
body(); |
| 898 |
return; |
| 899 |
} |
| 900 |
output.push("after loop"); |
| 901 |
} |
| 902 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 903 |
|
| 904 |
await main(); |
| 905 |
|
| 906 |
assert.deepEqual(output, [ |
| 907 |
"before loop", |
| 908 |
"enter loop", |
| 909 |
"body", |
| 910 |
"a disposed", |
| 911 |
]); |
| 912 |
}); |
| 913 |
|
| 914 |
it("'await using' in head of 'for-of', 'break' in body (es2018)", async () => { |
| 915 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 916 |
export const output: any[] = []; |
| 917 |
|
| 918 |
function* g() { |
| 919 |
yield { |
| 920 |
async [Symbol.asyncDispose]() { |
| 921 |
output.push("a disposed"); |
| 922 |
} |
| 923 |
}; |
| 924 |
yield { |
| 925 |
async [Symbol.asyncDispose]() { |
| 926 |
output.push("b disposed"); |
| 927 |
} |
| 928 |
}; |
| 929 |
} |
| 930 |
|
| 931 |
function body() { |
| 932 |
output.push("body"); |
| 933 |
} |
| 934 |
|
| 935 |
export async function main() { |
| 936 |
output.push("before loop"); |
| 937 |
for (await using _ of g()) { |
| 938 |
output.push("enter loop"); |
| 939 |
body(); |
| 940 |
break; |
| 941 |
} |
| 942 |
output.push("after loop"); |
| 943 |
} |
| 944 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 945 |
|
| 946 |
await main(); |
| 947 |
|
| 948 |
assert.deepEqual(output, [ |
| 949 |
"before loop", |
| 950 |
"enter loop", |
| 951 |
"body", |
| 952 |
"a disposed", |
| 953 |
"after loop" |
| 954 |
]); |
| 955 |
}); |
| 956 |
|
| 957 |
it("'await using' in head of 'for-of', 'continue' in body (es2018)", async () => { |
| 958 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 959 |
export const output: any[] = []; |
| 960 |
|
| 961 |
function* g() { |
| 962 |
yield { |
| 963 |
async [Symbol.asyncDispose]() { |
| 964 |
output.push("a disposed"); |
| 965 |
} |
| 966 |
}; |
| 967 |
yield { |
| 968 |
async [Symbol.asyncDispose]() { |
| 969 |
output.push("b disposed"); |
| 970 |
} |
| 971 |
}; |
| 972 |
} |
| 973 |
|
| 974 |
function body() { |
| 975 |
output.push("body"); |
| 976 |
} |
| 977 |
|
| 978 |
export async function main() { |
| 979 |
output.push("before loop"); |
| 980 |
for (await using _ of g()) { |
| 981 |
output.push("enter loop"); |
| 982 |
body(); |
| 983 |
continue; |
| 984 |
} |
| 985 |
output.push("after loop"); |
| 986 |
} |
| 987 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 988 |
|
| 989 |
await main(); |
| 990 |
|
| 991 |
assert.deepEqual(output, [ |
| 992 |
"before loop", |
| 993 |
"enter loop", |
| 994 |
"body", |
| 995 |
"a disposed", |
| 996 |
"enter loop", |
| 997 |
"body", |
| 998 |
"b disposed", |
| 999 |
"after loop" |
| 1000 |
]); |
| 1001 |
}); |
| 1002 |
|
| 1003 |
it("'await using' in head of 'for-await-of', normal completion (es2018)", async () => { |
| 1004 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1005 |
export const output: any[] = []; |
| 1006 |
|
| 1007 |
function* g() { |
| 1008 |
yield { |
| 1009 |
async [Symbol.asyncDispose]() { |
| 1010 |
output.push("a disposed"); |
| 1011 |
} |
| 1012 |
}; |
| 1013 |
yield { |
| 1014 |
async [Symbol.asyncDispose]() { |
| 1015 |
output.push("b disposed"); |
| 1016 |
} |
| 1017 |
}; |
| 1018 |
} |
| 1019 |
|
| 1020 |
function body() { |
| 1021 |
output.push("body"); |
| 1022 |
} |
| 1023 |
|
| 1024 |
export async function main() { |
| 1025 |
output.push("before loop"); |
| 1026 |
for await (await using _ of g()) { |
| 1027 |
output.push("enter loop"); |
| 1028 |
body(); |
| 1029 |
output.push("exit loop"); |
| 1030 |
} |
| 1031 |
output.push("after loop"); |
| 1032 |
} |
| 1033 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1034 |
|
| 1035 |
await main(); |
| 1036 |
|
| 1037 |
assert.deepEqual(output, [ |
| 1038 |
"before loop", |
| 1039 |
"enter loop", |
| 1040 |
"body", |
| 1041 |
"exit loop", |
| 1042 |
"a disposed", |
| 1043 |
"enter loop", |
| 1044 |
"body", |
| 1045 |
"exit loop", |
| 1046 |
"b disposed", |
| 1047 |
"after loop" |
| 1048 |
]); |
| 1049 |
}); |
| 1050 |
|
| 1051 |
it("'await using' in head of 'for-await-of', 'throw' in body (es2018)", async () => { |
| 1052 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1053 |
export const output: any[] = []; |
| 1054 |
|
| 1055 |
function* g() { |
| 1056 |
yield { |
| 1057 |
async [Symbol.asyncDispose]() { |
| 1058 |
output.push("a disposed"); |
| 1059 |
} |
| 1060 |
}; |
| 1061 |
yield { |
| 1062 |
async [Symbol.asyncDispose]() { |
| 1063 |
output.push("b disposed"); |
| 1064 |
} |
| 1065 |
}; |
| 1066 |
} |
| 1067 |
|
| 1068 |
function body() { |
| 1069 |
output.push("body"); |
| 1070 |
throw "error"; |
| 1071 |
} |
| 1072 |
|
| 1073 |
export async function main() { |
| 1074 |
output.push("before loop"); |
| 1075 |
try { |
| 1076 |
for await (await using _ of g()) { |
| 1077 |
output.push("enter loop"); |
| 1078 |
body(); |
| 1079 |
output.push("exit loop"); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
catch (e) { |
| 1083 |
output.push(e); |
| 1084 |
} |
| 1085 |
output.push("after loop"); |
| 1086 |
} |
| 1087 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1088 |
|
| 1089 |
await main(); |
| 1090 |
|
| 1091 |
assert.deepEqual(output, [ |
| 1092 |
"before loop", |
| 1093 |
"enter loop", |
| 1094 |
"body", |
| 1095 |
"a disposed", |
| 1096 |
"error", |
| 1097 |
"after loop", |
| 1098 |
]); |
| 1099 |
}); |
| 1100 |
|
| 1101 |
it("'await using' in head of 'for-await-of', 'throw' in dispose (es2018)", async () => { |
| 1102 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1103 |
export const output: any[] = []; |
| 1104 |
|
| 1105 |
function* g() { |
| 1106 |
yield { |
| 1107 |
async [Symbol.asyncDispose]() { |
| 1108 |
output.push("a disposed"); |
| 1109 |
throw "error"; |
| 1110 |
} |
| 1111 |
}; |
| 1112 |
yield { |
| 1113 |
async [Symbol.asyncDispose]() { |
| 1114 |
output.push("b disposed"); |
| 1115 |
} |
| 1116 |
}; |
| 1117 |
} |
| 1118 |
|
| 1119 |
function body() { |
| 1120 |
output.push("body"); |
| 1121 |
} |
| 1122 |
|
| 1123 |
export async function main() { |
| 1124 |
output.push("before loop"); |
| 1125 |
try { |
| 1126 |
for await (await using _ of g()) { |
| 1127 |
output.push("enter loop"); |
| 1128 |
body(); |
| 1129 |
output.push("exit loop"); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
catch (e) { |
| 1133 |
output.push(e); |
| 1134 |
} |
| 1135 |
output.push("after loop"); |
| 1136 |
} |
| 1137 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1138 |
|
| 1139 |
await main(); |
| 1140 |
|
| 1141 |
assert.deepEqual(output, [ |
| 1142 |
"before loop", |
| 1143 |
"enter loop", |
| 1144 |
"body", |
| 1145 |
"exit loop", |
| 1146 |
"a disposed", |
| 1147 |
"error", |
| 1148 |
"after loop", |
| 1149 |
]); |
| 1150 |
}); |
| 1151 |
|
| 1152 |
it("'await using' in head of 'for-await-of', 'throw' in body and dispose (es2018)", async () => { |
| 1153 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1154 |
export const output: any[] = []; |
| 1155 |
|
| 1156 |
function* g() { |
| 1157 |
yield { |
| 1158 |
async [Symbol.asyncDispose]() { |
| 1159 |
output.push("a disposed"); |
| 1160 |
throw "dispose error"; |
| 1161 |
} |
| 1162 |
}; |
| 1163 |
yield { |
| 1164 |
async [Symbol.asyncDispose]() { |
| 1165 |
output.push("b disposed"); |
| 1166 |
} |
| 1167 |
}; |
| 1168 |
} |
| 1169 |
|
| 1170 |
function body() { |
| 1171 |
output.push("body"); |
| 1172 |
throw "body error"; |
| 1173 |
} |
| 1174 |
|
| 1175 |
export async function main() { |
| 1176 |
output.push("before loop"); |
| 1177 |
try { |
| 1178 |
for await (await using _ of g()) { |
| 1179 |
output.push("enter loop"); |
| 1180 |
body(); |
| 1181 |
output.push("exit loop"); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
catch (e) { |
| 1185 |
output.push(e); |
| 1186 |
} |
| 1187 |
output.push("after loop"); |
| 1188 |
} |
| 1189 |
`, { target: ts.ScriptTarget.ES2018 }, { SuppressedError: FakeSuppressedError }); |
| 1190 |
|
| 1191 |
await main(); |
| 1192 |
|
| 1193 |
assert.deepEqual(output, [ |
| 1194 |
"before loop", |
| 1195 |
"enter loop", |
| 1196 |
"body", |
| 1197 |
"a disposed", |
| 1198 |
{ |
| 1199 |
error: "dispose error", |
| 1200 |
suppressed: "body error" |
| 1201 |
}, |
| 1202 |
"after loop", |
| 1203 |
]); |
| 1204 |
}); |
| 1205 |
|
| 1206 |
it("'await using' in head of 'for-await-of', 'return' in body (es2018)", async () => { |
| 1207 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1208 |
export const output: any[] = []; |
| 1209 |
|
| 1210 |
function* g() { |
| 1211 |
yield { |
| 1212 |
async [Symbol.asyncDispose]() { |
| 1213 |
output.push("a disposed"); |
| 1214 |
} |
| 1215 |
}; |
| 1216 |
yield { |
| 1217 |
async [Symbol.asyncDispose]() { |
| 1218 |
output.push("b disposed"); |
| 1219 |
} |
| 1220 |
}; |
| 1221 |
} |
| 1222 |
|
| 1223 |
function body() { |
| 1224 |
output.push("body"); |
| 1225 |
} |
| 1226 |
|
| 1227 |
export async function main() { |
| 1228 |
output.push("before loop"); |
| 1229 |
for await (await using _ of g()) { |
| 1230 |
output.push("enter loop"); |
| 1231 |
body(); |
| 1232 |
return; |
| 1233 |
} |
| 1234 |
output.push("after loop"); |
| 1235 |
} |
| 1236 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1237 |
|
| 1238 |
await main(); |
| 1239 |
|
| 1240 |
assert.deepEqual(output, [ |
| 1241 |
"before loop", |
| 1242 |
"enter loop", |
| 1243 |
"body", |
| 1244 |
"a disposed", |
| 1245 |
]); |
| 1246 |
}); |
| 1247 |
|
| 1248 |
it("'await using' in head of 'for-await-of', 'break' in body (es2018)", async () => { |
| 1249 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1250 |
export const output: any[] = []; |
| 1251 |
|
| 1252 |
function* g() { |
| 1253 |
yield { |
| 1254 |
async [Symbol.asyncDispose]() { |
| 1255 |
output.push("a disposed"); |
| 1256 |
} |
| 1257 |
}; |
| 1258 |
yield { |
| 1259 |
async [Symbol.asyncDispose]() { |
| 1260 |
output.push("b disposed"); |
| 1261 |
} |
| 1262 |
}; |
| 1263 |
} |
| 1264 |
|
| 1265 |
function body() { |
| 1266 |
output.push("body"); |
| 1267 |
} |
| 1268 |
|
| 1269 |
export async function main() { |
| 1270 |
output.push("before loop"); |
| 1271 |
for await (await using _ of g()) { |
| 1272 |
output.push("enter loop"); |
| 1273 |
body(); |
| 1274 |
break; |
| 1275 |
} |
| 1276 |
output.push("after loop"); |
| 1277 |
} |
| 1278 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1279 |
|
| 1280 |
await main(); |
| 1281 |
|
| 1282 |
assert.deepEqual(output, [ |
| 1283 |
"before loop", |
| 1284 |
"enter loop", |
| 1285 |
"body", |
| 1286 |
"a disposed", |
| 1287 |
"after loop" |
| 1288 |
]); |
| 1289 |
}); |
| 1290 |
|
| 1291 |
it("'await using' in head of 'for-await-of', 'continue' in body (es2018)", async () => { |
| 1292 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1293 |
export const output: any[] = []; |
| 1294 |
|
| 1295 |
function* g() { |
| 1296 |
yield { |
| 1297 |
async [Symbol.asyncDispose]() { |
| 1298 |
output.push("a disposed"); |
| 1299 |
} |
| 1300 |
}; |
| 1301 |
yield { |
| 1302 |
async [Symbol.asyncDispose]() { |
| 1303 |
output.push("b disposed"); |
| 1304 |
} |
| 1305 |
}; |
| 1306 |
} |
| 1307 |
|
| 1308 |
function body() { |
| 1309 |
output.push("body"); |
| 1310 |
} |
| 1311 |
|
| 1312 |
export async function main() { |
| 1313 |
output.push("before loop"); |
| 1314 |
for await (await using _ of g()) { |
| 1315 |
output.push("enter loop"); |
| 1316 |
body(); |
| 1317 |
continue; |
| 1318 |
} |
| 1319 |
output.push("after loop"); |
| 1320 |
} |
| 1321 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1322 |
|
| 1323 |
await main(); |
| 1324 |
|
| 1325 |
assert.deepEqual(output, [ |
| 1326 |
"before loop", |
| 1327 |
"enter loop", |
| 1328 |
"body", |
| 1329 |
"a disposed", |
| 1330 |
"enter loop", |
| 1331 |
"body", |
| 1332 |
"b disposed", |
| 1333 |
"after loop" |
| 1334 |
]); |
| 1335 |
}); |
| 1336 |
|
| 1337 |
it("'await using' at top level of module (System)", async () => { |
| 1338 |
const { output, x, y } = await evaluator.evaluateTypeScript(` |
| 1339 |
export const output: any[] = []; |
| 1340 |
output.push("before export x"); |
| 1341 |
export const x = 1; |
| 1342 |
output.push("before using"); |
| 1343 |
await using _ = { |
| 1344 |
async [Symbol.asyncDispose]() { |
| 1345 |
output.push("disposed"); |
| 1346 |
} |
| 1347 |
}; |
| 1348 |
output.push("after using"); |
| 1349 |
export const y = 2; |
| 1350 |
output.push("after export y"); |
| 1351 |
`, { target: ts.ScriptTarget.ES2018, module: ts.ModuleKind.System }); |
| 1352 |
|
| 1353 |
assert.strictEqual(x, 1); |
| 1354 |
assert.strictEqual(y, 2); |
| 1355 |
assert.deepEqual(output, [ |
| 1356 |
"before export x", |
| 1357 |
"before using", |
| 1358 |
"after using", |
| 1359 |
"after export y", |
| 1360 |
"disposed" |
| 1361 |
]); |
| 1362 |
}); |
| 1363 |
|
| 1364 |
it("'await using' for 'null' value", async () => { |
| 1365 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1366 |
export const output: any[] = []; |
| 1367 |
|
| 1368 |
function body() { |
| 1369 |
output.push("body"); |
| 1370 |
} |
| 1371 |
|
| 1372 |
export async function main() { |
| 1373 |
output.push("before block"); |
| 1374 |
{ |
| 1375 |
output.push("enter block"); |
| 1376 |
await using _ = null; |
| 1377 |
body(); |
| 1378 |
output.push("exit block"); |
| 1379 |
} |
| 1380 |
output.push("after block"); |
| 1381 |
} |
| 1382 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1383 |
|
| 1384 |
await main(); |
| 1385 |
|
| 1386 |
assert.deepEqual(output, [ |
| 1387 |
"before block", |
| 1388 |
"enter block", |
| 1389 |
"body", |
| 1390 |
"exit block", |
| 1391 |
"after block" |
| 1392 |
]); |
| 1393 |
}); |
| 1394 |
|
| 1395 |
it("'await using' for 'undefined' value", async () => { |
| 1396 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1397 |
export const output: any[] = []; |
| 1398 |
|
| 1399 |
function body() { |
| 1400 |
output.push("body"); |
| 1401 |
} |
| 1402 |
|
| 1403 |
export async function main() { |
| 1404 |
output.push("before block"); |
| 1405 |
{ |
| 1406 |
output.push("enter block"); |
| 1407 |
await using _ = undefined; |
| 1408 |
body(); |
| 1409 |
output.push("exit block"); |
| 1410 |
} |
| 1411 |
output.push("after block"); |
| 1412 |
} |
| 1413 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1414 |
|
| 1415 |
await main(); |
| 1416 |
|
| 1417 |
assert.deepEqual(output, [ |
| 1418 |
"before block", |
| 1419 |
"enter block", |
| 1420 |
"body", |
| 1421 |
"exit block", |
| 1422 |
"after block" |
| 1423 |
]); |
| 1424 |
}); |
| 1425 |
|
| 1426 |
it("'await using' for sync disposable value", async () => { |
| 1427 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1428 |
export const output: any[] = []; |
| 1429 |
|
| 1430 |
const disposable = { |
| 1431 |
[Symbol.dispose]() { |
| 1432 |
output.push("disposed"); |
| 1433 |
} |
| 1434 |
}; |
| 1435 |
|
| 1436 |
function body() { |
| 1437 |
output.push("body"); |
| 1438 |
} |
| 1439 |
|
| 1440 |
export async function main() { |
| 1441 |
output.push("before block"); |
| 1442 |
{ |
| 1443 |
output.push("enter block"); |
| 1444 |
await using _ = disposable; |
| 1445 |
body(); |
| 1446 |
output.push("exit block"); |
| 1447 |
} |
| 1448 |
output.push("after block"); |
| 1449 |
} |
| 1450 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1451 |
|
| 1452 |
await main(); |
| 1453 |
|
| 1454 |
assert.deepEqual(output, [ |
| 1455 |
"before block", |
| 1456 |
"enter block", |
| 1457 |
"body", |
| 1458 |
"exit block", |
| 1459 |
"disposed", |
| 1460 |
"after block" |
| 1461 |
]); |
| 1462 |
}); |
| 1463 |
|
| 1464 |
it("'await using' for non-disposable value", async () => { |
| 1465 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1466 |
export const output: any[] = []; |
| 1467 |
|
| 1468 |
function body() { |
| 1469 |
output.push("body"); |
| 1470 |
} |
| 1471 |
|
| 1472 |
export async function main() { |
| 1473 |
output.push("before block"); |
| 1474 |
{ |
| 1475 |
output.push("enter block"); |
| 1476 |
await using _ = {} as any; |
| 1477 |
body(); |
| 1478 |
output.push("exit block"); |
| 1479 |
} |
| 1480 |
output.push("after block"); |
| 1481 |
} |
| 1482 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1483 |
|
| 1484 |
try { |
| 1485 |
await main(); |
| 1486 |
assert.fail("Expected 'main' to throw an error"); |
| 1487 |
} |
| 1488 |
catch { |
| 1489 |
// ignore |
| 1490 |
} |
| 1491 |
|
| 1492 |
assert.deepEqual(output, [ |
| 1493 |
"before block", |
| 1494 |
"enter block", |
| 1495 |
]); |
| 1496 |
}); |
| 1497 |
|
| 1498 |
it("'await using' disposes in reverse order", async () => { |
| 1499 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1500 |
export const output: any[] = []; |
| 1501 |
|
| 1502 |
const disposable_1 = { |
| 1503 |
async [Symbol.asyncDispose]() { |
| 1504 |
output.push("disposed_1"); |
| 1505 |
} |
| 1506 |
}; |
| 1507 |
const disposable_2 = { |
| 1508 |
async [Symbol.asyncDispose]() { |
| 1509 |
output.push("disposed_2"); |
| 1510 |
} |
| 1511 |
}; |
| 1512 |
|
| 1513 |
function body() { |
| 1514 |
output.push("body"); |
| 1515 |
} |
| 1516 |
|
| 1517 |
export async function main() { |
| 1518 |
output.push("before block"); |
| 1519 |
{ |
| 1520 |
output.push("enter block"); |
| 1521 |
await using _1 = disposable_1, _2 = disposable_2; |
| 1522 |
body(); |
| 1523 |
output.push("exit block"); |
| 1524 |
} |
| 1525 |
output.push("after block"); |
| 1526 |
} |
| 1527 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1528 |
|
| 1529 |
await main(); |
| 1530 |
|
| 1531 |
assert.deepEqual(output, [ |
| 1532 |
"before block", |
| 1533 |
"enter block", |
| 1534 |
"body", |
| 1535 |
"exit block", |
| 1536 |
"disposed_2", |
| 1537 |
"disposed_1", |
| 1538 |
"after block" |
| 1539 |
]); |
| 1540 |
}); |
| 1541 |
|
| 1542 |
it("'await using' + 'using' disposes in reverse order", async () => { |
| 1543 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1544 |
export const output: any[] = []; |
| 1545 |
|
| 1546 |
const disposable_1 = { |
| 1547 |
async [Symbol.asyncDispose]() { |
| 1548 |
output.push("disposed_1"); |
| 1549 |
} |
| 1550 |
}; |
| 1551 |
|
| 1552 |
const disposable_2 = { |
| 1553 |
[Symbol.dispose]() { |
| 1554 |
output.push("disposed_2"); |
| 1555 |
} |
| 1556 |
}; |
| 1557 |
|
| 1558 |
function body() { |
| 1559 |
output.push("body"); |
| 1560 |
} |
| 1561 |
|
| 1562 |
export async function main() { |
| 1563 |
output.push("before block"); |
| 1564 |
{ |
| 1565 |
output.push("enter block"); |
| 1566 |
await using _1 = disposable_1; |
| 1567 |
using _2 = disposable_2; |
| 1568 |
body(); |
| 1569 |
output.push("exit block"); |
| 1570 |
} |
| 1571 |
output.push("after block"); |
| 1572 |
} |
| 1573 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1574 |
|
| 1575 |
await main(); |
| 1576 |
|
| 1577 |
assert.deepEqual(output, [ |
| 1578 |
"before block", |
| 1579 |
"enter block", |
| 1580 |
"body", |
| 1581 |
"exit block", |
| 1582 |
"disposed_2", |
| 1583 |
"disposed_1", |
| 1584 |
"after block" |
| 1585 |
]); |
| 1586 |
}); |
| 1587 |
|
| 1588 |
it("'await using' forces await if null and evaluated", async () => { |
| 1589 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1590 |
export const output: any[] = []; |
| 1591 |
|
| 1592 |
function body() { |
| 1593 |
output.push("body"); |
| 1594 |
} |
| 1595 |
|
| 1596 |
async function a() { |
| 1597 |
output.push("before block"); |
| 1598 |
{ |
| 1599 |
output.push("enter block"); |
| 1600 |
await using _ = null; |
| 1601 |
body(); |
| 1602 |
output.push("exit block"); |
| 1603 |
} |
| 1604 |
output.push("after block"); |
| 1605 |
} |
| 1606 |
|
| 1607 |
function b() { |
| 1608 |
output.push("interleave"); |
| 1609 |
} |
| 1610 |
|
| 1611 |
export async function main() { |
| 1612 |
const p = a(); |
| 1613 |
b(); |
| 1614 |
await p; |
| 1615 |
} |
| 1616 |
|
| 1617 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1618 |
|
| 1619 |
await main(); |
| 1620 |
|
| 1621 |
assert.deepEqual(output, [ |
| 1622 |
"before block", |
| 1623 |
"enter block", |
| 1624 |
"body", |
| 1625 |
"exit block", |
| 1626 |
"interleave", |
| 1627 |
"after block" |
| 1628 |
]); |
| 1629 |
}); |
| 1630 |
|
| 1631 |
it("'await using' does not force await if null and not evaluated", async () => { |
| 1632 |
const { main, output } = evaluator.evaluateTypeScript(` |
| 1633 |
export const output: any[] = []; |
| 1634 |
|
| 1635 |
function body() { |
| 1636 |
output.push("body"); |
| 1637 |
} |
| 1638 |
|
| 1639 |
async function a(exitEarly: boolean) { |
| 1640 |
output.push("before block"); |
| 1641 |
block: { |
| 1642 |
output.push("enter block"); |
| 1643 |
if (exitEarly) break block; |
| 1644 |
await using _ = null; |
| 1645 |
body(); |
| 1646 |
output.push("exit block"); |
| 1647 |
} |
| 1648 |
output.push("after block"); |
| 1649 |
} |
| 1650 |
|
| 1651 |
function b() { |
| 1652 |
output.push("no interleave"); |
| 1653 |
} |
| 1654 |
|
| 1655 |
export async function main() { |
| 1656 |
const p = a(true); |
| 1657 |
b(); |
| 1658 |
await p; |
| 1659 |
} |
| 1660 |
|
| 1661 |
`, { target: ts.ScriptTarget.ES2018 }); |
| 1662 |
|
| 1663 |
await main(); |
| 1664 |
|
| 1665 |
assert.deepEqual(output, [ |
| 1666 |
"before block", |
| 1667 |
"enter block", |
| 1668 |
"after block", |
| 1669 |
"no interleave", |
| 1670 |
]); |
| 1671 |
}); |
| 1672 |
}); |