aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 3e5b524d..22fb330e 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1380,6 +1380,10 @@ merge(Compressor.prototype, {
});
OPT(AST_Seq, function(self, compressor){
+ if (!compressor.option("side_effects"))
+ return self;
+ if (!self.car.has_side_effects())
+ return self.cdr;
if (compressor.option("cascade")) {
if (self.car instanceof AST_Assign
&& !self.car.left.has_side_effects()
@@ -1395,7 +1399,26 @@ merge(Compressor.prototype, {
return self;
});
+ AST_Unary.DEFMETHOD("lift_sequences", function(compressor){
+ if (compressor.option("sequences")) {
+ if (this.expression instanceof AST_Seq) {
+ var seq = this.expression;
+ var x = seq.to_array();
+ this.expression = x.pop();
+ x.push(this);
+ seq = AST_Seq.from_array(x).transform(compressor);
+ return seq;
+ }
+ }
+ return this;
+ });
+
+ OPT(AST_UnaryPostfix, function(self, compressor){
+ return self.lift_sequences(compressor);
+ });
+
OPT(AST_UnaryPrefix, function(self, compressor){
+ self = self.lift_sequences(compressor);
var e = self.expression;
if (compressor.option("booleans") && compressor.in_boolean_context()) {
switch (self.operator) {
@@ -1418,7 +1441,32 @@ merge(Compressor.prototype, {
return self.evaluate(compressor)[0];
});
+ AST_Binary.DEFMETHOD("lift_sequences", function(compressor){
+ if (compressor.option("sequences")) {
+ if (this.left instanceof AST_Seq) {
+ var seq = this.left;
+ 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.operator == "||" || this.operator == "&&")
+ && !this.left.has_side_effects()) {
+ 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 this;
+ });
+
OPT(AST_Binary, function(self, compressor){
+ self = self.lift_sequences(compressor);
if (compressor.option("comparisons")) switch (self.operator) {
case "===":
case "!==":
@@ -1557,6 +1605,7 @@ merge(Compressor.prototype, {
var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ];
OPT(AST_Assign, function(self, compressor){
+ self = self.lift_sequences(compressor);
if (self.operator == "="
&& self.left instanceof AST_SymbolRef
&& self.right instanceof AST_Binary