aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-03-24 18:52:48 +0800
committerGitHub <noreply@github.com>2017-03-24 18:52:48 +0800
commit0432a7abb98f3aec871daa88331aa9223979dde3 (patch)
tree48b15e34a50aa13c1138a083457f3842ffa3f078
parentf3a1694a4182e1a26d3dd63dd21fcd4b38dafe3a (diff)
downloadtracifyjs-0432a7abb98f3aec871daa88331aa9223979dde3.tar.gz
tracifyjs-0432a7abb98f3aec871daa88331aa9223979dde3.zip
fix assignment extraction from conditional (#1651)
fixes #1645 fixes #1646
-rw-r--r--lib/compress.js16
-rw-r--r--test/compress/conditionals.js40
2 files changed, 47 insertions, 9 deletions
diff --git a/lib/compress.js b/lib/compress.js
index b3edb840..cbcb7b86 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -3550,19 +3550,17 @@ merge(Compressor.prototype, {
}
var consequent = self.consequent;
var alternative = self.alternative;
+ // if (foo) exp = something; else exp = something_else;
+ // |
+ // v
+ // exp = foo ? something : something_else;
if (consequent instanceof AST_Assign
&& alternative instanceof AST_Assign
&& consequent.operator == alternative.operator
&& consequent.left.equivalent_to(alternative.left)
- && (!consequent.left.has_side_effects(compressor)
- || !self.condition.has_side_effects(compressor))
- ) {
- /*
- * Stuff like this:
- * if (foo) exp = something; else exp = something_else;
- * ==>
- * exp = foo ? something : something_else;
- */
+ && (!self.condition.has_side_effects(compressor)
+ || consequent.operator == "="
+ && !consequent.left.has_side_effects(compressor))) {
return make_node(AST_Assign, self, {
operator: consequent.operator,
left: consequent.left,
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index 7c81cc80..c5639836 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -893,3 +893,43 @@ equality_conditionals_true: {
}
expect_stdout: true
}
+
+issue_1645_1: {
+ options = {
+ conditionals: true,
+ }
+ input: {
+ var a = 100, b = 10;
+ (b = a) ? a++ + (b += a) ? b += a : b += a : b ^= a;
+ console.log(a, b);
+ }
+ expect: {
+ var a = 100, b = 10;
+ (b = a) ? (a++ + (b += a), b += a) : b ^= a;
+ console.log(a,b);
+ }
+ expect_stdout: true
+}
+
+issue_1645_2: {
+ options = {
+ conditionals: true,
+ }
+ input: {
+ var a = 0;
+ function f() {
+ return a++;
+ }
+ f() ? a += 2 : a += 4;
+ console.log(a);
+ }
+ expect: {
+ var a = 0;
+ function f(){
+ return a++;
+ }
+ f() ? a += 2 : a += 4;
+ console.log(a);
+ }
+ expect_stdout: true
+}