JavaScript ES5 to ES6/ES7 transpiler that performs the opposite function of Babel
npx @tessl/cli install tessl/npm-lebab@3.2.0Lebab is a JavaScript transpiler that converts ES5 code to modern ES6/ES7 syntax. It performs the opposite function of Babel, helping developers modernize legacy JavaScript codebases by automatically upgrading syntax patterns to use contemporary language features.
npm install -g lebab (CLI) or npm install lebab (programmatic)const { transform } = require("lebab");For ES6 modules:
import { transform } from "lebab";Alternative CommonJS usage:
const lebab = require("lebab");
// Then use: lebab.transform(code, transforms)const { transform } = require("lebab");
// Transform ES5 code to modern JavaScript
const { code, warnings } = transform(
'var f = function(a) { return a; };',
['let', 'arrow', 'arrow-return']
);
console.log(code); // 'const f = a => a;'
console.log(warnings); // []# Transform a single file
lebab es5.js -o es6.js --transform let,arrow
# Transform entire directory in-place
lebab --replace src/js/ --transform arrow
# Transform with glob patterns
lebab --replace 'src/js/**/*.jsx' --transform arrowLebab is built around a modular transformation system:
Main programmatic API for converting ES5 code to modern JavaScript syntax.
/**
* Transforms ES5 code to ES6/ES7 using specified transforms
* @param {string} code - The ES5 code to transform
* @param {string[]} transformNames - Array of transform names to apply
* @returns {TransformResult} Object with 'code' and 'warnings' properties
*/
function transform(code, transformNames);High-confidence transforms with minimal risk of breaking existing functionality. These transforms use strict rules and produce nearly equivalent code.
// Available safe transform names
type SafeTransforms =
| "arrow" // Callbacks to arrow functions
| "arrow-return" // Drop return statements in arrow functions
| "for-of" // For loops to for-of loops
| "for-each" // For loops to Array.forEach()
| "arg-rest" // Arguments to function(...args)
| "arg-spread" // apply() to spread operator
| "obj-method" // Function values in objects to methods
| "obj-shorthand" // {foo: foo} to {foo}
| "no-strict" // Remove "use strict" directives
| "exponent" // Math.pow() to ** operator (ES7)
| "multi-var"; // Single var declarations to multipleTransforms that use heuristics and may require manual review. Apply with caution as they can potentially change code behavior.
// Available unsafe transform names
type UnsafeTransforms =
| "let" // var to let/const
| "class" // Function/prototypes to classes
| "commonjs" // CommonJS modules to ES6 modules
| "template" // String concatenation to template strings
| "default-param" // Default parameters instead of a = a || 2
| "destruct-param" // Destructuring parameters
| "includes"; // indexOf() to includes()The result object returned by the transform function.
interface TransformResult {
/** The transformed code */
code: string;
/** Array of warnings about transformation issues */
warnings: Warning[];
}
interface Warning {
/** Line number where warning occurred */
line: number;
/** Warning message */
msg: string;
/** Transform type that generated the warning */
type: string;
}Command-line interface for batch processing of JavaScript files.
# Basic usage
lebab <input-file> -o <output-file> --transform <transforms>
# Options:
# --transform, -t Comma-separated list of transforms to apply
# --replace, -r In-place transformation using glob patterns
# --help, -h Show help
# --version, -v Show version// All available transform names
type TransformName = SafeTransforms | UnsafeTransforms;
// Transform names as defined in TypeScript definitions
type TransformTypes =
| "class" | "template" | "arrow" | "arrow-return" | "let"
| "default-param" | "destruct-param" | "arg-spread" | "arg-rest"
| "obj-method" | "obj-shorthand" | "no-strict" | "commonjs"
| "exponent" | "multi-var" | "for-of" | "for-each" | "includes";