1.0.0 • Published 6 years ago

func-to-classes v1.0.0

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

Transform ES5 Functions to ES6 Classes :package:

Live Demo 💻

Features :100:

  • Methods on prototype
  • Static Methods on prototype
  • Variables and Literals on prototype
  • Getters, Setters with defineProperty
  • Block-level Variables
Similar project - lebab. It doesn't currently support:
  • Static Methods on prototype
  • Block-level Variables

Setup and Run :runner:

npm i -g jscodeshift
git clone --depth=1 https://github.com/dhruvdutt/es5-function-to-class-codemod codemod && cd codemod
jscodeshift -t func-to-class.js example/input.js

Sample Input 🛴

function Controller(param1, param2) {
  const name = "dhruvdutt";
  let age = 21;
}

Controller.prototype.name = "Dhruvdutt";

Controller.prototype.contributeTo = function(param) {
  var foo = "webpack";
};

Controller.staticMethod = function(param) {
  var bar = "webpack-cli";
};

Object.defineProperty(Controller.prototype, "hello", {
    get: function () {
        return "world";
    },
    set: function (name) {
        console.log("Do anything with " + name);
    },
});

Object.defineProperty(Controller.prototype, "lastname", {
    get: function () {
        return "Jadhav";
    },
});

Sample Output :rocket:

class Controller {
  constructor(param1, param2) {
    const name = "dhruvdutt";
    let age = 21;
    this.name = "Dhruvdutt";
  }

  contributeTo(param) {
    var foo = "webpack";
  }

  static staticMethod(param) {
    var bar = "webpack-cli";
  }

  get hello() {
    return "world";
  }

  set hello(name) {
    console.log("Do anything with " + name);
  }

  get lastname() {
    return "Jadhav";
  }
}

Complex example: Multiple functions in same file with one of the functions having same name.

Also, featured on awesome-jscodeshift.