JavaScript ES5 to ES6/ES7 transpiler that performs the opposite function of Babel
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Unsafe transforms should be applied with caution. They either use heuristics which can't guarantee equivalent code, or they have significant bugs which can break your code.
Converts var to let/const based on usage patterns.
// Transform name: "let"What it transforms:
constletLimitations and Bugs:
Usage Example:
// Before
var name = 'John'; // Never modified
var age = 25; // Modified later
age = 26;
var items = [1, 2, 3]; // Never modified
// After
const name = 'John'; // Converted to const
let age = 25; // Converted to let
age = 26;
const items = [1, 2, 3]; // Converted to constConverts function constructors and prototypes to ES6 classes.
// Transform name: "class"What it transforms:
Foo.prototype.method = function(){} to class methodsFoo.prototype = { ...methods... } to class bodyFoo.method = function(){}Object.defineProperty()Child.prototype = new Parent()util.inherits(Child, Parent)super()super.method()Limitations:
super() call in subclass constructorsuper() call position in subclass constructorUsage Example:
// Before
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return 'Hello, ' + this.name;
};
Person.prototype.getName = function() {
return this.name;
};
// After
class Person {
constructor(name) {
this.name = name;
}
greet() {
return 'Hello, ' + this.name;
}
getName() {
return this.name;
}
}Converts CommonJS modules to ES6 import/export syntax.
// Transform name: "commonjs"What it transforms:
var foo = require("foo") to import foo from "foo"var bar = require("foo").bar to import {bar} from "foo"var {bar} = require("foo") to import {bar} from "foo"module.exports = <anything> to export default <anything>exports.foo = function(){} to export function foo(){}exports.Foo = class {} to export class Foo {}exports.foo = 123 to export var foo = 123exports.foo = bar to export {bar as foo}Limitations:
require() calls in var declarationsconstUsage Example:
// Before
var fs = require('fs');
var {readFile} = require('fs');
var express = require('express');
exports.readData = function() {
return fs.readFileSync('data.txt');
};
module.exports = {
version: '1.0.0'
};
// After
import fs from 'fs';
import {readFile} from 'fs';
import express from 'express';
export function readData() {
return fs.readFileSync('data.txt');
}
export default {
version: '1.0.0'
};Converts string concatenation to template literals.
// Transform name: "template"What it transforms:
+ to template literals${...} syntaxLimitations and Bugs:
.toString() and .valueOf()Usage Example:
// Before
var message = 'Hello ' + name + ', you have ' + count + ' messages';
var url = 'https://api.example.com/users/' + userId + '/profile';
// After
var message = `Hello ${name}, you have ${count} messages`;
var url = `https://api.example.com/users/${userId}/profile`;Converts default parameter patterns to ES6 default parameters.
// Transform name: "default-param"What it transforms:
a = a || 2 patterns to default parametersa = a ? a : 2 patterns to default parametersa = typeof a !== 'undefined' ? a : 2 patternsUsage Example:
// Before
function greet(name, greeting) {
name = name || 'World';
greeting = greeting || 'Hello';
return greeting + ', ' + name + '!';
}
// After
function greet(name = 'World', greeting = 'Hello') {
return greeting + ', ' + name + '!';
}Converts parameter patterns to destructuring syntax.
// Transform name: "destruct-param"What it transforms:
Usage Example:
// Before
function processUser(user) {
var name = user.name;
var age = user.age;
var email = user.email;
console.log(name, age, email);
}
// After
function processUser({name, age, email}) {
console.log(name, age, email);
}Converts indexOf() usage to includes() method.
// Transform name: "includes"What it transforms:
array.indexOf(item) !== -1 to array.includes(item)array.indexOf(item) >= 0 to array.includes(item)array.indexOf(item) > -1 to array.includes(item)array.indexOf(item) === -1 to !array.includes(item)Usage Example:
// Before
if (items.indexOf('apple') !== -1) {
console.log('Found apple');
}
if (users.indexOf(currentUser) >= 0) {
console.log('User exists');
}
// After
if (items.includes('apple')) {
console.log('Found apple');
}
if (users.includes(currentUser)) {
console.log('User exists');
}