Skip to content

Commit ffe0fe7

Browse files
authoredJun 5, 2024··
enhance join_vars (#5824)
closes #5790
1 parent aa7c338 commit ffe0fe7

File tree

2 files changed

+140
-10
lines changed

2 files changed

+140
-10
lines changed
 

‎lib/compress.js

+42-10
Original file line numberDiff line numberDiff line change
@@ -4222,16 +4222,48 @@ Compressor.prototype.compress = function(node) {
42224222
if (value.left instanceof AST_SymbolRef) names.set(value.left.name, true);
42234223
value = value.right;
42244224
}
4225-
if (!(value instanceof AST_Object)) return;
4226-
var trimmed = false;
4227-
do {
4228-
if (!try_join(exprs[0])) break;
4229-
exprs.shift();
4230-
trimmed = true;
4231-
} while (exprs.length);
4232-
return trimmed;
4225+
if (value instanceof AST_Array) {
4226+
var trimmed = false;
4227+
do {
4228+
if (!try_join_array(exprs[0])) break;
4229+
exprs.shift();
4230+
trimmed = true;
4231+
} while (exprs.length);
4232+
return trimmed;
4233+
} else if (value instanceof AST_Object) {
4234+
var trimmed = false;
4235+
do {
4236+
if (!try_join_object(exprs[0])) break;
4237+
exprs.shift();
4238+
trimmed = true;
4239+
} while (exprs.length);
4240+
return trimmed;
4241+
}
4242+
4243+
function try_join_array(node) {
4244+
if (!(node instanceof AST_Assign)) return;
4245+
if (node.operator != "=") return;
4246+
if (!(node.left instanceof AST_PropAccess)) return;
4247+
var sym = node.left.expression;
4248+
if (!(sym instanceof AST_SymbolRef)) return;
4249+
if (!names.has(sym.name)) return;
4250+
if (!node.right.is_constant_expression(scope)) return;
4251+
var prop = node.left.property;
4252+
if (prop instanceof AST_Node) {
4253+
if (try_join_array(prop)) prop = node.left.property = prop.right.clone();
4254+
prop = prop.evaluate(compressor);
4255+
}
4256+
if (prop instanceof AST_Node) return;
4257+
if (!RE_POSITIVE_INTEGER.test("" + prop)) return;
4258+
prop = +prop;
4259+
var len = value.elements.length;
4260+
if (prop > len + 4) return;
4261+
while (len < prop) value.elements[len++] = make_node(AST_Hole, value);
4262+
value.elements[prop] = node.right;
4263+
return true;
4264+
}
42334265

4234-
function try_join(node) {
4266+
function try_join_object(node) {
42354267
if (!(node instanceof AST_Assign)) return;
42364268
if (node.operator != "=") return;
42374269
if (!(node.left instanceof AST_PropAccess)) return;
@@ -4241,7 +4273,7 @@ Compressor.prototype.compress = function(node) {
42414273
if (!node.right.is_constant_expression(scope)) return;
42424274
var prop = node.left.property;
42434275
if (prop instanceof AST_Node) {
4244-
if (try_join(prop)) prop = node.left.property = prop.right.clone();
4276+
if (try_join_object(prop)) prop = node.left.property = prop.right.clone();
42454277
prop = prop.evaluate(compressor);
42464278
}
42474279
if (prop instanceof AST_Node) return;

‎test/compress/join_vars.js

+98
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,104 @@ join_vars_assign: {
1616
expect_stdout: "PASS"
1717
}
1818

19+
join_array_assignments_1: {
20+
options = {
21+
evaluate: true,
22+
join_vars: true,
23+
}
24+
input: {
25+
console.log(function () {
26+
var a = ["foo", , "bar"];
27+
a[1] = "baz";
28+
a[7] = "moo";
29+
a[0] = "moz";
30+
return a;
31+
}().join());
32+
}
33+
expect: {
34+
console.log(function () {
35+
var a = ["moz", "baz", "bar", , , , , "moo"];
36+
return a;
37+
}().join());
38+
}
39+
expect_stdout: "moz,baz,bar,,,,,moo"
40+
}
41+
42+
join_array_assignments_2: {
43+
options = {
44+
evaluate: true,
45+
join_vars: true,
46+
}
47+
input: {
48+
console.log(function () {
49+
var a = ["foo"];
50+
a[1] = "bar";
51+
a[7] = "baz";
52+
a[2] = "moo";
53+
return a;
54+
}().join());
55+
}
56+
expect: {
57+
console.log(function () {
58+
var a = ["foo", "bar"];
59+
a[7] = "baz";
60+
a[2] = "moo";
61+
return a;
62+
}().join());
63+
}
64+
expect_stdout: "foo,bar,moo,,,,,baz"
65+
}
66+
67+
join_array_assignments_3: {
68+
options = {
69+
evaluate: true,
70+
join_vars: true,
71+
}
72+
input: {
73+
console.log(function () {
74+
var a = ["foo"];
75+
a[1] = "bar";
76+
a.b = "baz";
77+
a[2] = "moo";
78+
return a;
79+
}().join());
80+
}
81+
expect: {
82+
console.log(function () {
83+
var a = ["foo", "bar"];
84+
a.b = "baz";
85+
a[2] = "moo";
86+
return a;
87+
}().join());
88+
}
89+
expect_stdout: true
90+
}
91+
92+
join_array_assignments_4: {
93+
options = {
94+
evaluate: true,
95+
join_vars: true,
96+
}
97+
input: {
98+
console.log(function () {
99+
var a = ["foo"];
100+
a[0] = "bar";
101+
a[1] = a;
102+
a[2] = "baz";
103+
return a;
104+
}().join());
105+
}
106+
expect: {
107+
console.log(function () {
108+
var a = ["bar"];
109+
a[1] = a;
110+
a[2] = "baz";
111+
return a;
112+
}().join());
113+
}
114+
expect_stdout: true
115+
}
116+
19117
join_object_assignments_1: {
20118
options = {
21119
evaluate: true,

0 commit comments

Comments
 (0)
Please sign in to comment.