diff --git a/doc/usage.md b/doc/usage.md index f9daf47ff2..46baecce82 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -95,6 +95,15 @@ to5.transformFile("filename.js", options, function (err, result) { }); ``` +### to5.transform.fromAst(ast, [code], [opts]) + +```javascript +var result = to5.transform(ast, "var a = 2;", opts); +result.code; +result.map; +result.ast; +``` + #### Options ```javascript diff --git a/lib/6to5/file.js b/lib/6to5/file.js index de9957fe79..0e1207b023 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -156,14 +156,16 @@ File.prototype.errorWithNode = function (node, msg, Error) { return err; }; -File.prototype.parse = function (code) { +File.prototype.addCode = function (code) { code = (code || "") + ""; + this.code = code; + return this.parseShebang(code); +}; +File.prototype.parse = function (code) { var self = this; - this.code = code; - - code = this.parseShebang(code); + this.addCode(code); return util.parse(this.opts, code, function (tree) { self.transform(tree); diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 497d65d376..94afc696c4 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -2,6 +2,7 @@ module.exports = transform; var Transformer = require("./transformer"); var File = require("../file"); +var util = require("../util"); var _ = require("lodash"); function transform(code, opts) { @@ -9,6 +10,15 @@ function transform(code, opts) { return file.parse(code); } +transform.fromAst = function (ast, code, opts) { + ast = util.normaliseAst(ast); + + var file = new File(opts); + file.addCode(code); + file.transform(); + return file.generate(); +}; + transform._ensureTransformerNames = function (type, keys) { _.each(keys, function (key) { if (!_.has(transform.transformers, key)) { @@ -30,9 +40,9 @@ transform.moduleFormatters = { _.each({ // spec - _blockHoistFunctions: require("./transformers/spec-block-hoist-functions"), - _noForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"), - _noDuplicateProperties: require("./transformers/spec-no-duplicate-properties"), + specBlockHoistFunctions: require("./transformers/spec-block-hoist-functions"), + specNoForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"), + specNoDuplicateProperties: require("./transformers/spec-no-duplicate-properties"), // playground methodBinding: require("./transformers/playground-method-binding"), @@ -70,8 +80,8 @@ _.each({ generators: require("./transformers/es6-generators"), // spec - _propertyLiterals: require("./transformers/spec-property-literals"), - _memberExpressioLiterals: require("./transformers/spec-member-expression-literals"), + specPropertyLiterals: require("./transformers/spec-property-literals"), + specMemberExpressioLiterals: require("./transformers/spec-member-expression-literals"), // wrap up _aliasFunctions: require("./transformers/_alias-functions"), diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 7bfe4e5ac3..ab43d7a30e 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -212,6 +212,20 @@ exports.repeat = function (width, cha) { return new Array(width + 1).join(cha); }; +exports.normaliseAst = function (ast, comments, tokens) { + if (ast && ast.type === "Program") { + ast = t.file(ast, comments || [], tokens || []); + + traverse(ast, function (node, parent) { + node._parent = parent; + }); + + return ast; + } else { + throw new Error("Not a valid ast?"); + } +}; + exports.parse = function (opts, code, callback) { try { var comments = []; @@ -231,11 +245,7 @@ exports.parse = function (opts, code, callback) { estraverse.attachComments(ast, comments, tokens); - ast = t.file(ast, comments, tokens); - - traverse(ast, function (node, parent) { - node._parent = parent; - }); + ast = exports.normaliseAst(ast, comments, tokens); if (callback) { return callback(ast);