Skip to content

Commit

Permalink
fix block elimination (#2023)
Browse files Browse the repository at this point in the history
fixes #1664
fixes #1672
  • Loading branch information
alexlamsl committed May 30, 2017
1 parent ec63588 commit 0cc6ded
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ merge(Compressor.prototype, {
function can_be_evicted_from_block(node) {
return !(
node instanceof AST_DefClass ||
node instanceof AST_Defun ||
node instanceof AST_Let ||
node instanceof AST_Const
);
Expand Down Expand Up @@ -1997,7 +1998,8 @@ merge(Compressor.prototype, {
self.body = tighten_body(self.body, compressor);
switch (self.body.length) {
case 1:
if (can_be_evicted_from_block(self.body[0])) {
if (!compressor.has_directive("use strict") && compressor.parent() instanceof AST_If
|| can_be_evicted_from_block(self.body[0])) {
return self.body[0];
}
break;
Expand Down
139 changes: 139 additions & 0 deletions test/compress/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,142 @@ keep_some_blocks: {
} else stuff();
}
}

issue_1664: {
input: {
var a = 1;
function f() {
if (undefined) a = 2;
{
function undefined() {}
undefined();
}
}
f();
console.log(a);
}
expect: {
var a = 1;
function f() {
if (undefined) a = 2;
{
function undefined() {}
undefined();
}
}
f();
console.log(a);
}
expect_stdout: "1"
node_version: ">=6"
}

issue_1672_for: {
input: {
switch (function() {
return xxx;
}) {
case xxx:
for (; console.log("FAIL");) {
function xxx() {}
}
break;
}
}
expect: {
switch (function() {
return xxx;
}) {
case xxx:
for (; console.log("FAIL");) {
function xxx() {}
}
break;
}
}
expect_stdout: true
node_version: ">=6"
}

issue_1672_for_strict: {
input: {
"use strict";
switch (function() {
return xxx;
}) {
case xxx:
for (; console.log("FAIL");) {
function xxx() {}
}
break;
}
}
expect: {
"use strict";
switch (function() {
return xxx;
}) {
case xxx:
for (; console.log("FAIL");) {
function xxx() {}
}
break;
}
}
expect_stdout: true
node_version: ">=6"
}

issue_1672_if: {
input: {
switch (function() {
return xxx;
}) {
case xxx:
if (console.log("FAIL")) {
function xxx() {}
}
break;
}
}
expect: {
switch (function() {
return xxx;
}) {
case xxx:
if (console.log("FAIL")) function xxx() {}
break;
}
}
expect_stdout: true
node_version: ">=6"
}

issue_1672_if_strict: {
input: {
"use strict";
switch (function() {
return xxx;
}) {
case xxx:
if (console.log("FAIL")) {
function xxx() {}
}
break;
}
}
expect: {
"use strict";
switch (function() {
return xxx;
}) {
case xxx:
if (console.log("FAIL")) {
function xxx() {}
}
break;
}
}
expect_stdout: true
node_version: ">=6"
}

0 comments on commit 0cc6ded

Please sign in to comment.