New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-private

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-private

This is a plugin provide private fields in ES6+ class. It's not like [ECMAScript Private Fields](https://github.com/tc39/proposal-private-fields) proposal but auto transform any field start with '_' into private in a class.

  • 0.1.1
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Babel Private Field Plugin

This is a plugin provide private fields in ES6+ class. It's not like ECMAScript Private Fields proposal but auto transform any field start with '_' into private in a class.

Usage

npm i babel-plugin-transform-private --save-dev

Config .babelrc or package.json

{
  "plugins": [
    ["../src/transform-private.js", {
      "pattern": "^_"
    }],
  ]
}

Compile results:

Simple class

input:

export default class Point2D{
  constructor(x, y){
    this._x = x;
    this._y = y;
  }
  get XY(){
    return [this._x, this._y];
  }
  get length(){
    return Math.sqrt(this._x * this._x + this._y * this._y);
  }
}

output:

const Point2D = function () {
  const [$_x$, $_y$] = [Symbol("$_x$"), Symbol("$_y$")];
  class Point2D {
    constructor(x, y) {
      this[$_x$] = x;
      this[$_y$] = y;
    }
    get XY() {
      return [this[$_x$], this[$_y$]];
    }
    get length() {
      return Math.sqrt(this[$_x$] * this[$_x$] + this[$_y$] * this[$_y$]);
    }
  }
  return Point2D;
}();

export default Point2D;

Nested class

input:

export default class Outer {
  constructor(){
    this._inner = class Inner{
      constructor(x){
        this._x = x;
      }
      get X(){
        return this._x;
      }
    }
  }
  get innerCls(){
    return this._inner;
  }
}

output:

const Outer = function () {
  const [$_inner$] = [Symbol("$_inner$")];
  class Outer {
    constructor() {
      this[$_inner$] = function () {
        const [$_x$] = [Symbol("$_x$")];
        return class Inner {
          constructor(x) {
            this[$_x$] = x;
          }
          get X() {
            return this[$_x$];
          }
        };
      }();
    }
    get innerCls() {
      return this[$_inner$];
    }
  }
  return Outer;
}();

export default Outer;

Protected fields & super

input:

export const Foo = class {
  constructor(x, y){
    this._x = x;
    this._y = y;
    this._zz = x + y;  
  }
  //protected filed
  get _z(){
    return this._zz;
  }
}

export const Bar = class extends Foo{
  constructor(x, y){
    super(x * 2, y * 3);
  }
  get z(){
    return super._z; //get x*2+y*3
  }
  get z2(){
    return super._zz; //undefined
  }
}

output:

export const Foo = function () {
  const [$_x$, $_y$, $_zz$, $_z$] = [Symbol("$_x$"), Symbol("$_y$"), Symbol("$_zz$"), Symbol("$_z$")];
  return class {
    constructor(x, y) {
      this[$_x$] = x;
      this[$_y$] = y;
      this[$_zz$] = x + y;
    }
    get [$_z$]() {
      return this[$_zz$];
    }
  };
}();

export const Bar = class extends Foo {
  constructor(x, y) {
    super(x * 2, y * 3);
  }
  get z() {
    return super[Object.getOwnPropertySymbols(this.__proto__.__proto__).filter(s => String(s) === "Symbol($_z$)")[0]];
  }
  get z2() {
    return super[Object.getOwnPropertySymbols(this.__proto__.__proto__).filter(s => String(s) === "Symbol($_zz$)")[0]];
  }
};

FAQs

Package last updated on 18 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc