Skip to content

Commit

Permalink
enhance join_vars (#5824)
Browse files Browse the repository at this point in the history
closes #5790
  • Loading branch information
alexlamsl authored Jun 5, 2024
1 parent aa7c338 commit ffe0fe7
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 10 deletions.
52 changes: 42 additions & 10 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4222,16 +4222,48 @@ Compressor.prototype.compress = function(node) {
if (value.left instanceof AST_SymbolRef) names.set(value.left.name, true);
value = value.right;
}
if (!(value instanceof AST_Object)) return;
var trimmed = false;
do {
if (!try_join(exprs[0])) break;
exprs.shift();
trimmed = true;
} while (exprs.length);
return trimmed;
if (value instanceof AST_Array) {
var trimmed = false;
do {
if (!try_join_array(exprs[0])) break;
exprs.shift();
trimmed = true;
} while (exprs.length);
return trimmed;
} else if (value instanceof AST_Object) {
var trimmed = false;
do {
if (!try_join_object(exprs[0])) break;
exprs.shift();
trimmed = true;
} while (exprs.length);
return trimmed;
}

function try_join_array(node) {
if (!(node instanceof AST_Assign)) return;
if (node.operator != "=") return;
if (!(node.left instanceof AST_PropAccess)) return;
var sym = node.left.expression;
if (!(sym instanceof AST_SymbolRef)) return;
if (!names.has(sym.name)) return;
if (!node.right.is_constant_expression(scope)) return;
var prop = node.left.property;
if (prop instanceof AST_Node) {
if (try_join_array(prop)) prop = node.left.property = prop.right.clone();
prop = prop.evaluate(compressor);
}
if (prop instanceof AST_Node) return;
if (!RE_POSITIVE_INTEGER.test("" + prop)) return;
prop = +prop;
var len = value.elements.length;
if (prop > len + 4) return;
while (len < prop) value.elements[len++] = make_node(AST_Hole, value);
value.elements[prop] = node.right;
return true;
}

function try_join(node) {
function try_join_object(node) {
if (!(node instanceof AST_Assign)) return;
if (node.operator != "=") return;
if (!(node.left instanceof AST_PropAccess)) return;
Expand All @@ -4241,7 +4273,7 @@ Compressor.prototype.compress = function(node) {
if (!node.right.is_constant_expression(scope)) return;
var prop = node.left.property;
if (prop instanceof AST_Node) {
if (try_join(prop)) prop = node.left.property = prop.right.clone();
if (try_join_object(prop)) prop = node.left.property = prop.right.clone();
prop = prop.evaluate(compressor);
}
if (prop instanceof AST_Node) return;
Expand Down
98 changes: 98 additions & 0 deletions test/compress/join_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,104 @@ join_vars_assign: {
expect_stdout: "PASS"
}

join_array_assignments_1: {
options = {
evaluate: true,
join_vars: true,
}
input: {
console.log(function () {
var a = ["foo", , "bar"];
a[1] = "baz";
a[7] = "moo";
a[0] = "moz";
return a;
}().join());
}
expect: {
console.log(function () {
var a = ["moz", "baz", "bar", , , , , "moo"];
return a;
}().join());
}
expect_stdout: "moz,baz,bar,,,,,moo"
}

join_array_assignments_2: {
options = {
evaluate: true,
join_vars: true,
}
input: {
console.log(function () {
var a = ["foo"];
a[1] = "bar";
a[7] = "baz";
a[2] = "moo";
return a;
}().join());
}
expect: {
console.log(function () {
var a = ["foo", "bar"];
a[7] = "baz";
a[2] = "moo";
return a;
}().join());
}
expect_stdout: "foo,bar,moo,,,,,baz"
}

join_array_assignments_3: {
options = {
evaluate: true,
join_vars: true,
}
input: {
console.log(function () {
var a = ["foo"];
a[1] = "bar";
a.b = "baz";
a[2] = "moo";
return a;
}().join());
}
expect: {
console.log(function () {
var a = ["foo", "bar"];
a.b = "baz";
a[2] = "moo";
return a;
}().join());
}
expect_stdout: true
}

join_array_assignments_4: {
options = {
evaluate: true,
join_vars: true,
}
input: {
console.log(function () {
var a = ["foo"];
a[0] = "bar";
a[1] = a;
a[2] = "baz";
return a;
}().join());
}
expect: {
console.log(function () {
var a = ["bar"];
a[1] = a;
a[2] = "baz";
return a;
}().join());
}
expect_stdout: true
}

join_object_assignments_1: {
options = {
evaluate: true,
Expand Down

0 comments on commit ffe0fe7

Please sign in to comment.