JavaScript ES5 to ES6/ES7 transpiler that performs the opposite function of Babel
npx @tessl/cli install tessl/npm-lebab@3.2.00
# Lebab
1
2
Lebab 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.
3
4
## Package Information
5
6
- **Package Name**: lebab
7
- **Package Type**: npm
8
- **Language**: JavaScript/Node.js
9
- **Installation**: `npm install -g lebab` (CLI) or `npm install lebab` (programmatic)
10
11
## Core Imports
12
13
```javascript
14
const { transform } = require("lebab");
15
```
16
17
For ES6 modules:
18
19
```javascript
20
import { transform } from "lebab";
21
```
22
23
Alternative CommonJS usage:
24
25
```javascript
26
const lebab = require("lebab");
27
// Then use: lebab.transform(code, transforms)
28
```
29
30
## Basic Usage
31
32
### Programmatic API
33
34
```javascript
35
const { transform } = require("lebab");
36
37
// Transform ES5 code to modern JavaScript
38
const { code, warnings } = transform(
39
'var f = function(a) { return a; };',
40
['let', 'arrow', 'arrow-return']
41
);
42
43
console.log(code); // 'const f = a => a;'
44
console.log(warnings); // []
45
```
46
47
### Command Line Interface
48
49
```bash
50
# Transform a single file
51
lebab es5.js -o es6.js --transform let,arrow
52
53
# Transform entire directory in-place
54
lebab --replace src/js/ --transform arrow
55
56
# Transform with glob patterns
57
lebab --replace 'src/js/**/*.jsx' --transform arrow
58
```
59
60
## Architecture
61
62
Lebab is built around a modular transformation system:
63
64
- **Transform Engine**: Core transformation pipeline using AST manipulation
65
- **Parser/Printer**: Uses Recast for code parsing and generation with source map support
66
- **Transform Registry**: 19 individual transforms organized into safe and unsafe categories
67
- **CLI Interface**: Command-line tool for batch file processing
68
- **Warning System**: Detailed feedback about transformation limitations and failures
69
70
## Capabilities
71
72
### Transform Function
73
74
Main programmatic API for converting ES5 code to modern JavaScript syntax.
75
76
```javascript { .api }
77
/**
78
* Transforms ES5 code to ES6/ES7 using specified transforms
79
* @param {string} code - The ES5 code to transform
80
* @param {string[]} transformNames - Array of transform names to apply
81
* @returns {TransformResult} Object with 'code' and 'warnings' properties
82
*/
83
function transform(code, transformNames);
84
```
85
86
### Safe Transforms
87
88
High-confidence transforms with minimal risk of breaking existing functionality. These transforms use strict rules and produce nearly equivalent code.
89
90
```javascript { .api }
91
// Available safe transform names
92
type SafeTransforms =
93
| "arrow" // Callbacks to arrow functions
94
| "arrow-return" // Drop return statements in arrow functions
95
| "for-of" // For loops to for-of loops
96
| "for-each" // For loops to Array.forEach()
97
| "arg-rest" // Arguments to function(...args)
98
| "arg-spread" // apply() to spread operator
99
| "obj-method" // Function values in objects to methods
100
| "obj-shorthand" // {foo: foo} to {foo}
101
| "no-strict" // Remove "use strict" directives
102
| "exponent" // Math.pow() to ** operator (ES7)
103
| "multi-var"; // Single var declarations to multiple
104
```
105
106
[Safe Transforms](./safe-transforms.md)
107
108
### Unsafe Transforms
109
110
Transforms that use heuristics and may require manual review. Apply with caution as they can potentially change code behavior.
111
112
```javascript { .api }
113
// Available unsafe transform names
114
type UnsafeTransforms =
115
| "let" // var to let/const
116
| "class" // Function/prototypes to classes
117
| "commonjs" // CommonJS modules to ES6 modules
118
| "template" // String concatenation to template strings
119
| "default-param" // Default parameters instead of a = a || 2
120
| "destruct-param" // Destructuring parameters
121
| "includes"; // indexOf() to includes()
122
```
123
124
[Unsafe Transforms](./unsafe-transforms.md)
125
126
### Transform Result
127
128
The result object returned by the transform function.
129
130
```javascript { .api }
131
interface TransformResult {
132
/** The transformed code */
133
code: string;
134
/** Array of warnings about transformation issues */
135
warnings: Warning[];
136
}
137
138
interface Warning {
139
/** Line number where warning occurred */
140
line: number;
141
/** Warning message */
142
msg: string;
143
/** Transform type that generated the warning */
144
type: string;
145
}
146
```
147
148
### CLI Interface
149
150
Command-line interface for batch processing of JavaScript files.
151
152
```bash { .api }
153
# Basic usage
154
lebab <input-file> -o <output-file> --transform <transforms>
155
156
# Options:
157
# --transform, -t Comma-separated list of transforms to apply
158
# --replace, -r In-place transformation using glob patterns
159
# --help, -h Show help
160
# --version, -v Show version
161
```
162
163
[CLI Usage](./cli-usage.md)
164
165
## Types
166
167
```javascript { .api }
168
// All available transform names
169
type TransformName = SafeTransforms | UnsafeTransforms;
170
171
// Transform names as defined in TypeScript definitions
172
type TransformTypes =
173
| "class" | "template" | "arrow" | "arrow-return" | "let"
174
| "default-param" | "destruct-param" | "arg-spread" | "arg-rest"
175
| "obj-method" | "obj-shorthand" | "no-strict" | "commonjs"
176
| "exponent" | "multi-var" | "for-of" | "for-each" | "includes";
177
```