aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-12-10 22:59:21 +0000
committerGitHub <noreply@github.com>2020-12-11 06:59:21 +0800
commit57105b299ec582bc731b58002703faa297f10063 (patch)
tree3e08387fd9e2e644e60461f47f9d782c51611692 /test
parent77e1bda4265c9a3fe36937dd734fab85db38437c (diff)
downloadtracifyjs-57105b299ec582bc731b58002703faa297f10063.tar.gz
tracifyjs-57105b299ec582bc731b58002703faa297f10063.zip
fix corner cases with spread syntax (#4358)
Diffstat (limited to 'test')
-rw-r--r--test/compress/objects.js27
-rw-r--r--test/compress/spread.js273
-rw-r--r--test/ufuzz/index.js21
3 files changed, 298 insertions, 23 deletions
diff --git a/test/compress/objects.js b/test/compress/objects.js
index 5d3653d0..5e59726d 100644
--- a/test/compress/objects.js
+++ b/test/compress/objects.js
@@ -360,3 +360,30 @@ issue_4269_4: {
expect_stdout: "PASS"
node_version: ">=4"
}
+
+issue_4269_5: {
+ options = {
+ evaluate: true,
+ objects: true,
+ }
+ input: {
+ console.log({
+ get 42() {
+ return "FAIL";
+ },
+ [console]: "bar",
+ 42: "PASS",
+ }[42]);
+ }
+ expect: {
+ console.log({
+ get 42() {
+ return "FAIL";
+ },
+ [console]: "bar",
+ 42: "PASS",
+ }[42]);
+ }
+ expect_stdout: "PASS"
+ node_version: ">=4"
+}
diff --git a/test/compress/spread.js b/test/compress/spread.js
index aaa8a7f7..32ed0b86 100644
--- a/test/compress/spread.js
+++ b/test/compress/spread.js
@@ -250,43 +250,119 @@ reduce_vars_2: {
node_version: ">=6"
}
-drop_object: {
+keep_getter_1: {
options = {
side_effects: true,
}
input: {
- ({ ...console.log("PASS") });
+ ({
+ ...{
+ get p() {
+ console.log("PASS");
+ },
+ },
+ get q() {
+ console.log("FAIL");
+ },
+ });
}
expect: {
- console.log("PASS");
+ ({
+ ...{
+ get p() {
+ console.log("PASS");
+ },
+ },
+ });
}
expect_stdout: "PASS"
node_version: ">=8"
}
-keep_getter: {
+keep_getter_2: {
options = {
side_effects: true,
}
input: {
({
- ...{
+ ...(console.log("foo"), {
get p() {
- console.log("PASS");
+ console.log("bar");
},
- },
- get q() {
- console.log("FAIL");
- },
+ }),
});
}
expect: {
({
- ...{
+ ...(console.log("foo"), {
get p() {
- console.log("PASS");
+ console.log("bar");
},
+ }),
+ });
+ }
+ expect_stdout: [
+ "foo",
+ "bar",
+ ]
+ node_version: ">=8"
+}
+
+keep_getter_3: {
+ options = {
+ side_effects: true,
+ }
+ input: {
+ ({
+ ...function() {
+ return {
+ get p() {
+ console.log("PASS");
+ },
+ };
+ }(),
+ });
+ }
+ expect: {
+ ({
+ ...function() {
+ return {
+ get p() {
+ console.log("PASS");
+ },
+ };
+ }(),
+ });
+ }
+ expect_stdout: "PASS"
+ node_version: ">=8"
+}
+
+keep_getter_4: {
+ options = {
+ reduce_vars: true,
+ side_effects: true,
+ toplevel: true,
+ }
+ input: {
+ var o = {
+ get p() {
+ console.log("PASS");
+ },
+ };
+ ({
+ q: o,
+ ...o,
+ });
+ }
+ expect: {
+ var o = {
+ get p() {
+ console.log("PASS");
},
+ };
+ ({
+ ...o,
});
}
expect_stdout: "PASS"
@@ -341,6 +417,179 @@ keep_accessor: {
node_version: ">=8"
}
+object_key_order_1: {
+ options = {
+ objects: true,
+ spread: true,
+ }
+ input: {
+ var o = {
+ ...{},
+ a: 1,
+ b: 2,
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ a: (1, 3),
+ b: 2,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "a 3",
+ "b 2",
+ ]
+ node_version: ">=8 <=10"
+}
+
+object_key_order_2: {
+ options = {
+ objects: true,
+ spread: true,
+ }
+ input: {
+ var o = {
+ a: 1,
+ ...{},
+ b: 2,
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ a: (1, 3),
+ b: 2,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "a 3",
+ "b 2",
+ ]
+ node_version: ">=8"
+}
+
+object_key_order_3: {
+ options = {
+ objects: true,
+ spread: true,
+ }
+ input: {
+ var o = {
+ a: 1,
+ b: 2,
+ ...{},
+ a: 3,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ a: (1, 3),
+ b: 2,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "a 3",
+ "b 2",
+ ]
+ node_version: ">=8"
+}
+
+object_key_order_4: {
+ options = {
+ objects: true,
+ spread: true,
+ }
+ input: {
+ var o = {
+ a: 1,
+ b: 2,
+ a: 3,
+ ...{},
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ a: (1, 3),
+ b: 2,
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "a 3",
+ "b 2",
+ ]
+ node_version: ">=8"
+}
+
+object_spread_array: {
+ options = {
+ objects: true,
+ spread: true,
+ }
+ input: {
+ var o = {
+ ...[ "foo", "bar" ],
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ ...[ "foo", "bar" ],
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "0 foo",
+ "1 bar",
+ ]
+ node_version: ">=8"
+}
+
+object_spread_string: {
+ options = {
+ objects: true,
+ spread: true,
+ }
+ input: {
+ var o = {
+ ..."foo",
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect: {
+ var o = {
+ ..."foo",
+ };
+ for (var k in o)
+ console.log(k, o[k]);
+ }
+ expect_stdout: [
+ "0 f",
+ "1 o",
+ "2 o",
+ ]
+ node_version: ">=8"
+}
+
unused_var_side_effects: {
options = {
unused: true,
diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js
index 568b4822..b6970fb4 100644
--- a/test/ufuzz/index.js
+++ b/test/ufuzz/index.js
@@ -722,17 +722,17 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
var label = createLabel(canBreak, canContinue);
canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK);
canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE);
- return "{var brake" + loop + " = 5; " + label.target + "do {" + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "} while ((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") && --brake" + loop + " > 0);}";
+ return "{var brake" + loop + " = 5; " + label.target + "do {" + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "} while (" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " && --brake" + loop + " > 0);}";
case STMT_WHILE:
var label = createLabel(canBreak, canContinue);
canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK);
canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE);
- return "{var brake" + loop + " = 5; " + label.target + "while ((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") && --brake" + loop + " > 0)" + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "}";
+ return "{var brake" + loop + " = 5; " + label.target + "while (" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " && --brake" + loop + " > 0)" + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "}";
case STMT_FOR_LOOP:
var label = createLabel(canBreak, canContinue);
canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK);
canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE);
- return label.target + "for (var brake" + loop + " = 5; (" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") && brake" + loop + " > 0; --brake" + loop + ")" + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth);
+ return label.target + "for (var brake" + loop + " = 5; " + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " && brake" + loop + " > 0; --brake" + loop + ")" + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth);
case STMT_FOR_IN:
var label = createLabel(canBreak, canContinue);
canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK);
@@ -931,7 +931,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
case p++:
return createExpression(recurmax, COMMA_OK, stmtDepth, canThrow);
case p++:
- return createExpression(recurmax, noComma, stmtDepth, canThrow) + "?" + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + ":" + createExpression(recurmax, noComma, stmtDepth, canThrow);
+ return createExpression(recurmax, noComma, stmtDepth, canThrow) + " ? " + createExpression(recurmax, NO_COMMA, stmtDepth, canThrow) + " : " + createExpression(recurmax, noComma, stmtDepth, canThrow);
case p++:
case p++:
var nameLenBefore = VAR_NAMES.length;
@@ -1031,14 +1031,13 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
case p++:
return createUnarySafePrefix() + "(" + createNestedBinaryExpr(recurmax, noComma, stmtDepth, canThrow) + ")";
case p++:
- return " ((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") || a || 3).toString() ";
+ return " (" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " || a || 3).toString() ";
case p++:
- return " /[abc4]/.test(((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") || b || 5).toString()) ";
+ return " /[abc4]/.test((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " || b || 5).toString()) ";
case p++:
- return " /[abc4]/g.exec(((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ") || b || 5).toString()) ";
+ return " /[abc4]/g.exec((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " || b || 5).toString()) ";
case p++:
- return " ((" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) +
- ") || " + rng(10) + ").toString()[" +
+ return " (" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " || " + rng(10) + ").toString()[" +
createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "] ";
case p++:
return createArrayLiteral(recurmax, stmtDepth, canThrow);
@@ -1209,10 +1208,10 @@ function createObjectLiteral(recurmax, stmtDepth, canThrow) {
obj.push("..." + getVarName() + ",");
break;
case 4:
- obj.push("..." + createObjectLiteral(recurmax, stmtDepth, canThrow) + ",");
+ obj.push("..." + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ",");
break;
default:
- obj.push(createObjectKey(recurmax, stmtDepth, canThrow) + ":(" + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "),");
+ obj.push(createObjectKey(recurmax, stmtDepth, canThrow) + ": " + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ",");
break;
}
obj.push("})");