跳到主要内容

@babel/plugin-proposal-destructuring-private

Transforms private destructuring var { #y: y } = this to var y = this.#y.

Example

JavaScript
class Foo {
x;
#y;
equalsTo({ x, #y: y }) {
return this.x === x && this.#y === y;
}
}

will be transformed to

JavaScript
class Foo {
x;
#y;
equalsTo(_p) {
var { x } = _p, y = _p.#y;
return this.x === x && this.#y === y;
}
}

The plugin respects these compiler assumptions:

Installation

npm install --save-dev @babel/plugin-proposal-destructuring-private

Usage

babel.config.json
{
"plugins": ["@babel/plugin-proposal-destructuring-private"]
}

Because the output code includes private fields, if you are already using other class feature plugins (e.g. `@babel/plugin-transform-class-properties), be sure to place it before the others.

babel.config.json
{
"plugins": [
"@babel/plugin-proposal-destructuring-private",
"@babel/plugin-transform-class-properties"
]
}

Via CLI

Shell
babel --plugins @babel/plugin-proposal-destructuring-private script.js

Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-destructuring-private"],
});

References