Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix corner case in objects #5214

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -12552,7 +12552,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 @@ -12595,19 +12596,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 @@ -12623,14 +12632,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