Fixes preprocessor testing and adds deadcode removal.

This commit is contained in:
Yury Delendik 2017-01-09 13:15:09 -06:00
parent f828f07ccd
commit 6265bb6038
7 changed files with 100 additions and 46 deletions

View File

@ -1,10 +1,10 @@
function test() {
"test";
"1";
"2";
"3";
if ("test") {
"5";
}
"4";
"test";
"1";
"2";
"3";
if ("test") {
"5";
}
"4";
}

View File

@ -1,28 +1,28 @@
function f1() {
/* head */
"1";
/* mid */
"2";
/* head */
"1";
/* mid */
"2";
}
/* tail */
function f2() {
// head
"1";
// mid
"2";
// head
"1";
// mid
"2";
}
// tail
function f2() {
if ("1") {
// begin block
"1";
if ("1") {
// begin block
"1";
}
"2";
// trailing
if (/* s */
"3")
/*e*/
{
"4";
}
"2";
// trailing
if (/* s */
"3")
/*e*/
{
"4";
}
}

View File

@ -0,0 +1,13 @@
function f1() {
}
function f2() {
return 1;
}
function f3() {
var i = 0;
throw "test";
}
function f4() {
var i = 0;
}

View File

@ -0,0 +1,25 @@
function f1() {
return;
var i = 0;
}
function f2() {
return 1;
var i = 0;
}
function f3() {
var i = 0;
throw "test";
var j = 0;
}
function f4() {
var i = 0;
if (true) {
return;
}
throw "test";
var j = 0;
}

View File

@ -5,8 +5,8 @@ var d = false;
var e = true;
var f = 'text';
var g = {
"obj": { "i": 1 },
"j": 2
"obj": { "i": 1 },
"j": 2
};
var h = { 'test': 'test' };
var i = '0';

View File

@ -1,17 +1,17 @@
if ('test') {
"1";
"1";
}
{
"1";
"1";
}
{
"1";
"1";
}
;
{
"2";
"2";
}
;
if ('1') {
"1";
"1";
}

View File

@ -169,22 +169,38 @@ function postprocessNode(ctx, node) {
case 'BlockStatement':
var subExpressionIndex = 0;
while (subExpressionIndex < node.body.length) {
if (node.body[subExpressionIndex].type === 'EmptyStatement') {
// Removing empty statements from the blocks.
node.body.splice(subExpressionIndex, 1);
continue;
}
if (node.body[subExpressionIndex].type === 'BlockStatement') {
// Block statements inside a block are moved to the parent one.
var subChildren = node.body[subExpressionIndex].body;
Array.prototype.splice.apply(node.body,
[subExpressionIndex, 1].concat(subChildren));
subExpressionIndex += subChildren.length;
continue;
switch (node.body[subExpressionIndex].type) {
case 'EmptyStatement':
// Removing empty statements from the blocks.
node.body.splice(subExpressionIndex, 1);
continue;
case 'BlockStatement':
// Block statements inside a block are moved to the parent one.
var subChildren = node.body[subExpressionIndex].body;
Array.prototype.splice.apply(node.body,
[subExpressionIndex, 1].concat(subChildren));
subExpressionIndex += Math.max(subChildren.length - 1, 0);
continue;
case 'ReturnStatement':
case 'ThrowStatement':
// Removing dead code after return or throw.
node.body.splice(subExpressionIndex + 1,
node.body.length - subExpressionIndex - 1);
break;
}
subExpressionIndex++;
}
break;
case 'FunctionDeclaration':
case 'FunctionExpression':
var block = node.body;
if (block.body.length > 0 &&
block.body[block.body.length - 1].type === 'ReturnStatement' &&
!block.body[block.body.length - 1].argument) {
// Function body ends with return without arg -- removing it.
block.body.pop();
}
break;
}
return node;
}