Skip to content

Commit

Permalink
inliner: inline expr body of while/for/if
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Sep 23, 2015
1 parent d26849c commit 542983c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/xjst/compiler/helpers/inliner.js
Expand Up @@ -27,6 +27,43 @@ Inliner.prototype.inline = function run(ast) {
};

Inliner.prototype.enter = function enter(node, parent) {
// while (...) (function() {})()
// for (...) (function() {})()
if ((node.type === 'WhileStatement' ||
node.type === 'ForStatement') &&
node.body.type === 'ExpressionStatement') {
var block = { type: 'BlockStatement', body: [ node.body ] };
return {
type: node.type,
init: node.init,
test: node.test,
update: node.update,
body: block
};

// if (...) (function() {})()
} else if (node.type === 'IfStatement') {
var clone = {
type: node.type,
test: node.test,
consequent: node.consequent,
alternate: node.alternate
};

var change = false;
if (clone.consequent.type === 'ExpressionStatement') {
change = true;
clone.consequent = { type: 'BlockStatement', body: [ clone.consequent ] };
}
if (clone.alternate && clone.alternate.type === 'ExpressionStatement') {
change = true;
clone.alternate = { type: 'BlockStatement', body: [ clone.alternate ] };
}

if (change)
return clone;
}

if (Array.isArray(node.body)) {
this.blockHistory.push(node);
this.lastBlock = node;
Expand Down

0 comments on commit 542983c

Please sign in to comment.