aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-03-05 17:15:37 +0800
committerGitHub <noreply@github.com>2017-03-05 17:15:37 +0800
commit067e5a5762cb5da8948ccb9bde4d020decb8d55b (patch)
tree0f8b2ed3af9083cd1ba6cac123ff9f8e94f3c670
parent33b5f3198469f53641172c0702a7f40566325fb0 (diff)
downloadtracifyjs-067e5a5762cb5da8948ccb9bde4d020decb8d55b.tar.gz
tracifyjs-067e5a5762cb5da8948ccb9bde4d020decb8d55b.zip
fixup for #1553 (#1555)
- `++a` is the one that is foldable - transform `a++` into `++a` for better optimisation
-rw-r--r--lib/compress.js10
-rw-r--r--test/compress/issue-368.js16
2 files changed, 22 insertions, 4 deletions
diff --git a/lib/compress.js b/lib/compress.js
index f1409d90..d9a67c16 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2833,7 +2833,7 @@ merge(Compressor.prototype, {
if (self.car instanceof AST_Assign
&& !self.car.left.has_side_effects(compressor)) {
left = self.car.left;
- } else if (self.car instanceof AST_UnaryPostfix
+ } else if (self.car instanceof AST_Unary
&& (self.car.operator == "++" || self.car.operator == "--")) {
left = self.car.expression;
}
@@ -2842,11 +2842,15 @@ merge(Compressor.prototype, {
var cdr = self.cdr;
while (true) {
if (cdr.equivalent_to(left)) {
+ var car = self.car instanceof AST_UnaryPostfix ? make_node(AST_UnaryPrefix, self.car, {
+ operator: self.car.operator,
+ expression: left
+ }) : self.car;
if (parent) {
- parent[field] = self.car;
+ parent[field] = car;
return self.cdr;
}
- return self.car;
+ return car;
}
if (cdr instanceof AST_Binary && !(cdr instanceof AST_Assign)) {
field = cdr.left.is_constant() ? "right" : "left";
diff --git a/test/compress/issue-368.js b/test/compress/issue-368.js
index 8c41a894..5960aa64 100644
--- a/test/compress/issue-368.js
+++ b/test/compress/issue-368.js
@@ -21,6 +21,16 @@ collapse: {
var a;
a = b(a / 2);
if (a < 0) {
+ a++;
+ ++c;
+ return c / 2;
+ }
+ }
+ function f4(c) {
+ var a;
+ a = b(a / 2);
+ if (a < 0) {
+ a++;
c++;
return c / 2;
}
@@ -35,7 +45,11 @@ collapse: {
}
function f3(c) {
var a;
- if ((a = b(a / 2)) < 0) return c++ / 2;
+ if ((a = b(a / 2)) < 0) return a++, ++c / 2;
+ }
+ function f4(c) {
+ var a;
+ if ((a = b(a / 2)) < 0) return a++, ++c / 2;
}
}
}