lebab

Turn your ES5 code into readable ES6 (sugar-syntax). It does the opposite of what Babel does.

Github星跟蹤圖

Build Status
Coverage Status
Dependencies
License
Version

Lebab

Lebab

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

Install it using npm:

$ npm install -g lebab

Full build:

Usage

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.

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.

  • let - var to let/const
  • class - function/prototypes to classes
  • commonjs - CommonJS module definition to ES6 modules
    • converts var foo = require("foo") to import foo from "foo"
    • converts var bar = require("foo").bar to import {bar} from "foo"
    • converts var {bar} = require("foo") to import {bar} from "foo"
    • converts module.exports = <anything> to export default <anything>
    • converts exports.foo = function(){} to export function foo(){}
    • converts exports.Foo = class {} to export class Foo {}
    • converts exports.foo = 123 to export var foo = 123
    • converts exports.foo = bar to export {bar as foo}
    • LIMITATION does not check if named export conflicts with existing variable names
    • LIMITATION Ignores imports/exports inside nested blocks/functions
    • LIMITATION only handles require() calls in var declarations
    • LIMITATION does not ensure that imported variable is treated as const
    • LIMITATION does not ensure named exports are imported with correct ES6 syntax
  • template - string concatenation to template strings
  • default-param - default parameters instead of a = a, 2
  • destruct-param - use destructuring for objects in function parameters
  • includes - array.indexOf(foo) !== -1 to array.includes(foo) (ES7)
    • works for both strings and arrays
    • converts !== -1 to array.includes(foo)
    • converts === -1 to !array.includes(foo)
    • recognizes all kinds of comparisons >= 0, > -1, etc
    • recognizes both indexOf() != -1 and -1 != indexOf()
    • LIMITATION does not detect that indexOf() is called on an actual Array or String.

Programming API

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.

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 issue
or voicing your opinion in existing one.

Want to contribute? Read how Lebab looks for patterns in syntax trees.

主要指標

概覽
名稱與所有者lebab/lebab
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2015-02-18 05:36:49
推送於2025-01-31 15:07:47
最后一次提交2025-01-31 17:07:47
發布數50
最新版本名稱v3.2.6 (發布於 2024-11-09 19:45:23)
第一版名稱v1.0.0 (發布於 2016-03-17 20:13:30)
用户参与
星數5.6k
關注者數71
派生數149
提交數1k
已啟用問題?
問題數233
打開的問題數36
拉請求數77
打開的拉請求數4
關閉的拉請求數52
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?