es6-arrow-function
Version:
Shorthand arrow functions compiled to ES5.
60 lines (51 loc) • 1.58 kB
JavaScript
/* jshint node:true, undef:true, unused:true */
var through = require('through');
var recast = require('recast');
var types = recast.types;
var Visitor = require('./visitors/arrow-function-expression');
/**
* Transform an Esprima AST generated from ES6 by replacing all
* ArrowFunctionExpression usages with the non-shorthand FunctionExpression.
*
* NOTE: The argument may be modified by this function. To prevent modification
* of your AST, pass a copy instead of a direct reference:
*
* // instead of transform(ast), pass a copy
* transform(JSON.parse(JSON.stringify(ast));
*
* @param {Object} ast
* @return {Object}
*/
function transform(ast) {
return types.visit(ast, Visitor.visitor);
}
/**
* Transform JavaScript written using ES6 by replacing all arrow function
* usages with the non-shorthand "function" keyword.
*
* compile('() => 42'); // 'function() { return 42; };'
*
* @param {string} source
* @param {object} mapOptions
* @return {string}
*/
function compile(source, mapOptions) {
mapOptions = mapOptions || {};
var recastOptions = {
sourceFileName: mapOptions.sourceFileName,
sourceMapName: mapOptions.sourceMapName
};
var ast = recast.parse(source, recastOptions);
return recast.print(transform(ast), recastOptions);
}
module.exports = function() {
var data = '';
return through(write, end);
function write(buf) { data += buf; }
function end() {
this.queue(module.exports.compile(data).code);
this.queue(null);
}
};
module.exports.compile = compile;
module.exports.transform = transform;