aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-05-22 04:38:09 +0100
committerGitHub <noreply@github.com>2020-05-22 11:38:09 +0800
commitd1cc5270a3c4e34f0e639a869ecd1df21cc62fe7 (patch)
tree9e88106022e52efb0dc3ba0e5e34c5300a8a5e9f
parent75c5b6029be45c831ea01e9a5396909a969c7789 (diff)
downloadtracifyjs-d1cc5270a3c4e34f0e639a869ecd1df21cc62fe7.tar.gz
tracifyjs-d1cc5270a3c4e34f0e639a869ecd1df21cc62fe7.zip
fix corner case in `evaluate` (#3921)
fixes #3920
-rw-r--r--lib/compress.js31
-rw-r--r--test/compress/evaluate.js22
2 files changed, 42 insertions, 11 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 02b18adf..a03b14e9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3256,11 +3256,21 @@ merge(Compressor.prototype, {
&& unaryPrefix[this.operator];
}
});
- function modified(sym) {
- if (!(sym instanceof AST_SymbolRef)) return;
- sym.definition().references.forEach(function(node) {
- delete node._eval;
- });
+ var scan_modified = new TreeWalker(function(node) {
+ if (node instanceof AST_Assign) modified(node.left);
+ if (node instanceof AST_Unary && unary_arithmetic[node.operator]) modified(node.expression);
+ });
+ function modified(node) {
+ if (node instanceof AST_Dot) {
+ modified(node.expression);
+ } else if (node instanceof AST_Sub) {
+ modified(node.expression);
+ node.property.walk(scan_modified);
+ } else if (node instanceof AST_SymbolRef) {
+ node.definition().references.forEach(function(ref) {
+ delete ref._eval;
+ });
+ }
}
def(AST_Statement, function() {
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
@@ -3280,12 +3290,11 @@ merge(Compressor.prototype, {
});
def(AST_Sequence, function(compressor, ignore_side_effects, cached, depth) {
if (!ignore_side_effects) return this;
- var tail = this.tail_node();
- this.walk(new TreeWalker(function(node) {
- if (node === tail) return true;
- if (node instanceof AST_Assign) modified(node.left);
- if (node instanceof AST_Unary && unary_arithmetic[node.operator]) modified(node.expression);
- }));
+ var exprs = this.expressions;
+ for (var i = 0, last = exprs.length - 1; i < last; i++) {
+ exprs[i].walk(scan_modified);
+ }
+ var tail = exprs[last];
var value = tail._eval(compressor, ignore_side_effects, cached, depth);
return value === tail ? this : value;
});
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index 247a3c62..db321450 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -2463,3 +2463,25 @@ issue_3905: {
}
expect_stdout: "0"
}
+
+issue_3920: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ unused: true,
+ }
+ input: {
+ var a = function(b) {
+ return (b[b = 0] = 0) >= (b ? 0 : 1);
+ }("foo");
+ console.log(a);
+ }
+ expect: {
+ var a = function(b) {
+ return (b[b = 0] = 0) >= (b ? 0 : 1);
+ }("foo");
+ console.log(a);
+ }
+ expect_stdout: "false"
+}