fix(es/proposals): Support using using keyword with functions (#8574)

**Related issue:**

 - Closes #8570
 - https://github.com/babel/babel/pull/16150
This commit is contained in:
Donny/강동윤 2024-01-30 15:55:43 +09:00 committed by GitHub
parent f5ee6d1d7f
commit d81596cd2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 34 additions and 11 deletions

View File

@ -1,9 +1,7 @@
function _using(stack, value, isAwait) {
if (value === null || value === void 0) return value;
if (typeof value !== "object") {
throw new TypeError(
"using declarations can only be used with objects, null, or undefined."
);
if (Object(value) !== value) {
throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
}
// core-js-pure uses Symbol.for for polyfilling well-known symbols
if (isAwait) {

View File

@ -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']);
}

View File

@ -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']);

View File

@ -1,6 +1,6 @@
{
"name": "@swc/counter",
"packageManager": "yarn@3.5.0",
"packageManager": "yarn@4.0.2",
"main": "index.js",
"version": "0.1.2",
"description": "Downloade counter for the swc project",

View File

@ -2,8 +2,8 @@
export function _using(stack, value, isAwait) {
if (value === null || value === void 0) return value;
if (typeof value !== "object") {
throw new TypeError("using declarations can only be used with objects, null, or undefined.");
if (Object(value) !== value) {
throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
}
// core-js-pure uses Symbol.for for polyfilling well-known symbols
if (isAwait) {

View File

@ -1,7 +1,7 @@
{
"name": "@swc/helpers",
"packageManager": "yarn@3.5.0",
"version": "0.5.3",
"packageManager": "yarn@4.0.2",
"version": "0.5.4",
"description": "External helpers for the swc project.",
"module": "esm/index.js",
"main": "cjs/index.cjs",

View File

@ -1,6 +1,6 @@
{
"name": "swc-info",
"packageManager": "yarn@3.5.0",
"packageManager": "yarn@4.0.2",
"version": "0.1.15",
"description": "CLI tool to help issue reporting for swc",
"sideEffects": false,

View File

@ -1,6 +1,6 @@
{
"name": "@swc/types",
"packageManager": "yarn@3.5.0",
"packageManager": "yarn@4.0.2",
"version": "0.1.5",
"description": "Typings for the swc project.",
"sideEffects": false,