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

add docs for plugin-proposal-explicit-resource-management #2792

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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