Skip to content

Commit

Permalink
add docs for plugin-proposal-explicit-resource-management (#2792)
Browse files Browse the repository at this point in the history
* add docs for plugin-proposal-explicit-resource-management

* Update docs/plugin-proposal-explicit-resource-management.md
  • Loading branch information
JLHwung committed May 26, 2023
1 parent ab365e2 commit 1f3bc9d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
65 changes: 65 additions & 0 deletions docs/plugin-proposal-explicit-resource-management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
id: babel-plugin-proposal-explicit-resource-management
title: "@babel/plugin-proposal-explicit-resource-management"
sidebar_label: explicit-resource-management
---

This plugin enables Babel to transform using declarations `using handler = await read();`.

## Example

```js title="input.js"
using handlerSync = openSync();
await using handlerAsync = await openAsync();
```

will be transformed to

```js title="output.js"
try {
var _stack = [];
var handlerSync = babelHelpers.using(_stack, openSync());
var handlerAsync = babelHelpers.using(_stack, await openAsync(), true);
} catch (_) {
var _error = _;
var _hasError = true;
} finally {
await babelHelpers.dispose(_stack, _error, _hasError);
}
```

[Try it on the REPL](https://babeljs.io/repl#?build=&builtIns=false&corejs=3.28&spec=false&loose=false&code_lz=K4Zwlgdg5gBAFgQwgEwDYFMBOBlAnhAYxgF4YB7AB3Qj0IAoBKAbgCgEB3BMAFxlEliIUGTAEEQ-IqQ5delauMmMmQA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=react&prettier=false&targets=&externalPlugins=%40babel%2Fplugin-proposal-explicit-resource-management%407.22.0%2C%40babel%2Fplugin-external-helpers%407.18.6&assumptions=%7B%7D).

## Installation

```shell npm2yarn
npm install --save-dev @babel/plugin-proposal-explicit-resource-management
```

## Usage

### With a configuration file (Recommended)

```json title="babel.config.json"
{
"plugins": ["@babel/plugin-proposal-explicit-resource-management"]
}
```

### Via CLI

```sh title="Shell"
babel --plugins @babel/plugin-proposal-explicit-resource-management script.js
```

### Via Node API

```js title="JavaScript"
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-explicit-resource-management"]
});
```

## References

- [Proposal: ECMAScript Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management)
2 changes: 1 addition & 1 deletion docs/plugin-syntax-explicit-resource-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_label: syntax-explicit-resource-management

> #### Syntax only
>
> This plugin only enables Babel to parse this syntax. Babel does not support transforming this syntax
> It's unlikely you want to use this plugin directly as it only enables Babel to parse this syntax. Instead, use [plugin-proposal-explicit-resource-management](plugin-proposal-explicit-resource-management.md) to _both_ parse and transform this syntax.
This plugin enables Babel to parse using declarations:

Expand Down
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ module.exports = {
"babel-plugin-proposal-destructuring-private",
"babel-plugin-proposal-do-expressions",
"babel-plugin-proposal-duplicate-named-capturing-groups-regex",
"babel-plugin-proposal-explicit-resource-management",
"babel-plugin-proposal-export-default-from",
"babel-plugin-proposal-function-bind",
"babel-plugin-proposal-function-sent",
Expand Down

0 comments on commit 1f3bc9d

Please sign in to comment.