Skip to content

Commit

Permalink
fix(es/parser): Parse await using in for head (#8775)
Browse files Browse the repository at this point in the history
**Related issue:**
 
 - Closes #8774
  • Loading branch information
magic-akari authored Mar 26, 2024
1 parent cd06af3 commit 53fd09c
Show file tree
Hide file tree
Showing 25 changed files with 532 additions and 200 deletions.
20 changes: 20 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8774/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"jsc": {
"parser": {
"explicitResourceManagement": true,
"syntax": "ecmascript",
"jsx": true,
},
"target": "es2022",
"loose": false,
"minify": {
"compress": false,
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}
21 changes: 21 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8774/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const logClean = function () {
return {
[Symbol.dispose]() {
console.log("clean in sync");
},
[Symbol.asyncDispose]() {
console.log("clean in async");
},
};
};

async function foo() {
using a = logClean();
await using b = logClean();
for (using a of [logClean(), logClean()]) {
}
for (await using a of [logClean(), logClean()]) {
}
}

foo();
53 changes: 53 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8774/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { _ as _dispose } from "@swc/helpers/_/_dispose";
import { _ as _using } from "@swc/helpers/_/_using";
const logClean = function() {
return {
[Symbol.dispose] () {
console.log("clean in sync");
},
[Symbol.asyncDispose] () {
console.log("clean in async");
}
};
};
async function foo() {
try {
var _stack = [];
var a = _using(_stack, logClean());
var b = _using(_stack, logClean(), true);
for (const a of [
logClean(),
logClean()
]){
try {
var _stack1 = [];
{}
} catch (_) {
var _error = _;
var _hasError = true;
} finally{
_dispose(_stack1, _error, _hasError);
}
}
for (const a of [
logClean(),
logClean()
]){
try {
var _stack2 = [];
{}
} catch (_) {
var _error1 = _;
var _hasError1 = true;
} finally{
_dispose(_stack2, _error1, _hasError1);
}
}
} catch (_) {
var _error2 = _;
var _hasError2 = true;
} finally{
await _dispose(_stack, _error2, _hasError2);
}
}
foo();
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
//// [awaitUsingDeclarationsInForAwaitOf.ts]
//!
//! x Expected ';', got 'd1'
//! ,-[1:1]
//! 1 | async function main() {
//! 2 | for await (await using d1 of [{ async [Symbol.asyncDispose]() {} }, { [Symbol.dispose]() {} }, null, undefined]) {
//! : ^^
//! 3 | }
//! 4 | }
//! `----
import { _ as _async_iterator } from "@swc/helpers/_/_async_iterator";
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _dispose } from "@swc/helpers/_/_dispose";
function main() {
return _main.apply(this, arguments);
}
function _main() {
_main = _async_to_generator(function*() {
{
var _iteratorAbruptCompletion = false, _didIteratorError = false, _iteratorError;
try {
for(var _iterator = _async_iterator([
{
[Symbol.asyncDispose] () {
return _async_to_generator(function*() {})();
}
},
{
[Symbol.dispose] () {}
},
null,
undefined
]), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false){
let _value = _step.value;
const d1 = _value;
try {
var _stack = [];
{}
} catch (_) {
var _error = _;
var _hasError = true;
} finally{
_dispose(_stack, _error, _hasError);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally{
try {
if (_iteratorAbruptCompletion && _iterator.return != null) {
yield _iterator.return();
}
} finally{
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
});
return _main.apply(this, arguments);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//// [awaitUsingDeclarationsInForAwaitOf.ts]
//!
//! x Expected ';', got 'd1'
//! ,-[1:1]
//! 1 | async function main() {
//! 2 | for await (await using d1 of [{ async [Symbol.asyncDispose]() {} }, { [Symbol.dispose]() {} }, null, undefined]) {
//! : ^^
//! 3 | }
//! 4 | }
//! `----
import "@swc/helpers/_/_async_iterator";
import "@swc/helpers/_/_async_to_generator";
import "@swc/helpers/_/_dispose";
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
//// [awaitUsingDeclarationsInForAwaitOf.ts]
//!
//! x Expected ';', got 'd1'
//! ,-[1:1]
//! 1 | async function main() {
//! 2 | for await (await using d1 of [{ async [Symbol.asyncDispose]() {} }, { [Symbol.dispose]() {} }, null, undefined]) {
//! : ^^
//! 3 | }
//! 4 | }
//! `----
import { _ as _dispose } from "@swc/helpers/_/_dispose";
async function main() {
for await (const d1 of [
{
async [Symbol.asyncDispose] () {}
},
{
[Symbol.dispose] () {}
},
null,
undefined
]){
try {
var _stack = [];
{}
} catch (_) {
var _error = _;
var _hasError = true;
} finally{
_dispose(_stack, _error, _hasError);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
//// [awaitUsingDeclarationsInForAwaitOf.ts]
//!
//! x Expected ';', got 'd1'
//! ,-[1:1]
//! 1 | async function main() {
//! 2 | for await (await using d1 of [{ async [Symbol.asyncDispose]() {} }, { [Symbol.dispose]() {} }, null, undefined]) {
//! : ^^
//! 3 | }
//! 4 | }
//! `----
import "@swc/helpers/_/_dispose";
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
//// [awaitUsingDeclarationsInForAwaitOf.ts]
//!
//! x Expected ';', got 'd1'
//! ,-[1:1]
//! 1 | async function main() {
//! 2 | for await (await using d1 of [{ async [Symbol.asyncDispose]() {} }, { [Symbol.dispose]() {} }, null, undefined]) {
//! : ^^
//! 3 | }
//! 4 | }
//! `----
import { _ as _dispose } from "@swc/helpers/_/_dispose";
async function main() {
for await (const d1 of [
{
async [Symbol.asyncDispose] () {}
},
{
[Symbol.dispose] () {}
},
null,
undefined
]){
try {
var _stack = [];
{}
} catch (_) {
var _error = _;
var _hasError = true;
} finally{
_dispose(_stack, _error, _hasError);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
//// [awaitUsingDeclarationsInForAwaitOf.ts]
//!
//! x Expected ';', got 'd1'
//! ,-[1:1]
//! 1 | async function main() {
//! 2 | for await (await using d1 of [{ async [Symbol.asyncDispose]() {} }, { [Symbol.dispose]() {} }, null, undefined]) {
//! : ^^
//! 3 | }
//! 4 | }
//! `----
import "@swc/helpers/_/_dispose";
Loading

0 comments on commit 53fd09c

Please sign in to comment.