Skip to content

Commit

Permalink
fix corner cases in inline (#3507)
Browse files Browse the repository at this point in the history
fixes #3506
  • Loading branch information
alexlamsl committed Oct 22, 2019
1 parent da5a21b commit 0b3705e
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 17 deletions.
12 changes: 8 additions & 4 deletions lib/compress.js
Expand Up @@ -3643,8 +3643,8 @@ merge(Compressor.prototype, {
if (!(sym.definition().id in in_use_ids)) {
sym.__unused = true;
if (trim) {
log(sym, "Dropping unused function argument {name} [{file}:{line},{col}]", template(sym));
a.pop();
AST_Node[sym.unreferenced() ? "warn" : "info"]("Dropping unused function argument {name} [{file}:{line},{col}]", template(sym));
}
} else {
trim = false;
Expand All @@ -3654,7 +3654,7 @@ merge(Compressor.prototype, {
if (drop_funcs && node instanceof AST_Defun && node !== self) {
var def = node.name.definition();
if (!(def.id in in_use_ids)) {
AST_Node[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
log(node.name, "Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
def.eliminated++;
return make_node(AST_EmptyStatement, node);
}
Expand Down Expand Up @@ -3742,7 +3742,7 @@ merge(Compressor.prototype, {
AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name));
side_effects.push(value);
} else {
AST_Node[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
log(def.name, "Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
}
sym.eliminated++;
}
Expand Down Expand Up @@ -3820,6 +3820,10 @@ merge(Compressor.prototype, {
return node;
}

function log(sym, text, props) {
AST_Node[sym.unreferenced() ? "warn" : "info"](text, props);
}

function template(sym) {
return {
name : sym.name,
Expand Down Expand Up @@ -5202,7 +5206,7 @@ merge(Compressor.prototype, {
if (stat instanceof AST_SimpleStatement) {
return make_node(AST_UnaryPrefix, stat, {
operator: "void",
expression: stat.body.clone(true)
expression: stat.body
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/compress/collapse_vars.js
Expand Up @@ -3511,7 +3511,7 @@ issue_2437_2: {
conditionals: true,
inline: true,
join_vars: true,
passes: 2,
passes: 3,
reduce_funcs: true,
reduce_vars: true,
sequences: true,
Expand Down
164 changes: 154 additions & 10 deletions test/compress/functions.js
Expand Up @@ -3066,7 +3066,7 @@ class_iife: {
expect_stdout: "PASS"
}

issue_3400: {
issue_3400_1: {
options = {
collapse_vars: true,
inline: true,
Expand Down Expand Up @@ -3095,17 +3095,71 @@ issue_3400: {
return g;
});
}
expect: {
void console.log(function() {
function g() {
function h(u) {
var o = {
p: u
};
return console.log(o[g]), o;
}
function e() {
return [ 42 ].map(function(v) {
return h(v);
});
}
return e();
}
return g;
}()()[0].p);
}
expect_stdout: [
"undefined",
"42",
]
}

issue_3400_2: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
reduce_funcs: true,
reduce_vars: true,
unused: true,
}
input: {
(function(f) {
console.log(f()()[0].p);
})(function() {
function g() {
function h(u) {
var o = {
p: u
};
return console.log(o[g]), o;
}
function e() {
return [ 42 ].map(function(v) {
return h(v);
});
}
return e();
}
return g;
});
}
expect: {
void console.log(function g() {
function e() {
return [42].map(function(v) {
return o = {
p: v
}, console.log(o[g]) , o;
var o;
});
}
return e();
return [ 42 ].map(function(v) {
return function(u) {
var o = {
p: u
};
return console.log(o[g]), o;
}(v);
});
}()[0].p);
}
expect_stdout: [
Expand Down Expand Up @@ -3196,3 +3250,93 @@ issue_3444: {
}
expect_stdout: "PASS"
}

issue_3506_1: {
options = {
collapse_vars: true,
evaluate: true,
inline: true,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
var a = "FAIL";
(function(b) {
(function(b) {
b && (a = "PASS");
})(b);
})(a);
console.log(a);
}
expect: {
var a = "FAIL";
!function(b) {
b && (a = "PASS");
}(a);
console.log(a);
}
expect_stdout: "PASS"
}

issue_3506_2: {
options = {
collapse_vars: true,
evaluate: true,
inline: true,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
var a = "FAIL";
(function(b) {
(function(c) {
var d = 1;
for (;c && (a = "PASS") && 0 < --d;);
})(b);
})(a);
console.log(a);
}
expect: {
var a = "FAIL";
!function(c) {
var d = 1;
for (;c && (a = "PASS") && 0 < --d;);
}(a);
console.log(a);
}
expect_stdout: "PASS"
}

issue_3506_3: {
options = {
collapse_vars: true,
dead_code: true,
evaluate: true,
inline: true,
loops: true,
reduce_vars: true,
side_effects: true,
unused: true,
}
input: {
var a = "FAIL";
(function(b) {
(function(c) {
var d = 1;
for (;c && (a = "PASS") && 0 < --d;);
})(b);
})(a);
console.log(a);
}
expect: {
var a = "FAIL";
!function(c) {
var d = 1;
for (;c && (a = "PASS") && 0 < --d;);
}(a);
console.log(a);
}
expect_stdout: "PASS"
}
2 changes: 2 additions & 0 deletions test/compress/pure_getters.js
Expand Up @@ -1193,6 +1193,7 @@ issue_3427: {
assignments: true,
collapse_vars: true,
inline: true,
passes: 2,
pure_getters: "strict",
sequences: true,
side_effects: true,
Expand All @@ -1206,4 +1207,5 @@ issue_3427: {
})(a || (a = {}));
}
expect: {}
expect_stdout: true
}
4 changes: 2 additions & 2 deletions test/compress/reduce_vars.js
Expand Up @@ -6609,10 +6609,10 @@ issues_3267_1: {
}
expect: {
!function(i) {
if (i)
if (Object())
return console.log("PASS");
throw "FAIL";
}(Object());
}();
}
expect_stdout: "PASS"
}
Expand Down

0 comments on commit 0b3705e

Please sign in to comment.