Turn your ES5 code into readable ES6. Lebab does the opposite of what Babel does.
Lebab transpiles your ES5 code to ES6/ES7.
It does exactly the opposite of what Babel does.
If you want to understand what Lebab exactly does, try the live demo.
Install it using npm:
$ npm install -g lebab
Convert your old-fashioned code using the lebab
cli tool,
enabling a specific transformation:
$ lebab es5.js -o es6.js --transform let
Or transform an entire directory of files in-place:
# .js files only
$ lebab --replace src/js/ --transform arrow
# For other file extensions, use explicit globbing
$ lebab --replace 'src/js/**/*.jsx' --transform arrow
For all the possible values for --transform
option
see the detailed docs below or use --help
from command line.
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.
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.
function(){}.bind(this)
this
arguments
obj-method
transform)that = this
assignmentsclass
transform first to prevent this.{ return x; }
to => x
arrow
transform first)item
for loop variable when loop body begins with var item = array[i];
let
transform first)Array.forEach()
item
for forEach parameter when loop body begins with var item = array[i];
let
transform first)args
variable already existsargs
Array.slice.call(arguments)
obj.method.apply(obj, args)
func.apply(undefined, args)
{foo: foo}
to {foo}
NaN
properties"use strict"
directives
x = "use strict";
Math.pow()
to **
operator (ES7)
var x,y;
declaration to multiple var x; var y;
(refactor)
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.
var
to let
/const
const
let
/const
declarations if neededlet
/const
are not convertedFoo.prototype.method = function(){ ... };
Foo.prototype = { ...methods... };
Foo.method = function(){ ... };
Object.defineProperty()
Child.prototype = new Parent()
util.inherits(Child, Parent);
super()
super.method()
var foo = require("foo")
to import foo from "foo"
var bar = require("foo").bar
to import {bar} from "foo"
var {bar} = require("foo")
to import {bar} from "foo"
module.exports = <anything>
to export default <anything>
exports.foo = function(){}
to export function foo(){}
exports.Foo = class {}
to export class Foo {}
exports.foo = 123
to export var foo = 123
exports.foo = bar
to export {bar as foo}
require()
calls in var
declarationsconst
${...}
.toString()
and .valueOf()
a = a || 2
a = a || 2
a = a ? a : 2
a = a === undefined ? 2 : a
a = typeof a === 'undefined' ? 2 : a
a = a || 2
does not produce strictly equivalent code(obj) => obj.a + obj.b
to ({a, b}) => a + b
array.indexOf(foo) !== -1
to array.includes(foo)
(ES7)
!== -1
to array.includes(foo)
=== -1
to !array.includes(foo)
>= 0
, > -1
, etcindexOf() != -1
and -1 != indexOf()
Simply import and call the transform()
function:
import {transform} from 'lebab';
const {code, warnings} = transform(
'var f = function(a) { return a; };', // code to transform
['let', 'arrow', 'arrow-return'] // transforms to apply
);
console.log(code); // -> "const f = a => a;"
The warnings will be an array of objects like:
[
{line: 12, msg: 'Unable to transform var', type: 'let'},
{line: 45, msg: 'Can not use arguments in arrow function', type: 'arrow'},
]
Most of the time there won’t be any warnings and the array will be empty.
Alternatively one can use Lebab through plugins in the following editors:
Which feature should Lebab implement next?
Let us know by creating an issue
or voicing your opinion in existing one.
Want to contribute? Read how Lebab looks for patterns in syntax trees.