Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Await nullish async disposable #16408

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ export default Object.freeze({
"7.22.0",
'export default function _using(o,n,e){if(null==n)return n;if(Object(n)!==n)throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");if(e)var r=n[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(null==r&&(r=n[Symbol.dispose||Symbol.for("Symbol.dispose")]),"function"!=typeof r)throw new TypeError("Property [Symbol.dispose] is not a function.");return o.push({v:n,d:r,a:e}),n}',
),
// size: 891, gzip size: 466
// size: 922, gzip size: 481
usingCtx: helper(
"7.23.9",
'export default function _usingCtx(){var r="function"==typeof SuppressedError?SuppressedError:function(r,n){var e=Error();return e.name="SuppressedError",e.suppressed=n,e.error=r,e},n={},e=[];function using(r,n){if(null!=n){if(Object(n)!==n)throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");if(r)var o=n[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(null==o&&(o=n[Symbol.dispose||Symbol.for("Symbol.dispose")]),"function"!=typeof o)throw new TypeError("Property [Symbol.dispose] is not a function.");e.push({v:n,d:o,a:r})}return n}return{e:n,u:using.bind(null,!1),a:using.bind(null,!0),d:function(){var o=this.e;function next(){for(;r=e.pop();)try{var r,t=r.d.call(r.v);if(r.a)return Promise.resolve(t).then(next,err)}catch(r){return err(r)}if(o!==n)throw o}function err(e){return o=o!==n?new r(o,e):e,next()}return next()}}}',
'export default function _usingCtx(){var r="function"==typeof SuppressedError?SuppressedError:function(r,n){var e=Error();return e.name="SuppressedError",e.suppressed=n,e.error=r,e},n={},e=[];function using(r,n){if(null!=n){if(Object(n)!==n)throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");if(r)var o=n[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(null==o&&(o=n[Symbol.dispose||Symbol.for("Symbol.dispose")]),"function"!=typeof o)throw new TypeError("Property [Symbol.dispose] is not a function.");e.push({v:n,d:o,a:r})}else r&&e.push({d:n,a:r});return n}return{e:n,u:using.bind(null,!1),a:using.bind(null,!0),d:function(){var o=this.e;function next(){for(;r=e.pop();)try{var r,t=r.d&&r.d.call(r.v);if(r.a)return Promise.resolve(t).then(next,err)}catch(r){return err(r)}if(o!==n)throw o}function err(e){return o=o!==n?new r(o,e):e,next()}return next()}}}',
),
// size: 1252, gzip size: 572
wrapRegExp: helper(
Expand Down
9 changes: 6 additions & 3 deletions packages/babel-helpers/src/helpers/usingCtx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* @minVersion 7.23.9 */

type Stack = {
v: any;
d: () => any;
v?: any;
d: null | undefined | (() => any);
a: boolean;
};

Expand Down Expand Up @@ -39,6 +39,9 @@ export default function _usingCtx() {
throw new TypeError(`Property [Symbol.dispose] is not a function.`);
}
stack.push({ v: value, d: dispose, a: isAwait });
} else if (isAwait) {
// provide the nullish `value` as `d` for minification gain
stack.push({ d: value, a: isAwait });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: currently await using x = document.all will go to this branch, we probably should reject it. I will check the spec and open a new PR to fix that.

}
return value;
}
Expand All @@ -58,7 +61,7 @@ export default function _usingCtx() {
while ((resource = stack.pop())) {
try {
var resource,
disposalResult = resource.d.call(resource.v);
disposalResult = resource.d && resource.d.call(resource.v);
if (resource.a) {
return Promise.resolve(disposalResult).then(next, err);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
return (async function () {
const log = [];

function disposable(name) {
return {
async [Symbol.dispose || Symbol.for("Symbol.dispose")]() {
log.push(name);
},
};
}

async function f() {
using y = disposable("y");
await using x = null;
log.push("f body");
}

const promise = f();

log.push("body");

await promise;

expect(log).toEqual(["f body", "body", "y"]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
return (async function () {
const log = [];

function disposable(name) {
return {
async [Symbol.dispose || Symbol.for("Symbol.dispose")]() {
log.push(name);
},
};
}

async function f() {
using y = disposable("y");
await using x = undefined;
log.push("f body");
}

const promise = f();

log.push("body");

await promise;

expect(log).toEqual(["f body", "body", "y"]);
})();
7 changes: 5 additions & 2 deletions packages/babel-runtime-corejs3/helpers/esm/usingCtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export default function _usingCtx() {
d: o,
a: r
});
}
} else r && _pushInstanceProperty(e).call(e, {
d: n,
a: r
});
return n;
}
return {
Expand All @@ -34,7 +37,7 @@ export default function _usingCtx() {
function next() {
for (; r = e.pop();) try {
var r,
t = r.d.call(r.v);
t = r.d && r.d.call(r.v);
if (r.a) return _Promise.resolve(t).then(next, err);
} catch (r) {
return err(r);
Expand Down