Lebab
Lebab transpiles your ES5 code to ES2015. It does exactly the opposite of what Babel does. If you want to understand what Lebab exactly does, try the live demo.
Usage
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 --enable let
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 ES2015 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) - converts immediate return
{ return x; }
to=> x
- does not remove
that = this
assignments - BUG fails with immediately returning functions that have methods invoked
- Converts bound functions like
- let -
var
tolet
/const
- never modified variables are converted to
const
- properly recognizes block-scoping
- splits single var declaration to multiple
let
/const
declarations if needed - recognizes vars defined/assigned using destructuring
- vars that conflict with block-scoping are not converted
- repeated declarations of the same var are not converted
- existing
let
/const
are not converted - BUG fails with repeated variable definitions that use destructuring
- never modified variables are converted to
- arg-spread - use of apply() to spread operator
- recognizes
obj.method.apply(obj, args)
- recognizes
func.apply(undefined, args)
- recognizes
- obj-method - function values in object to methods
- LIMITATION does not convert named function expressions
- does not convert arrow-functions
- obj-shorthand -
{foo: foo}
to{foo}
- ignores numeric and
NaN
properties - does not convert string properties
- ignores numeric and
- no-strict - removal of
"use strict"
directives- does not touch stuff like
x = "use strict";
- does not touch stuff like
- commonjs - CommonJS module definition to ES6 modules
- converts
var foo = require("foo")
toimport foo from "foo"
- converts
var bar = require("foo").bar
toimport {bar} from "foo"
- converts
var {bar} = require("foo")
toimport {bar} from "foo"
- only handles
require()
calls invar
declarations - does not ensure that imported variable is treated as
const
- converts
module.exports = <anything>
toexport default <anything>
- converts
exports.foo = function(){}
toexport function foo(){}
- converts
exports.Foo = class {}
toexport class Foo {}
- converts
exports.foo = 123
toexport var foo = 123
- converts
exports.foo = bar
toexport {bar as foo}
- does not check if named export conflicts with existing variable names
- does not recognize imports/exports inside nested blocks/functions
- converts
Unsafe transforms
These transforms should be applied with caution. They use heuristics to detect common patterns that can be expressed with ES2015 syntax. There are no guarantees that the resulting code is equivalent of the original code.
- class - function/prototypes to classes
- recognizes
Foo.prototype.method = function(){ ... };
- recognizes
Foo.prototype = { ...methods... };
- recognizes static methods like
Foo.method = function(){ ... };
- recognizes getters/setters defined with
Object.defineProperty()
- does not recognize classes without methods
- no support for extending classes
- LIMITATION does not support namespaced classes
- recognizes
- template - string concatenation to template strings
- converts variables and arbitrary expressions to
${...}
- BUG removes indentation of multi-line strings
- LIMITATION ignores difference between
.toString()
and.valueOf()
- converts variables and arbitrary expressions to
- default-param - default parameters instead of
a = a || 2
- recognizes
a = a || 2
- recognizes
a = a ? a : 2
- recognizes
a = a === undefined ? 2 : a
- recognizes
a = typeof a === 'undefined' ? 2 : a
- LIMITATION transforming
a = a || 2
does produce strictly equivalent code
- recognizes
What's next?
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.