or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--plugin-proposal-destructuring-private

Transform destructuring private proposal syntax in JavaScript code

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/plugin-proposal-destructuring-private@7.28.x

To install, run

npx @tessl/cli install tessl/npm-babel--plugin-proposal-destructuring-private@7.28.0

index.mddocs/

@babel/plugin-proposal-destructuring-private

@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.

Package Information

  • Package Name: @babel/plugin-proposal-destructuring-private
  • Package Type: npm (Babel plugin)
  • Language: TypeScript
  • Installation: npm install --save-dev @babel/plugin-proposal-destructuring-private

Core Imports

ESM (recommended):

import babelPluginDestructuringPrivate from "@babel/plugin-proposal-destructuring-private";

CommonJS:

const babelPluginDestructuringPrivate = require("@babel/plugin-proposal-destructuring-private").default;

Basic Usage

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;
  }
}

Capabilities

Babel Plugin Function

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;

Plugin Configuration

The plugin automatically configures itself:

  • Name: "proposal-destructuring-private"
  • Parser Support: Adds "destructuringPrivate" to parser plugins
  • Version Requirement: Requires Babel ^7.17.0

Transformation Contexts

The plugin handles private destructuring in multiple contexts:

Function Parameters

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;
  }
}

Variable Declarations

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;
  }
}

Assignment Expressions

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;
  }
}

Catch Clauses

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);
    }
  }
}

For-In/For-Of Statements

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);
    }
  }
}

Plugin Assumptions

The plugin respects two Babel assumptions:

The plugin respects two Babel assumptions that can be configured in your Babel setup:

  • ignoreFunctionLength: When true, preserves function length for parameters with private destructuring
  • objectRestNoSymbols: When true, optimizes object rest operations by excluding symbols

These are configured at the Babel level, not within the plugin itself.

Error Handling

The plugin requires:

  • Babel version ^7.17.0 or higher
  • Classes containing private elements for transformation to occur
  • Valid destructuring patterns with private property access

Common error scenarios:

  • Using private destructuring outside of class contexts
  • Incompatible Babel version
  • Malformed private property syntax

Types

// 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;