diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-03-19 03:04:22 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-19 03:04:22 +0800 |
commit | cd58635dcc8f74aafa842c2015b294ff4097ba08 (patch) | |
tree | d6f382af98d2b71c84948cd389c72ca981361d5e | |
parent | 274331d0ea05197ea7cb531ccd1d78e0c7b8662c (diff) | |
download | tracifyjs-cd58635dcc8f74aafa842c2015b294ff4097ba08.tar.gz tracifyjs-cd58635dcc8f74aafa842c2015b294ff4097ba08.zip |
fix AST_Binary.lift_sequences() (#1621)
Commit eab99a1c fails to account for side effects from compound assignments.
-rw-r--r-- | lib/compress.js | 37 | ||||
-rw-r--r-- | test/compress/sequences.js | 26 |
2 files changed, 40 insertions, 23 deletions
diff --git a/lib/compress.js b/lib/compress.js index 66a4120e..53412a3f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2999,14 +2999,6 @@ merge(Compressor.prototype, { return self; }); - function has_side_effects_or_prop_access(node, compressor) { - var save_pure_getters = compressor.option("pure_getters"); - compressor.options.pure_getters = false; - var ret = node.has_side_effects(compressor); - compressor.options.pure_getters = save_pure_getters; - return ret; - } - AST_Binary.DEFMETHOD("lift_sequences", function(compressor){ if (compressor.option("sequences")) { if (this.left instanceof AST_Seq) { @@ -3014,18 +3006,23 @@ merge(Compressor.prototype, { var x = seq.to_array(); this.left = x.pop(); x.push(this); - seq = AST_Seq.from_array(x).transform(compressor); - return seq; - } - if (this.right instanceof AST_Seq - && this instanceof AST_Assign - && !has_side_effects_or_prop_access(this.left, compressor)) { - var seq = this.right; - var x = seq.to_array(); - this.right = x.pop(); - x.push(this); - seq = AST_Seq.from_array(x).transform(compressor); - return seq; + return AST_Seq.from_array(x).optimize(compressor); + } + if (this.right instanceof AST_Seq && !this.left.has_side_effects(compressor)) { + var assign = this.operator == "=" && this.left instanceof AST_SymbolRef; + var root = this.right; + var cursor, seq = root; + while (assign || !seq.car.has_side_effects(compressor)) { + cursor = seq; + if (seq.cdr instanceof AST_Seq) { + seq = seq.cdr; + } else break; + } + if (cursor) { + this.right = cursor.cdr; + cursor.cdr = this; + return root.optimize(compressor); + } } } return this; diff --git a/test/compress/sequences.js b/test/compress/sequences.js index 41cfc726..f78987aa 100644 --- a/test/compress/sequences.js +++ b/test/compress/sequences.js @@ -103,15 +103,18 @@ lift_sequences_1: { lift_sequences_2: { options = { sequences: true, evaluate: true }; input: { - var foo, bar; + var foo = 1, bar; foo.x = (foo = {}, 10); bar = (bar = {}, 10); + console.log(foo, bar); } expect: { - var foo, bar; + var foo = 1, bar; foo.x = (foo = {}, 10), - bar = {}, bar = 10; + bar = {}, bar = 10, + console.log(foo, bar); } + expect_stdout: true } lift_sequences_3: { @@ -138,6 +141,23 @@ lift_sequences_4: { } } +lift_sequences_5: { + options = { + sequences: true, + } + input: { + var a = 2, b; + a *= (b, a = 4, 3); + console.log(a); + } + expect: { + var a = 2, b; + b, a *= (a = 4, 3), + console.log(a); + } + expect_stdout: "6" +} + for_sequences: { options = { sequences: true }; input: { |