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

using: Allow looking up Symbol.dispose on a function #16150

Merged
merged 1 commit into from Dec 11, 2023
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
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers-generated.ts
Expand Up @@ -111,7 +111,7 @@ export default Object.freeze({
),
using: helper(
"7.22.0",
'export default function _using(o,e,n){if(null==e)return e;if("object"!=typeof e)throw new TypeError("using declarations can only be used with objects, null, or undefined.");if(n)var r=e[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(null==r&&(r=e[Symbol.dispose||Symbol.for("Symbol.dispose")]),"function"!=typeof r)throw new TypeError("Property [Symbol.dispose] is not a function.");return o.push({v:e,d:r,a:n}),e}',
'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}',
),
wrapRegExp: helper(
"7.19.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-helpers/src/helpers/using.js
Expand Up @@ -2,9 +2,9 @@

export default function _using(stack, value, isAwait) {
if (value === null || value === void 0) return value;
if (typeof value !== "object") {
if (Object(value) !== value) {
throw new TypeError(
"using declarations can only be used with objects, null, or undefined."
"using declarations can only be used with objects, functions, null, or undefined."
);
}
// core-js-pure uses Symbol.for for polyfilling well-known symbols
Expand Down
@@ -0,0 +1,15 @@
return async function () {
let log = [];
async function getDisposable() {
function disposable() { log.push('call') }
disposable[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")] = () => { log.push('dispose') };
return disposable;
}

{
await using x = getDisposable();
x();
}

expect(log).toEqual(['call', 'dispose']);
}
@@ -0,0 +1,10 @@
let log = [];
function disposable() { log.push('call') }
disposable[Symbol.dispose || Symbol.for("Symbol.dispose")] = () => { log.push('dispose') };

{
using x = disposable;
x();
}

expect(log).toEqual(['call', 'dispose']);