0
# Safe Transforms
1
2
Safe transforms can be applied with relatively high confidence. They use strict rules for changing code and the resulting code should be nearly 100% equivalent to the original.
3
4
## Capabilities
5
6
### Arrow Functions
7
8
Converts callbacks to arrow functions while preserving behavior.
9
10
```javascript { .api }
11
// Transform name: "arrow"
12
```
13
14
**What it transforms:**
15
16
- Function expressions to arrow functions
17
- Bound functions like `function(){}.bind(this)` to arrow functions
18
- Preserves `this` context correctly
19
20
**Limitations:**
21
22
- Not applied to unbound functions that use `this`
23
- Not applied to functions that use `arguments`
24
- Not applied to object properties (use `obj-method` transform)
25
- Can mess up prototype-based classes (run `class` transform first)
26
27
**Usage Example:**
28
29
```javascript
30
// Before
31
var callback = function(x) {
32
return x * 2;
33
};
34
35
var bound = function() {
36
return this.value;
37
}.bind(this);
38
39
// After
40
const callback = x => {
41
return x * 2;
42
};
43
44
const bound = () => {
45
return this.value;
46
};
47
```
48
49
### Arrow Return
50
51
Drops return statements in arrow functions for more concise syntax.
52
53
```javascript { .api }
54
// Transform name: "arrow-return"
55
```
56
57
**What it transforms:**
58
59
- Immediate return statements `{ return x; }` to `=> x`
60
- Applies to arrow functions and nested arrow functions
61
62
**Limitations:**
63
64
- Only applies to arrow functions (run `arrow` transform first)
65
66
**Usage Example:**
67
68
```javascript
69
// Before
70
const double = x => { return x * 2; };
71
const add = (a, b) => { return a + b; };
72
73
// After
74
const double = x => x * 2;
75
const add = (a, b) => a + b;
76
```
77
78
### For-Of Loops
79
80
Converts traditional for loops to for-of loops.
81
82
```javascript { .api }
83
// Transform name: "for-of"
84
```
85
86
**What it transforms:**
87
88
- For loops iterating over arrays to for-of syntax
89
- Uses variable name from `var item = array[i];` pattern when present
90
91
**Limitations:**
92
93
- Requires let/const variables (run `let` transform first)
94
- Does not work when no alias is defined at start of loop body
95
96
**Usage Example:**
97
98
```javascript
99
// Before
100
for (var i = 0; i < items.length; i++) {
101
var item = items[i];
102
console.log(item);
103
}
104
105
// After
106
for (const item of items) {
107
console.log(item);
108
}
109
```
110
111
### For-Each Loops
112
113
Converts for loops to `Array.forEach()` calls.
114
115
```javascript { .api }
116
// Transform name: "for-each"
117
```
118
119
**What it transforms:**
120
121
- For loops iterating over arrays to `.forEach()` method calls
122
- Uses variable name from `var item = array[i];` pattern when present
123
- Adds index parameter when loop body uses the index variable
124
125
**Limitations:**
126
127
- Requires let/const variables (run `let` transform first)
128
- Does not work when no alias is defined at start of loop body
129
130
**Usage Example:**
131
132
```javascript
133
// Before
134
for (var i = 0; i < items.length; i++) {
135
var item = items[i];
136
process(item);
137
}
138
139
// After
140
items.forEach((item) => {
141
process(item);
142
});
143
```
144
145
### Rest Parameters
146
147
Converts `arguments` usage to rest parameters.
148
149
```javascript { .api }
150
// Transform name: "arg-rest"
151
```
152
153
**What it transforms:**
154
155
- Functions using `arguments` to rest parameter syntax `function(...args)`
156
157
**Limitations:**
158
159
- Does not transform when `args` variable already exists
160
- Always names the rest parameter as `args`
161
- Does not transform functions with formal parameters
162
- Does not remove uses of `Array.slice.call(arguments)`
163
164
**Usage Example:**
165
166
```javascript
167
// Before
168
function sum() {
169
var total = 0;
170
for (var i = 0; i < arguments.length; i++) {
171
total += arguments[i];
172
}
173
return total;
174
}
175
176
// After
177
function sum(...args) {
178
var total = 0;
179
for (var i = 0; i < args.length; i++) {
180
total += args[i];
181
}
182
return total;
183
}
184
```
185
186
### Spread Operator
187
188
Converts `apply()` usage to spread operator.
189
190
```javascript { .api }
191
// Transform name: "arg-spread"
192
```
193
194
**What it transforms:**
195
196
- `obj.method.apply(obj, args)` to `obj.method(...args)`
197
- `func.apply(undefined, args)` to `func(...args)`
198
199
**Usage Example:**
200
201
```javascript
202
// Before
203
Math.max.apply(Math, numbers);
204
callback.apply(undefined, args);
205
obj.method.apply(obj, params);
206
207
// After
208
Math.max(...numbers);
209
callback(...args);
210
obj.method(...params);
211
```
212
213
### Object Methods
214
215
Converts function values in objects to method syntax.
216
217
```javascript { .api }
218
// Transform name: "obj-method"
219
```
220
221
**What it transforms:**
222
223
- Object properties with function values to method shorthand
224
225
**Limitations:**
226
227
- Does not convert named function expressions
228
- Does not convert arrow functions
229
230
**Usage Example:**
231
232
```javascript
233
// Before
234
var obj = {
235
method: function(x) {
236
return x * 2;
237
},
238
other: function() {
239
console.log('hello');
240
}
241
};
242
243
// After
244
var obj = {
245
method(x) {
246
return x * 2;
247
},
248
other() {
249
console.log('hello');
250
}
251
};
252
```
253
254
### Object Shorthand
255
256
Converts `{foo: foo}` patterns to `{foo}` shorthand.
257
258
```javascript { .api }
259
// Transform name: "obj-shorthand"
260
```
261
262
**What it transforms:**
263
264
- Object properties where key and value have the same name
265
266
**Limitations:**
267
268
- Ignores numeric and `NaN` properties
269
- Does not convert string properties
270
271
**Usage Example:**
272
273
```javascript
274
// Before
275
var name = 'John';
276
var age = 30;
277
var obj = {
278
name: name,
279
age: age,
280
active: active
281
};
282
283
// After
284
var name = 'John';
285
var age = 30;
286
var obj = {
287
name,
288
age,
289
active
290
};
291
```
292
293
### Remove Strict Mode
294
295
Removes unnecessary "use strict" directives.
296
297
```javascript { .api }
298
// Transform name: "no-strict"
299
```
300
301
**What it transforms:**
302
303
- Removes `"use strict";` directives
304
- Does not touch assignments like `x = "use strict";`
305
306
**Usage Example:**
307
308
```javascript
309
// Before
310
"use strict";
311
312
function myFunction() {
313
"use strict";
314
return 42;
315
}
316
317
// After
318
319
function myFunction() {
320
return 42;
321
}
322
```
323
324
### Exponentiation Operator
325
326
Converts `Math.pow()` to exponentiation operator (ES7).
327
328
```javascript { .api }
329
// Transform name: "exponent"
330
```
331
332
**What it transforms:**
333
334
- `Math.pow(base, exponent)` to `base ** exponent`
335
336
**Usage Example:**
337
338
```javascript
339
// Before
340
var result = Math.pow(2, 8);
341
var complex = Math.pow(x + 1, y * 2);
342
343
// After
344
var result = 2 ** 8;
345
var complex = (x + 1) ** (y * 2);
346
```
347
348
### Multiple Variable Declarations
349
350
Splits single variable declarations to multiple declarations.
351
352
```javascript { .api }
353
// Transform name: "multi-var"
354
```
355
356
**What it transforms:**
357
358
- Single `var x, y;` declarations to multiple `var x; var y;` declarations
359
360
**Usage Example:**
361
362
```javascript
363
// Before
364
var a = 1, b = 2, c;
365
366
// After
367
var a = 1;
368
var b = 2;
369
var c;
370
```