Transform destructuring private proposal syntax in JavaScript code
npx @tessl/cli install tessl/npm-babel--plugin-proposal-destructuring-private@7.28.0@babel/plugin-proposal-destructuring-private is a Babel plugin that transforms destructuring private proposal syntax. It enables developers to destructure private class fields and methods in various contexts including function parameters, catch clauses, for-in/for-of loops, variable declarations, and assignment expressions.
npm install --save-dev @babel/plugin-proposal-destructuring-privateESM (recommended):
import babelPluginDestructuringPrivate from "@babel/plugin-proposal-destructuring-private";CommonJS:
const babelPluginDestructuringPrivate = require("@babel/plugin-proposal-destructuring-private").default;Add the plugin to your Babel configuration:
// babel.config.js
module.exports = {
plugins: [
"@babel/plugin-proposal-destructuring-private"
]
};The plugin transforms destructuring patterns containing private class members:
// Input - destructuring private fields in assignment
class C {
static #x;
static #y;
static {
({ a = 1, #x: x = 2, #y: y, b = 3 } = C);
}
}
// Output - transformed to individual property access
class C {
static #x;
static #y;
static {
var _m, _m2;
({
a = 1
} = C), _m = C.#x, x = _m === void 0 ? 2 : _m, _m2 = C.#y, y = _m2 === void 0 ? void 0 : _m2, {
b = 3
} = C;
}
}The main export is a Babel plugin function that transforms destructuring private proposal syntax.
// Main export - Babel plugin function
function babelPluginDestructuringPrivate(): BabelPlugin;
export default babelPluginDestructuringPrivate;The plugin automatically configures itself:
"proposal-destructuring-private""destructuringPrivate" to parser plugins^7.17.0The plugin handles private destructuring in multiple contexts:
Transforms function parameters with private destructuring patterns.
// Input
class C {
static #x;
static method(a, { #x: x } = C) {
return x;
}
}
// Output
class C {
static #x;
static method(a, _p = void 0) {
var x = (_p === void 0 ? C : _p).#x;
return x;
}
}Transforms variable declarations with private destructuring.
// Input
class C {
static #x;
static {
var { a = 1, #x: x = 2, b = 3 } = C;
}
}
// Output
class C {
static #x;
static {
var {
a = 1
} = C,
_m = C.#x,
x = _m === void 0 ? 2 : _m,
{
b = 3
} = C;
}
}Transforms assignment expressions with private destructuring patterns.
// Input
class C {
static #x;
static {
({ #x: x } = C);
}
}
// Output
class C {
static #x;
static {
var _m;
_m = C.#x, x = _m;
}
}Transforms catch clause parameters with private destructuring.
// Input
class C {
static #x;
static {
try {
throw C;
} catch ({ #x: x }) {
console.log(x);
}
}
}
// Output
class C {
static #x;
static {
try {
throw C;
} catch (_p) {
var x = _p.#x;
console.log(x);
}
}
}Transforms for-in and for-of loop destructuring with private fields.
// Input
class C {
static #x;
static {
for (const { #x: x } of [C]) {
console.log(x);
}
}
}
// Output
class C {
static #x;
static {
for (const _p of [C]) {
var x = _p.#x;
console.log(x);
}
}
}The plugin respects two Babel assumptions:
The plugin respects two Babel assumptions that can be configured in your Babel setup:
true, preserves function length for parameters with private destructuringtrue, optimizes object rest operations by excluding symbolsThese are configured at the Babel level, not within the plugin itself.
The plugin requires:
^7.17.0 or higherCommon error scenarios:
// The plugin exports a standard Babel plugin function
interface BabelPlugin {
name: "proposal-destructuring-private";
manipulateOptions: (opts: any, parserOpts: any) => void;
visitor: object; // Babel visitor object
}
type PluginFunction = () => BabelPlugin;