CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lebab

JavaScript ES5 to ES6/ES7 transpiler that performs the opposite function of Babel

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

unsafe-transforms.mddocs/

Unsafe Transforms

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.

Capabilities

Let/Const Variables

Converts var to let/const based on usage patterns.

// Transform name: "let"

What it transforms:

  • Never-modified variables to const
  • Block-scoped variables to let
  • Properly recognizes block-scoping rules
  • Splits single var declarations to multiple let/const if needed
  • Recognizes destructuring assignments

Limitations and Bugs:

  • BUG: Fails with repeated variable definitions that use destructuring
  • BUG: Fails with closure over a loop variable
  • BUG: Fails when function closes over variable declared after function is called
  • Variables that conflict with block-scoping are not converted
  • Repeated declarations of the same var are not converted

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 const

ES6 Classes

Converts function constructors and prototypes to ES6 classes.

// Transform name: "class"

What it transforms:

  • Foo.prototype.method = function(){} to class methods
  • Foo.prototype = { ...methods... } to class body
  • Static methods like Foo.method = function(){}
  • Getters/setters defined with Object.defineProperty()
  • Inheritance with Child.prototype = new Parent()
  • Inheritance with util.inherits(Child, Parent)
  • Superclass constructor calls to super()
  • Superclass method calls to super.method()

Limitations:

  • Does not require super() call in subclass constructor
  • Does not enforce super() call position in subclass constructor
  • Does not support namespaced classes

Usage 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;
  }
}

ES6 Modules

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 = 123
  • exports.foo = bar to export {bar as foo}

Limitations:

  • Does not check if named export conflicts with existing variable names
  • Ignores imports/exports inside nested blocks/functions
  • Only handles require() calls in var declarations
  • Does not ensure that imported variable is treated as const
  • Does not ensure named exports are imported with correct ES6 syntax

Usage 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'
};

Template Literals

Converts string concatenation to template literals.

// Transform name: "template"

What it transforms:

  • String concatenation using + to template literals
  • Variables and expressions to ${...} syntax

Limitations and Bugs:

  • BUG: Removes indentation of multi-line strings
  • LIMITATION: Ignores difference between .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`;

Default Parameters

Converts default parameter patterns to ES6 default parameters.

// Transform name: "default-param"

What it transforms:

  • a = a || 2 patterns to default parameters
  • a = a ? a : 2 patterns to default parameters
  • a = typeof a !== 'undefined' ? a : 2 patterns

Usage 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 + '!';
}

Destructuring Parameters

Converts parameter patterns to destructuring syntax.

// Transform name: "destruct-param"

What it transforms:

  • Functions that access object properties to destructuring parameters
  • Parameter object property access patterns

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);
}

Array Includes

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');
}

docs

cli-usage.md

index.md

safe-transforms.md

unsafe-transforms.md

tile.json