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

Implement transform support for using declarations #15633

Merged
merged 22 commits into from May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -91,7 +91,7 @@ export default Object.freeze({
),
using: helper(
"7.0.0-beta.0",
'export default function _using(stack,value,isAwait){if(null!=value){if(isAwait)var dispose=value[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(null==dispose&&(dispose=value[Symbol.dispose||Symbol.for("Symbol.dispose")]),"function"!=typeof dispose)throw new TypeError("Property [Symbol.dispose] is not a function.");stack.push({v:value,d:dispose,a:isAwait})}return value}',
'export default function _using(stack,value,isAwait){if(null==value)return value;if("object"!=typeof value)throw new TypeError("using decarations can only be used with objects, null, or undefined.");if(isAwait)var dispose=value[Symbol.asyncDispose||Symbol.for("Symbol.asyncDispose")];if(null==dispose&&(dispose=value[Symbol.dispose||Symbol.for("Symbol.dispose")]),"function"!=typeof dispose)throw new TypeError("Property [Symbol.dispose] is not a function.");return stack.push({v:value,d:dispose,a:isAwait}),value}',
),
wrapRegExp: helper(
"7.19.0",
Expand Down
30 changes: 17 additions & 13 deletions packages/babel-helpers/src/helpers/using.js
@@ -1,19 +1,23 @@
/* @minVersion 7.0.0-beta.0 */

export default function _using(stack, value, isAwait) {
if (value !== null && value !== void 0) {
// core-js-pure uses Symbol.for for polyfilling well-known symbols
if (isAwait) {
var dispose =
value[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")];
}
if (dispose === null || dispose === void 0) {
dispose = value[Symbol.dispose || Symbol.for("Symbol.dispose")];
}
if (typeof dispose !== "function") {
throw new TypeError(`Property [Symbol.dispose] is not a function.`);
}
stack.push({ v: value, d: dispose, a: isAwait });
if (value === null || value === void 0) return value;
if (typeof value !== "object") {
throw new TypeError(
"using decarations can only be used with objects, null, or undefined."
);
}
// core-js-pure uses Symbol.for for polyfilling well-known symbols
if (isAwait) {
var dispose =
value[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")];
}
if (dispose === null || dispose === void 0) {
dispose = value[Symbol.dispose || Symbol.for("Symbol.dispose")];
}
if (typeof dispose !== "function") {
throw new TypeError(`Property [Symbol.dispose] is not a function.`);
}
stack.push({ v: value, d: dispose, a: isAwait });
return value;
}
@@ -0,0 +1,19 @@
return async function () {
let disposed = false;
let beforeEnd;

const err = {};
let thrown;

try {
await using x = {
async [Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")]() {
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
throw err;
}
};
} catch (e) {
thrown = e;
}

expect(thrown).toBe(err);
}();
@@ -0,0 +1,3 @@
expect(() => {
using x = 2;
}).toThrow(TypeError);
@@ -1,3 +1,4 @@
expect(() => {
using x = null;
using y = undefined;
}).not.toThrow();