aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-01-01 23:53:15 +0000
committerGitHub <noreply@github.com>2021-01-02 07:53:15 +0800
commitdfb86ccdd1dfe6f07953afc8c308b00c9c6856b1 (patch)
treea0679faaa9095183260c7210f902ff694facc522
parent0417a69c3ef4922470b6891ad6039221059f5750 (diff)
downloadtracifyjs-dfb86ccdd1dfe6f07953afc8c308b00c9c6856b1.tar.gz
tracifyjs-dfb86ccdd1dfe6f07953afc8c308b00c9c6856b1.zip
fix corner case in `conditionals` (#4492)
-rw-r--r--lib/compress.js13
-rw-r--r--test/compress/spread.js22
2 files changed, 32 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 0a560dda..5f8f3736 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -9843,10 +9843,17 @@ merge(Compressor.prototype, {
&& !condition.has_side_effects(compressor)
&& !consequent.expression.has_side_effects(compressor)) {
var node = consequent.clone();
- node.args[arg_index] = make_node(AST_Conditional, self, {
+ var arg = consequent.args[arg_index];
+ node.args[arg_index] = arg instanceof AST_Spread ? make_node(AST_Spread, self, {
+ expression: make_node(AST_Conditional, self, {
+ condition: condition,
+ consequent: arg.expression,
+ alternative: alternative.args[arg_index].expression,
+ }),
+ }) : make_node(AST_Conditional, self, {
condition: condition,
- consequent: consequent.args[arg_index],
- alternative: alternative.args[arg_index]
+ consequent: arg,
+ alternative: alternative.args[arg_index],
});
return node;
}
diff --git a/test/compress/spread.js b/test/compress/spread.js
index 67aa549a..944915fe 100644
--- a/test/compress/spread.js
+++ b/test/compress/spread.js
@@ -85,6 +85,28 @@ collapse_vars_4: {
node_version: ">=6"
}
+conditionals_farg: {
+ options = {
+ conditionals: true,
+ }
+ input: {
+ function log(msg) {
+ console.log(msg);
+ }
+ var a = 42, b = [ "PASS" ], c = [ "FAIL" ];
+ a ? log(...b) : log(...c);
+ }
+ expect: {
+ function log(msg) {
+ console.log(msg);
+ }
+ var a = 42, b = [ "PASS" ], c = [ "FAIL" ];
+ log(...a ? b : c);
+ }
+ expect_stdout: "PASS"
+ node_version: ">=6"
+}
+
dont_inline: {
options = {
inline: true,