Skip to content

Commit d81596c

Browse files
authoredJan 30, 2024
fix(es/proposals): Support using using keyword with functions (#8574)
**Related issue:** - Closes #8570 - babel/babel#16150
1 parent f5ee6d1 commit d81596c

File tree

8 files changed

+34
-11
lines changed

8 files changed

+34
-11
lines changed
 

‎crates/swc_ecma_transforms_base/src/helpers/_using.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
function _using(stack, value, isAwait) {
22
if (value === null || value === void 0) return value;
3-
if (typeof value !== "object") {
4-
throw new TypeError(
5-
"using declarations can only be used with objects, null, or undefined."
6-
);
3+
if (Object(value) !== value) {
4+
throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
75
}
86
// core-js-pure uses Symbol.for for polyfilling well-known symbols
97
if (isAwait) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
return async function () {
2+
let log = [];
3+
async function getDisposable() {
4+
function disposable() { log.push('call') }
5+
disposable[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")] = () => { log.push('dispose') };
6+
return disposable;
7+
}
8+
9+
{
10+
await using x = getDisposable();
11+
x();
12+
}
13+
14+
expect(log).toEqual(['call', 'dispose']);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let log = [];
2+
function disposable() { log.push('call') }
3+
disposable[Symbol.dispose || Symbol.for("Symbol.dispose")] = () => { log.push('dispose') };
4+
5+
{
6+
using x = disposable;
7+
x();
8+
}
9+
10+
expect(log).toEqual(['call', 'dispose']);

‎packages/counter/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swc/counter",
3-
"packageManager": "yarn@3.5.0",
3+
"packageManager": "yarn@4.0.2",
44
"main": "index.js",
55
"version": "0.1.2",
66
"description": "Downloade counter for the swc project",

‎packages/helpers/esm/_using.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

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

‎packages/helpers/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@swc/helpers",
3-
"packageManager": "yarn@3.5.0",
4-
"version": "0.5.3",
3+
"packageManager": "yarn@4.0.2",
4+
"version": "0.5.4",
55
"description": "External helpers for the swc project.",
66
"module": "esm/index.js",
77
"main": "cjs/index.cjs",

‎packages/swc-info/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swc-info",
3-
"packageManager": "yarn@3.5.0",
3+
"packageManager": "yarn@4.0.2",
44
"version": "0.1.15",
55
"description": "CLI tool to help issue reporting for swc",
66
"sideEffects": false,

‎packages/types/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swc/types",
3-
"packageManager": "yarn@3.5.0",
3+
"packageManager": "yarn@4.0.2",
44
"version": "0.1.5",
55
"description": "Typings for the swc project.",
66
"sideEffects": false,

0 commit comments

Comments
 (0)
Please sign in to comment.