Skip to content

Commit

Permalink
fix corner case in objects (#5214)
Browse files Browse the repository at this point in the history
fixes #5213
  • Loading branch information
alexlamsl committed Dec 9, 2021
1 parent 9e4c4c9 commit 57a9519
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
24 changes: 17 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -12560,7 +12560,8 @@ Compressor.prototype.compress = function(node) {
var found = false;
var generated = false;
var keep_duplicate = compressor.has_directive("use strict");
var keys = new Dictionary();
var keys = [];
var map = new Dictionary();
var values = [];
self.properties.forEach(function(prop) {
if (!(prop instanceof AST_Spread)) return process(prop);
Expand Down Expand Up @@ -12603,19 +12604,27 @@ Compressor.prototype.compress = function(node) {
return make_node(AST_Object, self, { properties: values });

function flush() {
keys.each(function(props) {
if (props.length == 1) return values.push(props[0]);
keys.forEach(function(key) {
var props = map.get(key);
switch (props.length) {
case 0:
return;
case 1:
return values.push(props[0]);
}
changed = true;
var tail = keep_duplicate && !generated && props.pop();
values.push(props.length == 1 ? props[0] : make_node(AST_ObjectKeyVal, self, {
key: props[0].key,
value: make_sequence(self, props.map(function(prop) {
return prop.value;
}))
})),
}));
if (tail) values.push(tail);
props.length = 0;
});
keys = new Dictionary();
keys = [];
map = new Dictionary();
}

function process(prop) {
Expand All @@ -12631,14 +12640,15 @@ Compressor.prototype.compress = function(node) {
}
if (can_hoist_property(prop)) {
if (prop.value.has_side_effects(compressor)) flush();
keys.add(key, prop);
keys.push(key);
map.add(key, prop);
} else {
flush();
values.push(prop);
}
if (found && !generated && typeof key == "string" && RE_POSITIVE_INTEGER.test(key)) {
generated = true;
if (keys.has(key)) prop = keys.get(key)[0];
if (map.has(key)) prop = map.get(key)[0];
prop.key = make_node(AST_Number, prop, { value: +key });
}
}
Expand Down
26 changes: 24 additions & 2 deletions test/compress/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ numeric_literal: {
expect_exact: [
'var obj = {',
' 0: 0,',
' 37: 4,',
' 42: 3,',
' "-0": 1,',
' 42: 3,',
' 37: 4,',
' o: 5,',
' 1e42: 8,',
' b: 7',
Expand Down Expand Up @@ -521,3 +521,25 @@ issue_4415: {
expect_stdout: "PASS"
node_version: ">=4"
}

issue_5213: {
options = {
objects: true,
}
input: {
var a = "FAIL";
console.log({
p: a = "PASS",
0: a,
p: null,
}[0]);
}
expect: {
var a = "FAIL";
console.log({
p: (a = "PASS", null),
0: a,
}[0]);
}
expect_stdout: "PASS"
}
4 changes: 2 additions & 2 deletions test/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function run_code_vm(code, toplevel, timeout) {
var ctx = vm.createContext({ console: console });
// for Node.js v6
vm.runInContext(setup_code, ctx);
vm.runInContext(toplevel ? "(function(){" + code + "})();" : code, ctx, { timeout: timeout });
vm.runInContext(toplevel ? "(function(){\n" + code + "\n})();" : code, ctx, { timeout: timeout });
// for Node.js v4
return strip_color_codes(stdout.replace(/\b(Array \[|Object {)/g, function(match) {
return match.slice(-1);
Expand All @@ -266,7 +266,7 @@ function run_code_vm(code, toplevel, timeout) {

function run_code_exec(code, toplevel, timeout) {
if (toplevel) {
code = setup_code + "(function(){" + code + "})();";
code = setup_code + "(function(){\n" + code + "\n})();";
} else {
code = code.replace(/^((["'])[^"']*\2(;|$))?/, function(directive) {
return directive + setup_code;
Expand Down

0 comments on commit 57a9519

Please sign in to comment.