The modular transformation system with individual transformers for different ES6+ features. Each transformer handles the conversion of specific JavaScript language features from ES6+ to ES5-compatible code.
/**
* Available transformers by name
*/
const transformers: { [name: string]: Transformer };
/**
* Transformer namespaces grouping related transformers
*/
const namespaces: {
es3: string[];
es5: string[];
es6: string[];
es7: string[];
spec: string[];
validation: string[];
minification: string[];
other: string[];
playground: string[];
internal: string[];
};
/**
* Mapping from transformer names to their namespace
*/
const transformerNamespaces: { [transformer: string]: string };
/**
* Deprecated transformer names mapped to current names
*/
const deprecatedTransformerMap: { [oldName: string]: string };/**
* Transform arrow functions to regular functions
* Input: (a, b) => a + b
* Output: function (a, b) { return a + b; }
*/
const "es6.arrowFunctions": Transformer;/**
* Transform ES6 classes to constructor functions and prototypes
* Input: class Foo extends Bar { method() {} }
* Output: function Foo() {} Foo.prototype.method = function() {}
*/
const "es6.classes": Transformer;/**
* Transform template literals to string concatenation
* Input: `Hello ${name}!`
* Output: "Hello " + name + "!"
*/
const "es6.templateLiterals": Transformer;/**
* Transform destructuring assignments and parameters
* Input: const {a, b} = obj; [x, y] = arr;
* Output: var a = obj.a, b = obj.b; x = arr[0]; y = arr[1];
*/
const "es6.destructuring": Transformer;/**
* Transform let and const to var with proper scoping
* Input: { let x = 1; const y = 2; }
* Output: { var x = 1; var y = 2; }
*/
const "es6.blockScoping": Transformer;/**
* Transform for-of loops to for loops with iterators
* Input: for (let item of items) {}
* Output: for (var _iterator = items[Symbol.iterator]()...) {}
*/
const "es6.forOf": Transformer;/**
* Transform spread operator in arrays and calls
* Input: [...arr1, ...arr2]; fn(...args);
* Output: arr1.concat(arr2); fn.apply(undefined, args);
*/
const "es6.spread": Transformer;/**
* Transform default parameter values
* Input: function fn(a = 1, b = 2) {}
* Output: function fn(a, b) { a = a === undefined ? 1 : a; ... }
*/
const "es6.parameters.default": Transformer;/**
* Transform rest parameters to arguments manipulation
* Input: function fn(a, ...rest) {}
* Output: function fn(a) { var rest = Array.prototype.slice.call(arguments, 1); }
*/
const "es6.parameters.rest": Transformer;/**
* Transform computed property names in object literals
* Input: { [key]: value }
* Output: var _obj = {}; _obj[key] = value;
*/
const "es6.properties.computed": Transformer;/**
* Transform property shorthand notation
* Input: { name, age }
* Output: { name: name, age: age }
*/
const "es6.properties.shorthand": Transformer;/**
* Transform const declarations to var with immutability checks
* Input: const PI = 3.14;
* Output: var PI = 3.14; (with assignment prevention)
*/
const "es6.constants": Transformer;/**
* Transform ES6 import/export to chosen module format
* Input: import {fn} from 'module'; export default class {};
* Output: var fn = require('module').fn; module.exports = class {};
*/
const "es6.modules": Transformer;/**
* Transform super in object methods
* Input: { method() { super.method(); } }
* Output: { method: function() { ParentProto.method.call(this); } }
*/
const "es6.objectSuper": Transformer;/**
* Transform unicode flag in regular expressions
* Input: /pattern/u
* Output: Equivalent regex without unicode flag
*/
const "es6.unicodeRegex": Transformer;/**
* Transform tail calls to avoid stack overflow
*/
const "es6.tailCall": Transformer;/**
* Add temporal dead zone checks for let/const
*/
const "es6.blockScopingTDZ": Transformer;/**
* Transform array and generator comprehensions
* Input: [for (x of arr) if (x > 0) x * 2]
* Output: Equivalent for-loop with array building
*/
const "es7.comprehensions": Transformer;/**
* Transform object rest and spread properties
* Input: {a, ...rest} = obj; newObj = {...obj, b: 2};
* Output: Object.assign and destructuring equivalents
*/
const "es7.objectRestSpread": Transformer;/**
* Transform exponentiation operator
* Input: a ** b
* Output: Math.pow(a, b)
*/
const "es7.exponentiationOperator": Transformer;/**
* Transform abstract property references
*/
const "es7.abstractReferences": Transformer;/**
* Transform ES5 property getters and setters for older environments
*/
const "es5.properties.mutators": Transformer;/**
* Quote reserved words used as property names
* Input: obj.class
* Output: obj["class"]
*/
const "es3.propertyLiterals": Transformer;/**
* Quote reserved words in member expressions
* Input: obj.class
* Output: obj["class"]
*/
const "es3.memberExpressionLiterals": Transformer;/**
* Transform JSX to React.createElement calls
* Input: <div className="foo">Hello</div>
* Output: React.createElement("div", {className: "foo"}, "Hello")
*/
const react: Transformer;/**
* Remove Flow type annotations
* Input: function fn(x: number): string {}
* Output: function fn(x) {}
*/
const flow: Transformer;/**
* Transform async/await and generators using regenerator
* Input: async function fn() { await promise; }
* Output: regenerator-based state machine
*/
const regenerator: Transformer;/**
* Transform async functions to generators
* Input: async function fn() {}
* Output: function fn() { return _asyncToGenerator(function* () {})(); }
*/
const asyncToGenerator: Transformer;/**
* Transform async functions to Bluebird coroutines
*/
const bluebirdCoroutines: Transformer;/**
* Make React transformer produce pre-v0.12 compatible code
*/
const reactCompat: Transformer;/**
* Add "use strict" directive to modules
*/
const useStrict: Transformer;/**
* Include helper functions inline instead of requiring runtime
*/
const selfContained: Transformer;/**
* Check for references to undeclared variables
*/
const "validation.undeclaredVariableCheck": Transformer;/**
* Prevent assignment in for-in and for-of loop heads
*/
const "validation.noForInOfAssignment": Transformer;/**
* Validate setter method signatures
*/
const "validation.setters": Transformer;/**
* Validate React JSX usage
*/
const "validation.react": Transformer;/**
* Handle block-scoped function declarations per spec
*/
const "spec.blockScopedFunctions": Transformer;/**
* Transform __proto__ to Object.setPrototypeOf
*/
const "spec.protoToAssign": Transformer;/**
* Handle typeof Symbol correctly
*/
const "spec.typeofSymbol": Transformer;/**
* Transform undefined to void 0
*/
const "spec.undefinedToVoid": Transformer;/**
* Remove debugger statements
*/
const "minification.removeDebugger": Transformer;/**
* Remove console method calls
*/
const "minification.removeConsoleCalls": Transformer;/**
* Remove unreachable code
*/
const "minification.deadCodeElimination": Transformer;/**
* Rename local variables to shorter names
*/
const "minification.renameLocalVariables": Transformer;Experimental transformers for proposed features:
/**
* Mallet operator transformer
*/
const "playground.malletOperator": Transformer;
/**
* Method binding transformer
*/
const "playground.methodBinding": Transformer;
/**
* Memoization operator transformer
*/
const "playground.memoizationOperator": Transformer;
/**
* Object getter memoization transformer
*/
const "playground.objectGetterMemoization": Transformer;System transformers used internally:
/**
* Module handling transformer
*/
const _modules: Transformer;
/**
* Block hoisting transformer
*/
const _blockHoist: Transformer;
/**
* Declaration handling transformer
*/
const _declarations: Transformer;
/**
* Function aliasing transformer
*/
const _aliasFunctions: Transformer;
/**
* Module formatter application
*/
const _moduleFormatter: Transformer;
/**
* Final cleanup transformer
*/
const _cleanUp: Transformer;const to5 = require("6to5");
// Enable only arrow functions and classes
const result = to5.transform(code, {
whitelist: ["es6.arrowFunctions", "es6.classes"]
});
// Enable all ES6 transformers
const es6Result = to5.transform(code, {
whitelist: to5.namespaces.es6
});// Transform everything except modules
const result = to5.transform(code, {
blacklist: ["es6.modules"]
});
// Disable all minification
const readable = to5.transform(code, {
blacklist: to5.namespaces.minification
});// Enable experimental ES7 features
const futuristic = to5.transform(code, {
optional: [
"es7.comprehensions",
"es7.objectRestSpread",
"es7.exponentiationOperator"
]
});
// Enable playground features
const experimental = to5.transform(code, {
playground: true,
optional: [
"playground.methodBinding",
"playground.memoizationOperator"
]
});// Enable loose mode for faster/smaller output
const loose = to5.transform(code, {
loose: [
"es6.classes", // No __proto__ checks
"es6.modules", // Simpler module compilation
"es6.forOf" // Assume arrays for for-of
]
});// Get all transformers in a namespace
console.log("ES6 transformers:", to5.namespaces.es6);
// Enable entire namespace
const result = to5.transform(code, {
whitelist: to5.namespaces.es6.concat(to5.namespaces.es7)
});