Skip to content

Commit

Permalink
fix corner case in evaluate (#3344)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Mar 18, 2019
1 parent 4430a43 commit 7aa7f21
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/compress.js
Expand Up @@ -537,13 +537,11 @@ merge(Compressor.prototype, {
var d = sym.definition();
var safe = safe_to_assign(tw, d, sym.scope, node.right);
d.assignments++;
if (!safe) return;
var fixed = d.fixed;
if (!fixed && node.operator != "=") return;
var eq = node.operator == "=";
var value = eq ? node.right : node;
if (is_modified(compressor, tw, node, value, 0)) return;
d.references.push(sym);
if (!eq) d.chained = true;
d.fixed = eq ? function() {
return node.right;
Expand All @@ -554,6 +552,8 @@ merge(Compressor.prototype, {
right: node.right
});
};
if (!safe) return;
d.references.push(sym);
mark(tw, d, false);
node.right.walk(tw);
mark(tw, d, true);
Expand Down Expand Up @@ -783,10 +783,8 @@ merge(Compressor.prototype, {
var d = exp.definition();
var safe = safe_to_assign(tw, d, exp.scope, true);
d.assignments++;
if (!safe) return;
var fixed = d.fixed;
if (!fixed) return;
d.references.push(exp);
d.chained = true;
d.fixed = function() {
return make_node(AST_Binary, node, {
Expand All @@ -800,6 +798,8 @@ merge(Compressor.prototype, {
})
});
};
if (!safe) return;
d.references.push(exp);
mark(tw, d, true);
return true;
});
Expand Down
44 changes: 44 additions & 0 deletions test/compress/evaluate.js
Expand Up @@ -1610,3 +1610,47 @@ truthy_loops: {
}
}
}

if_increment: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function(a) {
if (console)
return ++a;
}(0));
}
expect: {
console.log(function(a) {
if (console)
return 1;
}());
}
expect_stdout: "1"
}

try_increment: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
console.log(function(a) {
try {
return ++a;
} catch (e) {}
}(0));
}
expect: {
console.log(function(a) {
try {
return 1;
} catch (e) {}
}());
}
expect_stdout: "1"
}

0 comments on commit 7aa7f21

Please sign in to comment.