Lebab chuyển mã ES5 của bạn thành ES6 / ES7. Nó hoàn toàn ngược lại với những gì Babel làm (ok)

https://lebab.unibtc.me/editor/

Ví dụ code es5

var Member =  (function () {
    function Member(id, name, isAdmin) {
        this.id = id;
        this.name = name;
        this.isAdmin = isAdmin;
    }
    Member.prototype.show = function () {
        console.log("Day la thanh vien " + this.name);
    };
    return Member;
}());

Ví dụ es6

const Member = (() => {
  class Member {
    constructor(id, name, isAdmin) {
      this.id = id;
      this.name = name;
      this.isAdmin = isAdmin;
    }
    show() {
      console.log(`Day la thanh vien ${this.name}`);
    }
  }
  return Member;
})();

Đọc thêm:

https://app.gitbook.com/@javascriptuse/s/advanced/turn-your-es5-code-into-readable-es6-okarrow-up-right

file-archive
2MB
archive

Lebab transpiles your ES5 code to ES6/ES7. It does exactly the opposite of what Babelarrow-up-right does. If you want to understand what Lebab exactly does, try the live demoarrow-up-right.

Install

Install it using npm:

Full build:

Usage

Convert your old-fashioned code using the lebab cli tool, enabling a specific transformation:

Or transform an entire directory of files in-place:

For all the possible values for --transform option see the detailed docs below or use --help from command line.

Features and known limitations

The recommended way of using Lebab is to apply one transform at a time, read what exactly the transform does and what are its limitations, apply it for your code and inspect the diff carefully.

Safe transforms

These transforms can be applied with relatively high confidence. They use pretty straight-forward and strict rules for changing the code. The resulting code should be almost 100% equivalent of the original code.

  • arrow - callbacks to arrow functions

    • Converts bound functions like function(){}.bind(this)

    • not applied to unbound functions that use this

    • not applied to functions that use arguments

    • not applied to object properties (use obj-method transform)

    • does not remove that = this assignments

  • arrow-return - drop return statements in arrow functions

    • converts immediate return { return x; } to => x

    • applies to arrow functions and nested arrow functions

    • LIMITATION only applies to arrow functions (run the arrow transform first)

  • for-of - for loop to for-of loop

  • for-each - for loop to Array.forEach()

  • arg-rest - use of arguments to function(...args)

  • arg-spread - use of apply() to spread operator

    • recognizes obj.method.apply(obj, args)

    • recognizes func.apply(undefined, args)

  • obj-method - function values in object to methods

  • obj-shorthand - {foo: foo} to {foo}

    • ignores numeric and NaN properties

    • does not convert string properties

  • no-strict - removal of "use strict" directives

    • does not touch stuff like x = "use strict";

  • exponent - Math.pow() to ** operator (ES7)

    • Full support for all new syntax from ES7

  • multi-var - single var x,y; declaration to multiple var x; var y; (refactor)

Unsafe transforms

These transforms should be applied with caution. They either use heuristics which can't guarantee that the resulting code is equivalent of the original code, or they have significant bugs which can result in breaking your code.

Programming API

Simply import and call the transform() function:

The warnings will be an array of objects like:

Most of the time there won't be any warnings and the array will be empty.

Editor plugins

Alternatively one can use Lebab through plugins in the following editors:

What's next?

Which feature should Lebab implement next? Let us know by creating an issuearrow-up-right or voicing your opinion in existing one.

Want to contribute? Read how Lebab looks for patterns in syntax trees.arrow-up-right

Last updated