Skip to content

Commit

Permalink
fix(es/proposals): Support using using keyword with functions (#8574)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8570
 - babel/babel#16150
  • Loading branch information
kdy1 committed Jan 30, 2024
1 parent f5ee6d1 commit d81596c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 11 deletions.
6 changes: 2 additions & 4 deletions crates/swc_ecma_transforms_base/src/helpers/_using.js
Original file line number Diff line number Diff line change
@@ -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) {
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']);
2 changes: 1 addition & 1 deletion packages/counter/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/helpers/esm/_using.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/helpers/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/swc-info/package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit d81596c

Please sign in to comment.