Tests for await using syntax (#14862)

* Add tests for `await using`

* Add tests for `await using` from Babel

* Add changelog

* Fix changelog
This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2023-05-29 20:50:18 +09:00
1 parent 16d327b989
commit 4d7afff735
17 files changed
+753 -2

No files matched your search

+9 -2
View File
@@ -1,6 +1,8 @@
#### Add support for "Explicit Resource Management" proposal (#13752 by @fisker)
#### Add support for "Explicit Resource Management" proposal (#13752 by @fisker, #14862 by @sosukesuzuki)
The Stage 2 proposal ["Explicit Resource Management"](https://github.com/tc39/proposal-explicit-resource-management/) is now supported via Babel 7.20.0. Also keep in mind our [policy on non-standardized syntax](https://prettier.io/docs/en/rationale.html#disclaimer-about-non-standard-syntax) before using this proposed syntax feature with Prettier.
The Stage 2 proposal ["Explicit Resource Management"](https://github.com/tc39/proposal-explicit-resource-management/) is now supported via Babel [7.20.0](https://babeljs.io/blog/2022/10/27/7.20.0) and [7.22.0](https://babeljs.io/blog/2023/05/26/7.22.0).
Also keep in mind our [policy on non-standardized syntax](https://prettier.io/docs/en/rationale.html#disclaimer-about-non-standard-syntax) before using this proposed syntax feature with Prettier.
<!-- prettier-ignore -->
```js
@@ -9,4 +11,9 @@ The Stage 2 proposal ["Explicit Resource Management"](https://github.com/tc39/pr
using obj = g(); // block-scoped declaration
const r = obj.next();
} // calls finally blocks in `g`
{
await using obj = g(); // block-scoped declaration
const r = obj.next();
} // calls finally blocks in `g`
```
@@ -860,6 +860,10 @@ function * g() {
const r = obj.next();
} // calls finally blocks in \`g\`
{
await using obj = g();
}
=====================================output=====================================
function* g() {
using handle = acquireFileHandle(); // block-scoped critical resource
@@ -870,6 +874,10 @@ function* g() {
const r = obj.next();
} // calls finally blocks in \`g\`
{
await using obj = g();
}
================================================================================
`;
@@ -6,3 +6,7 @@ function * g() {
using obj = g(); // block-scoped declaration
const r = obj.next();
} // calls finally blocks in `g`
{
await using obj = g();
}
@@ -252,6 +252,441 @@ for (using /* 1 */ foo of bar());
================================================================================
`;
exports[`valid-await-expr-using.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using[x];
await using.x + await using(x) ? await using?.x : await using\`x\`;
}
=====================================output=====================================
async function f() {
await using[x];
(await using.x) + (await using(x)) ? await using?.x : await using\`x\`;
}
================================================================================
`;
exports[`valid-await-expr-using-in.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using in foo;
}
=====================================output=====================================
async function f() {
(await using) in foo;
}
================================================================================
`;
exports[`valid-await-expr-using-instanceof.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using instanceof foo;
}
=====================================output=====================================
async function f() {
(await using) instanceof foo;
}
================================================================================
`;
exports[`valid-await-using-asi-assignment.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using
using = h();
}
=====================================output=====================================
async function f() {
await using;
using = h();
}
================================================================================
`;
exports[`valid-await-using-binding-basic.js [acorn] format 1`] = `
"Unexpected token (2:15)
1 | async function f() {
> 2 | await using basic = getReader();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-basic.js [espree] format 1`] = `
"Unexpected token basic (2:15)
1 | async function f() {
> 2 | await using basic = getReader();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-basic.js [flow] format 1`] = `
"Unexpected identifier, expected the end of an expression statement (\`;\`) (2:15)
1 | async function f() {
> 2 | await using basic = getReader();
| ^^^^^
3 | }
4 |"
`;
exports[`valid-await-using-binding-basic.js [meriyah] format 1`] = `
"Unexpected token: 'identifier' (2:19)
1 | async function f() {
> 2 | await using basic = getReader();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-basic.js [typescript] format 1`] = `
"';' expected. (2:15)
1 | async function f() {
> 2 | await using basic = getReader();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-basic.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using basic = getReader();
}
=====================================output=====================================
async function f() {
await using basic = getReader();
}
================================================================================
`;
exports[`valid-await-using-binding-escaped.js [acorn] format 1`] = `
"Unexpected token (2:15)
1 | async function f() {
> 2 | await using \\u0061b = c;
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-escaped.js [espree] format 1`] = `
"Unexpected token \\u0061b (2:15)
1 | async function f() {
> 2 | await using \\u0061b = c;
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-escaped.js [flow] format 1`] = `
"Unexpected identifier, expected the end of an expression statement (\`;\`) (2:15)
1 | async function f() {
> 2 | await using \\u0061b = c;
| ^^^^^^^
3 | }
4 |"
`;
exports[`valid-await-using-binding-escaped.js [meriyah] format 1`] = `
"Unexpected token: 'identifier' (2:15)
1 | async function f() {
> 2 | await using \\u0061b = c;
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-escaped.js [typescript] format 1`] = `
"';' expected. (2:15)
1 | async function f() {
> 2 | await using \\u0061b = c;
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-escaped.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using \\u0061b = c;
}
=====================================output=====================================
async function f() {
await using ab = c;
}
================================================================================
`;
exports[`valid-await-using-binding-non-bmp.js [acorn] format 1`] = `
"Unexpected token (2:15)
1 | async function f() {
> 2 | await using 𠮷 = foo();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-non-bmp.js [espree] format 1`] = `
"Unexpected token 𠮷 (2:15)
1 | async function f() {
> 2 | await using 𠮷 = foo();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-non-bmp.js [flow] format 1`] = `
"Unexpected identifier, expected the end of an expression statement (\`;\`) (2:15)
1 | async function f() {
> 2 | await using 𠮷 = foo();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-non-bmp.js [meriyah] format 1`] = `
"Unexpected token: 'identifier' (2:17)
1 | async function f() {
> 2 | await using 𠮷 = foo();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-non-bmp.js [typescript] format 1`] = `
"';' expected. (2:15)
1 | async function f() {
> 2 | await using 𠮷 = foo();
| ^
3 | }
4 |"
`;
exports[`valid-await-using-binding-non-bmp.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using 𠮷 = foo();
}
=====================================output=====================================
async function f() {
await using 𠮷 = foo();
}
================================================================================
`;
exports[`valid-await-using-binding-using.js [acorn] format 1`] = `
"Unexpected token (2:15)
1 | async function f() {
> 2 | await using using = of;
| ^
3 | for (await using using of of);
4 | }
5 |"
`;
exports[`valid-await-using-binding-using.js [espree] format 1`] = `
"Unexpected token using (2:15)
1 | async function f() {
> 2 | await using using = of;
| ^
3 | for (await using using of of);
4 | }
5 |"
`;
exports[`valid-await-using-binding-using.js [flow] format 1`] = `
"Unexpected identifier, expected the end of an expression statement (\`;\`) (2:15)
1 | async function f() {
> 2 | await using using = of;
| ^^^^^
3 | for (await using using of of);
4 | }
5 |"
`;
exports[`valid-await-using-binding-using.js [meriyah] format 1`] = `
"Unexpected token: 'identifier' (2:19)
1 | async function f() {
> 2 | await using using = of;
| ^
3 | for (await using using of of);
4 | }
5 |"
`;
exports[`valid-await-using-binding-using.js [typescript] format 1`] = `
"';' expected. (2:15)
1 | async function f() {
> 2 | await using using = of;
| ^
3 | for (await using using of of);
4 | }
5 |"
`;
exports[`valid-await-using-binding-using.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
await using using = of;
for (await using using of of);
}
=====================================output=====================================
async function f() {
await using using = of;
for (await using using of of);
}
================================================================================
`;
exports[`valid-await-using-comments.js [acorn] format 1`] = `
"Unexpected token (3:28)
1 | async function f() {
2 | {
> 3 | /*0*/await/*1*/using/*2*/b/*3*/=/*4*/f()/*5*/;
| ^
4 | }
5 | {
6 | /*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/=/*6*/x/*7*/;/*8*/;/*9*/)/*10*/;"
`;
exports[`valid-await-using-comments.js [espree] format 1`] = `
"Unexpected token b (3:28)
1 | async function f() {
2 | {
> 3 | /*0*/await/*1*/using/*2*/b/*3*/=/*4*/f()/*5*/;
| ^
4 | }
5 | {
6 | /*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/=/*6*/x/*7*/;/*8*/;/*9*/)/*10*/;"
`;
exports[`valid-await-using-comments.js [flow] format 1`] = `
"Unexpected identifier, expected the end of an expression statement (\`;\`) (3:28)
1 | async function f() {
2 | {
> 3 | /*0*/await/*1*/using/*2*/b/*3*/=/*4*/f()/*5*/;
| ^
4 | }
5 | {
6 | /*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/=/*6*/x/*7*/;/*8*/;/*9*/)/*10*/;"
`;
exports[`valid-await-using-comments.js [meriyah] format 1`] = `
"Unexpected token: 'identifier' (3:28)
1 | async function f() {
2 | {
> 3 | /*0*/await/*1*/using/*2*/b/*3*/=/*4*/f()/*5*/;
| ^
4 | }
5 | {
6 | /*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/=/*6*/x/*7*/;/*8*/;/*9*/)/*10*/;"
`;
exports[`valid-await-using-comments.js [typescript] format 1`] = `
"';' expected. (3:28)
1 | async function f() {
2 | {
> 3 | /*0*/await/*1*/using/*2*/b/*3*/=/*4*/f()/*5*/;
| ^
4 | }
5 | {
6 | /*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/=/*6*/x/*7*/;/*8*/;/*9*/)/*10*/;"
`;
exports[`valid-await-using-comments.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
{
/*0*/await/*1*/using/*2*/b/*3*/=/*4*/f()/*5*/;
}
{
/*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/=/*6*/x/*7*/;/*8*/;/*9*/)/*10*/;
}
{
/*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/of/*6*/x/*7*/)/*8*/;
}
{
/*0*/for/*1*/await/*2*/(/*3*/await/*4*/using/*5*/b/*6*/of/*7*/x/*8*/)/*9*/;
}
}
=====================================output=====================================
async function f() {
{
/*0*/ await using /*1*/ /*2*/ b /*3*/ = /*4*/ f(); /*5*/
}
{
/*0*/ for (
/*1*/ /*2*/ await using /*3*/ /*4*/ b /*5*/ =
/*6*/ x /*7*/ /*8*/ /*9*/ /*10*/;
;
);
}
{
/*0*/ for (/*1*/ /*2*/ await using /*3*/ /*4*/ b /*5*/ of /*6*/ x /*7*/ /*8*/);
}
{
/*0*/ for await (/*1*/ /*2*/ /*3*/ await using /*4*/ /*5*/ b /*6*/ of /*7*/ x /*8*/ /*9*/);
}
}
================================================================================
`;
exports[`valid-for-await-using-binding-escaped-of-of.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
@@ -284,6 +719,202 @@ printWidth: 80
================================================================================
`;
exports[`valid-for-using-binding-of-of.js [acorn] format 1`] = `
"Assigning to rvalue (2:8)
1 | async function f() {
> 2 | for (await using of of of);
| ^
3 | for await (await using of of of);
4 | }
5 |"
`;
exports[`valid-for-using-binding-of-of.js [espree] format 1`] = `
"Assigning to rvalue (2:8)
1 | async function f() {
> 2 | for (await using of of of);
| ^
3 | for await (await using of of of);
4 | }
5 |"
`;
exports[`valid-for-using-binding-of-of.js [flow] format 1`] = `
"Invalid left-hand side in for-of (2:8)
1 | async function f() {
> 2 | for (await using of of of);
| ^^^^^^^^^^^
3 | for await (await using of of of);
4 | }
5 |"
`;
exports[`valid-for-using-binding-of-of.js [meriyah] format 1`] = `
"Invalid left-hand side in for-of (2:21)
1 | async function f() {
> 2 | for (await using of of of);
| ^
3 | for await (await using of of of);
4 | }
5 |"
`;
exports[`valid-for-using-binding-of-of.js [typescript] format 1`] = `
"')' expected. (2:26)
1 | async function f() {
> 2 | for (await using of of of);
| ^
3 | for await (await using of of of);
4 | }
5 |"
`;
exports[`valid-for-using-binding-of-of.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
for (await using of of of);
for await (await using of of of);
}
=====================================output=====================================
async function f() {
for (await using of of of);
for await (await using of of of);
}
================================================================================
`;
exports[`valid-for-using-declaration.js [acorn] format 1`] = `
"Unexpected token (2:20)
1 | async function f() {
> 2 | for (await using basic = reader();;);
| ^
3 | }
4 |"
`;
exports[`valid-for-using-declaration.js [espree] format 1`] = `
"Unexpected token basic (2:20)
1 | async function f() {
> 2 | for (await using basic = reader();;);
| ^
3 | }
4 |"
`;
exports[`valid-for-using-declaration.js [flow] format 1`] = `
"Unexpected identifier, expected the token \`;\` (2:20)
1 | async function f() {
> 2 | for (await using basic = reader();;);
| ^^^^^
3 | }
4 |"
`;
exports[`valid-for-using-declaration.js [meriyah] format 1`] = `
"Expected ';' (2:24)
1 | async function f() {
> 2 | for (await using basic = reader();;);
| ^
3 | }
4 |"
`;
exports[`valid-for-using-declaration.js [typescript] format 1`] = `
"';' expected. (2:20)
1 | async function f() {
> 2 | for (await using basic = reader();;);
| ^
3 | }
4 |"
`;
exports[`valid-for-using-declaration.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
async function f() {
for (await using basic = reader();;);
}
=====================================output=====================================
async function f() {
for (await using basic = reader(); ; );
}
================================================================================
`;
exports[`valid-module-block-top-level-await-using-binding.js [acorn] format 1`] = `
"Unexpected token (1:18)
> 1 | const m = module {
| ^
2 | await using foo = bar();
3 | }
4 |"
`;
exports[`valid-module-block-top-level-await-using-binding.js [espree] format 1`] = `
"Unexpected token { (1:18)
> 1 | const m = module {
| ^
2 | await using foo = bar();
3 | }
4 |"
`;
exports[`valid-module-block-top-level-await-using-binding.js [flow] format 1`] = `
"Unexpected token \`{\`, expected the token \`;\` (1:18)
> 1 | const m = module {
| ^
2 | await using foo = bar();
3 | }
4 |"
`;
exports[`valid-module-block-top-level-await-using-binding.js [meriyah] format 1`] = `
"Unexpected token: '{' (1:18)
> 1 | const m = module {
| ^
2 | await using foo = bar();
3 | }
4 |"
`;
exports[`valid-module-block-top-level-await-using-binding.js [typescript] format 1`] = `
"',' expected. (1:18)
> 1 | const m = module {
| ^
2 | await using foo = bar();
3 | }
4 |"
`;
exports[`valid-module-block-top-level-await-using-binding.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
const m = module {
await using foo = bar();
}
=====================================output=====================================
const m = module {
await using foo = bar();
};
================================================================================
`;
exports[`valid-module-block-top-level-using-binding.js [acorn] format 1`] = `
"Unexpected token (1:8)
> 1 | module {
@@ -13,6 +13,16 @@ run_spec(import.meta, ["babel", "flow", "typescript"], {
"valid-using-binding-escaped.js",
"valid-using-binding-non-bmp.js",
"valid-using-binding-using.js",
"valid-await-using-binding-basic.js",
"valid-await-using-binding-escaped.js",
"valid-await-using-binding-non-bmp.js",
"valid-await-using-binding-using.js",
"valid-await-using-comments.js",
"valid-for-lhs-await-as-identifier.js",
"valid-for-using-binding-of-of.js",
"valid-for-using-declaration.js",
"valid-module-block-top-level-await-using-binding.js",
],
typescript: [
"for-await-using-of-comments.js",
@@ -26,6 +36,16 @@ run_spec(import.meta, ["babel", "flow", "typescript"], {
"valid-using-binding-escaped.js",
"valid-using-binding-non-bmp.js",
"valid-using-binding-using.js",
"valid-await-using-binding-basic.js",
"valid-await-using-binding-escaped.js",
"valid-await-using-binding-non-bmp.js",
"valid-await-using-binding-using.js",
"valid-await-using-comments.js",
"valid-for-lhs-await-as-identifier.js",
"valid-for-using-binding-of-of.js",
"valid-for-using-declaration.js",
"valid-module-block-top-level-await-using-binding.js",
],
espree: [
"for-await-using-of-comments.js",
@@ -39,6 +59,16 @@ run_spec(import.meta, ["babel", "flow", "typescript"], {
"valid-using-binding-escaped.js",
"valid-using-binding-non-bmp.js",
"valid-using-binding-using.js",
"valid-await-using-binding-basic.js",
"valid-await-using-binding-escaped.js",
"valid-await-using-binding-non-bmp.js",
"valid-await-using-binding-using.js",
"valid-await-using-comments.js",
"valid-for-lhs-await-as-identifier.js",
"valid-for-using-binding-of-of.js",
"valid-for-using-declaration.js",
"valid-module-block-top-level-await-using-binding.js",
],
meriyah: [
"for-await-using-of-comments.js",
@@ -52,6 +82,16 @@ run_spec(import.meta, ["babel", "flow", "typescript"], {
"valid-using-binding-escaped.js",
"valid-using-binding-non-bmp.js",
"valid-using-binding-using.js",
"valid-await-using-binding-basic.js",
"valid-await-using-binding-escaped.js",
"valid-await-using-binding-non-bmp.js",
"valid-await-using-binding-using.js",
"valid-await-using-comments.js",
"valid-for-lhs-await-as-identifier.js",
"valid-for-using-binding-of-of.js",
"valid-for-using-declaration.js",
"valid-module-block-top-level-await-using-binding.js",
],
acorn: [
"for-await-using-of-comments.js",
@@ -65,6 +105,16 @@ run_spec(import.meta, ["babel", "flow", "typescript"], {
"valid-using-binding-escaped.js",
"valid-using-binding-non-bmp.js",
"valid-using-binding-using.js",
"valid-await-using-binding-basic.js",
"valid-await-using-binding-escaped.js",
"valid-await-using-binding-non-bmp.js",
"valid-await-using-binding-using.js",
"valid-await-using-comments.js",
"valid-for-lhs-await-as-identifier.js",
"valid-for-using-binding-of-of.js",
"valid-for-using-declaration.js",
"valid-module-block-top-level-await-using-binding.js",
],
},
});
@@ -0,0 +1,3 @@
async function f() {
await using in foo;
}
@@ -0,0 +1,3 @@
async function f() {
await using instanceof foo;
}
@@ -0,0 +1,4 @@
async function f() {
await using[x];
await using.x + await using(x) ? await using?.x : await using`x`;
}
@@ -0,0 +1,4 @@
async function f() {
await using
using = h();
}
@@ -0,0 +1,3 @@
async function f() {
await using basic = getReader();
}
@@ -0,0 +1,3 @@
async function f() {
await using \u0061b = c;
}
@@ -0,0 +1,3 @@
async function f() {
await using 𠮷 = foo();
}
@@ -0,0 +1,4 @@
async function f() {
await using using = of;
for (await using using of of);
}
@@ -0,0 +1,14 @@
async function f() {
{
/*0*/await/*1*/using/*2*/b/*3*/=/*4*/f()/*5*/;
}
{
/*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/=/*6*/x/*7*/;/*8*/;/*9*/)/*10*/;
}
{
/*0*/for/*1*/(/*2*/await/*3*/using/*4*/b/*5*/of/*6*/x/*7*/)/*8*/;
}
{
/*0*/for/*1*/await/*2*/(/*3*/await/*4*/using/*5*/b/*6*/of/*7*/x/*8*/)/*9*/;
}
}
@@ -0,0 +1,4 @@
async function f() {
for (await using of of of);
for await (await using of of of);
}
@@ -0,0 +1,3 @@
async function f() {
for (await using basic = reader();;);
}
@@ -0,0 +1,3 @@
const m = module {
await using foo = bar();
}