Skip to content

Commit

Permalink
Fix outputting a shorthand property in quotes when safari10 and ecma=…
Browse files Browse the repository at this point in the history
…2015 options are enabled. Closes #1005
  • Loading branch information
fabiosantoscode committed May 22, 2023
1 parent 6cdee01 commit 3eec4e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
59 changes: 33 additions & 26 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ function OutputStream(options) {
? output.option("ie8")
: !is_identifier_string(
prop,
output.option("ecma") >= 2015 || output.option("safari10")
output.option("ecma") >= 2015 && !output.option("safari10")
);

if (self.optional) output.print("?.");
Expand Down Expand Up @@ -2085,15 +2085,19 @@ function OutputStream(options) {
output.print("new.target");
});

/** Prints a prop name. Returns whether it can be used as a shorthand. */
function print_property_name(key, quote, output) {
if (output.option("quote_keys")) {
return output.print_string(key);
output.print_string(key);
return false;
}
if ("" + +key == key && key >= 0) {
if (output.option("keep_numbers")) {
return output.print(key);
output.print(key);
return false;
}
return output.print(make_num(key));
output.print(make_num(key));
return false;
}
var print_string = ALL_RESERVED_WORDS.has(key)
? output.option("ie8")
Expand All @@ -2103,9 +2107,11 @@ function OutputStream(options) {
: !is_identifier_string(key, true)
);
if (print_string || (quote && output.option("keep_quoted_props"))) {
return output.print_string(key, quote);
output.print_string(key, quote);
return false;
}
return output.print_name(key);
output.print_name(key);
return true;
}

DEFPRINT(AST_ObjectKeyVal, function(self, output) {
Expand All @@ -2114,28 +2120,29 @@ function OutputStream(options) {
return def ? def.mangled_name || def.name : self.name;
}

var allowShortHand = output.option("shorthand");
if (allowShortHand &&
self.value instanceof AST_Symbol &&
is_identifier_string(
self.key,
output.option("ecma") >= 2015 || output.option("safari10")
) &&
get_name(self.value) === self.key &&
!ALL_RESERVED_WORDS.has(self.key)
const try_shorthand = output.option("shorthand") && !(self.key instanceof AST_Node);
if (
try_shorthand
&& self.value instanceof AST_Symbol
&& get_name(self.value) === self.key
&& !ALL_RESERVED_WORDS.has(self.key)
) {
print_property_name(self.key, self.quote, output);

} else if (allowShortHand &&
self.value instanceof AST_DefaultAssign &&
self.value.left instanceof AST_Symbol &&
is_identifier_string(
self.key,
output.option("ecma") >= 2015 || output.option("safari10")
) &&
get_name(self.value.left) === self.key
const was_shorthand = print_property_name(self.key, self.quote, output);
if (!was_shorthand) {
output.colon();
self.value.print(output);
}
} else if (
try_shorthand
&& self.value instanceof AST_DefaultAssign
&& self.value.left instanceof AST_Symbol
&& get_name(self.value.left) === self.key
) {
print_property_name(self.key, self.quote, output);
const was_shorthand = print_property_name(self.key, self.quote, output);
if (!was_shorthand) {
output.colon();
self.value.left.print(output);
}
output.space();
output.print("=");
output.space();
Expand Down
13 changes: 13 additions & 0 deletions test/compress/harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,19 @@ shorthand_keywords: {
expect_stdout: true
}

shorthand_safari: {
beautify = {
ecma: 2015,
safari10: true,
}
input: {
var ä = "PASS";
console.log({ ä });
}
expect_exact: 'var ä="PASS";console.log({"ä":ä});'
expect_stdout: true
}

array_literal_with_spread_1: {
options = {
properties: true,
Expand Down

0 comments on commit 3eec4e0

Please sign in to comment.