Skip to content

Commit

Permalink
api: add code argument for .translate
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Apr 9, 2014
1 parent 5962409 commit 42476c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/xjst/api.js
Expand Up @@ -8,6 +8,10 @@ exports.compile = function compile(code, options) {
return xjst.compiler.create(options).compile(code);
};

exports.translate = function translate(ast, options) {
return xjst.compiler.create(options).translate(ast);
exports.translate = function translate(ast, code, options) {
if (typeof code !== 'string') {
options = code;
code = null;
}
return xjst.compiler.create(options).translate(ast, code);
};
10 changes: 6 additions & 4 deletions lib/xjst/compiler/base.js
Expand Up @@ -117,13 +117,12 @@ Compiler.prototype.generate = function generate(code) {
'};';
}

this.code = code;
var ast = esprima.parse(code, {
loc: true
});
assert.equal(ast.type, 'Program');

ast = this.translate(ast);
ast = this.translate(ast, code);

var uast = uglify.AST_Node.from_mozilla_ast(ast);

Expand Down Expand Up @@ -151,7 +150,7 @@ Compiler.prototype.compile = function compile(code) {
};

Compiler.prototype.getNodeLine = function getNodeLine(node) {
if (!node.loc)
if (!this.code || !node.loc)
return '';

var loc = node.loc.start;
Expand Down Expand Up @@ -184,7 +183,10 @@ Compiler.prototype.assertEqual = function assertEqual(node, lhs, rhs, text) {
};

// Run compiler in phases to translate AST to AST
Compiler.prototype.translate = function translate(ast) {
Compiler.prototype.translate = function translate(ast, code) {
if (code)
this.code = code;

// 1. Get all template()() invokations (and other chunks of code)
var program = this.getTemplates(ast);

Expand Down

0 comments on commit 42476c3

Please sign in to comment.