Skip to content

Commit 80ebacb

Browse files
authoredAug 7, 2024··
generate shorter unicode escape sequence (#5914)
- fix input option mutation
1 parent 69f4e3a commit 80ebacb

File tree

7 files changed

+143
-11
lines changed

7 files changed

+143
-11
lines changed
 

‎bin/uglifyjs

+6-3
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,14 @@ function run() {
467467
} else if (output) {
468468
var code;
469469
if (result.ast) {
470-
var opts = {};
470+
var output_options = {};
471+
for (var name in UglifyJS.default_options("output")) {
472+
if (name in options) output_options[name] = options[name];
473+
}
471474
for (var name in options.output) {
472-
if (!/^ast|code$/.test(name)) opts[name] = options.output[name];
475+
if (!/^ast|code$/.test(name)) output_options[name] = options.output[name];
473476
}
474-
code = UglifyJS.AST_Node.from_mozilla_ast(result.ast.to_mozilla_ast()).print_to_string(opts);
477+
code = UglifyJS.AST_Node.from_mozilla_ast(result.ast.to_mozilla_ast()).print_to_string(output_options);
475478
} else {
476479
code = result.code;
477480
}

‎lib/minify.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ function parse_source_map(content) {
4949
function set_shorthand(name, options, keys) {
5050
keys.forEach(function(key) {
5151
if (options[key]) {
52-
if (typeof options[key] != "object") options[key] = {};
53-
if (!(name in options[key])) options[key][name] = options[name];
52+
var defs = {};
53+
defs[name] = options[name];
54+
options[key] = defaults(options[key], defs);
5455
}
5556
});
5657
}
@@ -105,7 +106,7 @@ function minify(files, options) {
105106
if (options.keep_fargs) set_shorthand("keep_fargs", options, [ "compress", "mangle", "rename" ]);
106107
if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle", "rename" ]);
107108
if (options.module === undefined && !options.ie) options.module = true;
108-
if (options.module) set_shorthand("module", options, [ "compress", "parse" ]);
109+
if (options.module) set_shorthand("module", options, [ "compress", "output", "parse" ]);
109110
if (options.toplevel !== undefined) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]);
110111
if (options.v8) set_shorthand("v8", options, [ "mangle", "output", "rename" ]);
111112
if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]);

‎lib/output.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function OutputStream(options) {
6363
inline_script : true,
6464
keep_quoted_props: false,
6565
max_line_len : false,
66+
module : false,
6667
preamble : null,
6768
preserve_line : false,
6869
quote_keys : false,
@@ -140,12 +141,22 @@ function OutputStream(options) {
140141

141142
reset();
142143
var to_utf8 = options.ascii_only ? function(str, identifier) {
143-
if (identifier) str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) {
144+
if (identifier || options.module) str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) {
144145
return "\\u{" + (ch.charCodeAt(0) - 0xd7c0 << 10 | ch.charCodeAt(1) - 0xdc00).toString(16) + "}";
145146
});
146-
return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
147-
var code = ch.charCodeAt(0).toString(16);
147+
return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(s, i) {
148+
var code = s.charCodeAt(0).toString(16);
148149
if (code.length <= 2 && !identifier) {
150+
switch (s) {
151+
case "\n": return "\\n";
152+
case "\r": return "\\r";
153+
case "\t": return "\\t";
154+
case "\b": return "\\b";
155+
case "\f": return "\\f";
156+
case "\x0B": return options.ie ? "\\x0B" : "\\v";
157+
case "\0":
158+
return /[0-9]/.test(str.charAt(i+1)) ? "\\x00" : "\\0";
159+
}
149160
while (code.length < 2) code = "0" + code;
150161
return "\\x" + code;
151162
} else {

‎test/compress/templates.js

+72
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ pure_funcs: {
432432
ascii_only: {
433433
beautify = {
434434
ascii_only: true,
435+
module: false,
435436
}
436437
options = {
437438
templates: false,
@@ -445,9 +446,27 @@ ascii_only: {
445446
node_version: ">=6"
446447
}
447448

449+
ascii_only_ecma: {
450+
beautify = {
451+
ascii_only: true,
452+
module: true,
453+
}
454+
options = {
455+
templates: false,
456+
}
457+
input: {
458+
console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`);
459+
}
460+
expect_exact: "console.log(`\\ud801\\udc37\\ud801\\u{10437}${42}\\u{10437}`);"
461+
expect_stdout: "𐐷\ud801𐐷42𐐷"
462+
// non-BMP support is platform-dependent on Node.js v4
463+
node_version: ">=6"
464+
}
465+
448466
ascii_only_templates: {
449467
beautify = {
450468
ascii_only: true,
469+
module: false,
451470
}
452471
options = {
453472
templates: true,
@@ -461,9 +480,44 @@ ascii_only_templates: {
461480
node_version: ">=6"
462481
}
463482

483+
ascii_only_templates_ecma: {
484+
beautify = {
485+
ascii_only: true,
486+
module: true,
487+
}
488+
options = {
489+
templates: true,
490+
}
491+
input: {
492+
console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`);
493+
}
494+
expect_exact: "console.log(`\\u{10437}\\ud801\\u{10437}${42}\\u{10437}`);"
495+
expect_stdout: "𐐷\ud801𐐷42𐐷"
496+
// non-BMP support is platform-dependent on Node.js v4
497+
node_version: ">=6"
498+
}
499+
464500
unicode: {
465501
beautify = {
466502
ascii_only: false,
503+
module: false,
504+
}
505+
options = {
506+
templates: false,
507+
}
508+
input: {
509+
console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`);
510+
}
511+
expect_exact: "console.log(`\\ud801\\udc37\\ud801𐐷${42}\\u{10437}`);"
512+
expect_stdout: "𐐷\ud801𐐷42𐐷"
513+
// non-BMP support is platform-dependent on Node.js v4
514+
node_version: ">=6"
515+
}
516+
517+
unicode_ecma: {
518+
beautify = {
519+
ascii_only: false,
520+
module: true,
467521
}
468522
options = {
469523
templates: false,
@@ -480,6 +534,24 @@ unicode: {
480534
unicode_templates: {
481535
beautify = {
482536
ascii_only: false,
537+
module: false,
538+
}
539+
options = {
540+
templates: true,
541+
}
542+
input: {
543+
console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`);
544+
}
545+
expect_exact: "console.log(`𐐷\\ud801𐐷${42}𐐷`);"
546+
expect_stdout: "𐐷\ud801𐐷42𐐷"
547+
// non-BMP support is platform-dependent on Node.js v4
548+
node_version: ">=6"
549+
}
550+
551+
unicode_templates_ecma: {
552+
beautify = {
553+
ascii_only: false,
554+
module: true,
483555
}
484556
options = {
485557
templates: true,

‎test/compress/unicode.js

+37
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ issue_2569: {
191191
surrogate_pair: {
192192
beautify = {
193193
ascii_only: false,
194+
module: false,
195+
}
196+
input: {
197+
var \u{2f800} = {
198+
\u{2f801}: "\u{100000}",
199+
};
200+
\u{2f800}.\u{2f802} = "\u{100001}";
201+
console.log(typeof \u{2f800}, \u{2f800}.\u{2f801}, \u{2f800}["\u{2f802}"]);
202+
}
203+
expect_exact: 'var \ud87e\udc00={"\ud87e\udc01":"\udbc0\udc00"};\ud87e\udc00.\ud87e\udc02="\udbc0\udc01";console.log(typeof \ud87e\udc00,\ud87e\udc00.\ud87e\udc01,\ud87e\udc00["\ud87e\udc02"]);'
204+
expect_stdout: "object \udbc0\udc00 \udbc0\udc01"
205+
// non-BMP support is platform-dependent on Node.js v4
206+
node_version: ">=6"
207+
}
208+
surrogate_pair_ecma: {
209+
beautify = {
210+
ascii_only: false,
211+
module: true,
194212
}
195213
input: {
196214
var \u{2f800} = {
@@ -208,6 +226,7 @@ surrogate_pair: {
208226
surrogate_pair_ascii: {
209227
beautify = {
210228
ascii_only: true,
229+
module: false,
211230
}
212231
input: {
213232
var \u{2f800} = {
@@ -221,3 +240,21 @@ surrogate_pair_ascii: {
221240
// non-BMP support is platform-dependent on Node.js v4
222241
node_version: ">=6"
223242
}
243+
244+
surrogate_pair_ascii_ecma: {
245+
beautify = {
246+
ascii_only: true,
247+
module: true,
248+
}
249+
input: {
250+
var \u{2f800} = {
251+
\u{2f801}: "\u{100000}",
252+
};
253+
\u{2f800}.\u{2f802} = "\u{100001}";
254+
console.log(typeof \u{2f800}, \u{2f800}.\u{2f801}, \u{2f800}["\u{2f802}"]);
255+
}
256+
expect_exact: 'var \\u{2f800}={"\\u{2f801}":"\\u{100000}"};\\u{2f800}.\\u{2f802}="\\u{100001}";console.log(typeof \\u{2f800},\\u{2f800}.\\u{2f801},\\u{2f800}["\\u{2f802}"]);'
257+
expect_stdout: "object \udbc0\udc00 \udbc0\udc01"
258+
// non-BMP support is platform-dependent on Node.js v4
259+
node_version: ">=6"
260+
}

‎test/mocha/minify.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ describe("minify", function() {
1919
var options = {
2020
compress: true,
2121
mangle: false,
22-
output: {},
22+
output: {
23+
v8: false,
24+
},
25+
webkit: true,
2326
};
2427
var value = JSON.stringify(options);
2528
var result = UglifyJS.minify("print(6 * 7);", options);

‎tools/node.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ function infer_options(options) {
9797
return result.error && result.error.defs;
9898
}
9999

100-
exports.default_options = function() {
100+
exports.default_options = function(component) {
101+
if (component) {
102+
var options = { module: false };
103+
options[component] = { 0: 0 };
104+
return infer_options(options);
105+
}
101106
var defs = infer_options({ 0: 0 });
102107
Object.keys(defs).forEach(function(component) {
103108
var options = { module: false };

0 commit comments

Comments
 (0)
Please sign in to comment.