Skip to content

Commit

Permalink
using: Allow looking up Symbol.dispose on a function (#16150)
Browse files Browse the repository at this point in the history
  • Loading branch information
odinho committed Dec 11, 2023
1 parent 8fa2a91 commit ed365d1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers-generated.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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']);
}
Original file line number Diff line number Diff line change
@@ -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']);

0 comments on commit ed365d1

Please sign in to comment.