1.0.1 • Published 6 years ago

babel-plugin-short-private-properties v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

short-private-properties

This plugin find in ES6 Classes all methods or properties with start _ and replace them with a short name.

npm AppVeyor Travis

Install

yarn add -D babel-plugin-short-private-properties
# or npm i -D babel-plugin-short-private-properties

Input:

class A {
  constructor() {
    this._veryLondProppertyNameA = "Test class A";
    this._veryLondProppertyNameA2 = "Write A";
  }
  _getAProperty() {
    console.log(this._veryLondProppertyNameA);
  }
}

class B extends A {
  constructor() {
    super();
    this._veryLondProppertyNameA2 = "Overwrite B";
    this._veryLondProppertyNameB = "Test class B";
  }
  getResult() {
    this._getAProperty();
    console.log(this._veryLondProppertyNameB);
    console.log(this._veryLondProppertyNameA2);
  }
}

new B().getResult();
// Test class A
// Test class B
// Overwrite B

Output:

class A {
  constructor() {
    this._b = "Test class A";
    this._c = "Write A";
  }
  _d() {
    console.log(this._b);
  }
}

class B extends A {
  constructor() {
    super();
    this._c = "Overwrite B";
    this._e = "Test class B";
  }
  getResult() {
    this._d();
    console.log(this._e);
    console.log(this._c);
  }
}

new B().getResult();
// Test class A
// Test class B
// Overwrite B

Usage

Via .babelrc (Recommended)

{
  "plugins": ["babel-plugin-short-private-properties"]
}

Via CLI

$ babel --plugins babel-plugin-short-private-properties script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["babel-plugin-short-private-properties"]
});