Skip to content

Commit d51b6b9

Browse files
ensure properties are quoted when the safari10 output option is enabled
1 parent 7937b3d commit d51b6b9

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

lib/output.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ function OutputStream(options) {
239239
let printed_comments = new Set();
240240

241241
var to_utf8 = options.ascii_only ? function(str, identifier) {
242-
if (options.ecma >= 2015) {
242+
if (options.ecma >= 2015 && !options.safari10) {
243243
str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) {
244244
var code = get_full_char_code(ch, 0).toString(16);
245245
return "\\u{" + code + "}";
@@ -1779,7 +1779,10 @@ function OutputStream(options) {
17791779
var prop = self.property;
17801780
var print_computed = RESERVED_WORDS.has(prop)
17811781
? output.option("ie8")
1782-
: !is_identifier_string(prop, output.option("ecma") >= 2015);
1782+
: !is_identifier_string(
1783+
prop,
1784+
output.option("ecma") >= 2015 || output.option("safari10")
1785+
);
17831786

17841787
if (self.optional) output.print("?.");
17851788

@@ -1949,7 +1952,7 @@ function OutputStream(options) {
19491952
var print_string = RESERVED_WORDS.has(key)
19501953
? output.option("ie8")
19511954
: (
1952-
output.option("ecma") < 2015
1955+
output.option("ecma") < 2015 || output.option("safari10")
19531956
? !is_basic_identifier_string(key)
19541957
: !is_identifier_string(key, true)
19551958
);
@@ -1968,7 +1971,10 @@ function OutputStream(options) {
19681971
var allowShortHand = output.option("shorthand");
19691972
if (allowShortHand &&
19701973
self.value instanceof AST_Symbol &&
1971-
is_identifier_string(self.key, output.option("ecma") >= 2015) &&
1974+
is_identifier_string(
1975+
self.key,
1976+
output.option("ecma") >= 2015 || output.option("safari10")
1977+
) &&
19721978
get_name(self.value) === self.key &&
19731979
!RESERVED_WORDS.has(self.key)
19741980
) {
@@ -1977,7 +1983,10 @@ function OutputStream(options) {
19771983
} else if (allowShortHand &&
19781984
self.value instanceof AST_DefaultAssign &&
19791985
self.value.left instanceof AST_Symbol &&
1980-
is_identifier_string(self.key, output.option("ecma") >= 2015) &&
1986+
is_identifier_string(
1987+
self.key,
1988+
output.option("ecma") >= 2015 || output.option("safari10")
1989+
) &&
19811990
get_name(self.value.left) === self.key
19821991
) {
19831992
print_property_name(self.key, self.quote, output);

lib/parse.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,14 @@ function is_identifier_char(ch) {
315315
return UNICODE.ID_Continue.test(ch);
316316
}
317317

318+
const BASIC_IDENT = /^[a-z_$][a-z0-9_$]*$/i;
319+
318320
function is_basic_identifier_string(str) {
319-
return /^[a-z_$][a-z0-9_$]*$/i.test(str);
321+
return BASIC_IDENT.test(str);
320322
}
321323

322324
function is_identifier_string(str, allow_surrogates) {
323-
if (/^[a-z_$][a-z0-9_$]*$/i.test(str)) {
325+
if (BASIC_IDENT.test(str)) {
324326
return true;
325327
}
326328
if (!allow_surrogates && /[\ud800-\udfff]/.test(str)) {

test/compress/unicode.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ keep_quoted_unicode_props_es5: {
2424
expect_exact: 'console.log({"Ꞡ":"2139"});'
2525
}
2626

27+
keep_quoted_unicode_props_safari: {
28+
beautify = { safari10: true, ecma: 2020 }
29+
input: {
30+
console.log({ "\uA7A0": "2139" });
31+
}
32+
expect_exact: 'console.log({"Ꞡ":"2139"});'
33+
}
34+
35+
unicode_props_safari: {
36+
beautify = { safari10: true, ecma: 2020 }
37+
input: {
38+
console.log({ 𝒶: "foo" })
39+
}
40+
expect_exact: 'console.log({"𝒶":"foo"});'
41+
}
42+
2743
unicode_escaped_identifier_2015: {
2844
beautify = {ecma: 2015}
2945
input: {
@@ -33,6 +49,14 @@ unicode_escaped_identifier_2015: {
3349
expect_exact: 'var a="foo";var \u{10000}="bar";';
3450
}
3551

52+
unicode_escaped_identifier_safari: {
53+
beautify = {ecma: 2020, safari10: true}
54+
input: {
55+
var \u{61} = "foo";
56+
}
57+
expect_exact: 'var a="foo";';
58+
}
59+
3660
unicode_escaped_identifier_es5_as_is: {
3761
beautify = {ecma: 5}
3862
input: `

0 commit comments

Comments
 (0)