Skip to content

Commit 4653e8a

Browse files
authored
improve diagnostics on top_retain & mangle.properties (#5622)
closes #5618
1 parent ac002b6 commit 4653e8a

File tree

7 files changed

+100
-8
lines changed

7 files changed

+100
-8
lines changed

Diff for: lib/compress.js

+1
Original file line numberDiff line numberDiff line change
@@ -6841,6 +6841,7 @@ Compressor.prototype.compress = function(node) {
68416841
if (self instanceof AST_Toplevel && compressor.top_retain) {
68426842
self.variables.each(function(def) {
68436843
if (compressor.top_retain(def) && !(def.id in in_use_ids)) {
6844+
AST_Node.info("Retaining variable {name}", def);
68446845
in_use_ids[def.id] = true;
68456846
in_use.push(def);
68466847
}

Diff for: lib/propmangle.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,14 @@ function mangle_properties(ast, options) {
260260
}
261261

262262
function should_mangle(name) {
263-
if (reserved.has(name)) return false;
264-
if (regex && !regex.test(name)) return false;
263+
if (reserved.has(name)) {
264+
AST_Node.info("Preserving reserved property {this}", name);
265+
return false;
266+
}
267+
if (regex && !regex.test(name)) {
268+
AST_Node.info("Preserving excluded property {this}", name);
269+
return false;
270+
}
265271
return cache.has(name) || names_to_mangle.has(name);
266272
}
267273

@@ -271,10 +277,7 @@ function mangle_properties(ast, options) {
271277
}
272278

273279
function mangle(name) {
274-
if (!should_mangle(name)) {
275-
AST_Node.info("Preserving property {this}", name);
276-
return name;
277-
}
280+
if (!should_mangle(name)) return name;
278281
var mangled = cache.get(name);
279282
if (!mangled) {
280283
if (debug) {

Diff for: test/compress/classes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2201,12 +2201,12 @@ mangle_properties: {
22012201
}
22022202
expect_stdout: "PASS 42"
22032203
expect_warnings: [
2204+
"INFO: Preserving reserved property q",
2205+
"INFO: Preserving reserved property log",
22042206
"INFO: Mapping property #P to #t",
22052207
"INFO: Mapping property Q to s",
22062208
"INFO: Mapping property #p to #i",
22072209
"INFO: Mapping property r to e",
2208-
"INFO: Preserving property q",
2209-
"INFO: Preserving property log",
22102210
]
22112211
node_version: ">=14.6"
22122212
}

Diff for: test/compress/drop-unused.js

+49
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,15 @@ drop_toplevel_retain: {
409409
a = 2;
410410
console.log(3);
411411
}
412+
expect_stdout: "3"
413+
expect_warnings: [
414+
"INFO: Retaining variable a",
415+
"INFO: Retaining variable f",
416+
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
417+
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
418+
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
419+
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
420+
]
412421
}
413422

414423
drop_toplevel_retain_array: {
@@ -442,6 +451,15 @@ drop_toplevel_retain_array: {
442451
a = 2;
443452
console.log(3);
444453
}
454+
expect_stdout: "3"
455+
expect_warnings: [
456+
"INFO: Retaining variable a",
457+
"INFO: Retaining variable f",
458+
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
459+
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
460+
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
461+
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
462+
]
445463
}
446464

447465
drop_toplevel_retain_regex: {
@@ -471,6 +489,15 @@ drop_toplevel_retain_regex: {
471489
a = 2;
472490
console.log(3);
473491
}
492+
expect_stdout: "3"
493+
expect_warnings: [
494+
"INFO: Retaining variable a",
495+
"INFO: Retaining variable f",
496+
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
497+
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
498+
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
499+
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
500+
]
474501
}
475502

476503
drop_toplevel_all_retain: {
@@ -501,6 +528,15 @@ drop_toplevel_all_retain: {
501528
a = 2;
502529
console.log(3);
503530
}
531+
expect_stdout: "3"
532+
expect_warnings: [
533+
"INFO: Retaining variable a",
534+
"INFO: Retaining variable f",
535+
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
536+
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
537+
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
538+
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
539+
]
504540
}
505541

506542
drop_toplevel_funcs_retain: {
@@ -532,6 +568,12 @@ drop_toplevel_funcs_retain: {
532568
function g() {}
533569
console.log(b = 3);
534570
}
571+
expect_stdout: "3"
572+
expect_warnings: [
573+
"INFO: Retaining variable a",
574+
"INFO: Retaining variable f",
575+
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
576+
]
535577
}
536578

537579
drop_toplevel_vars_retain: {
@@ -564,6 +606,13 @@ drop_toplevel_vars_retain: {
564606
function h() {}
565607
console.log(3);
566608
}
609+
expect_stdout: "3"
610+
expect_warnings: [
611+
"INFO: Retaining variable a",
612+
"INFO: Retaining variable f",
613+
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
614+
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
615+
]
567616
}
568617

569618
drop_toplevel_keep_assign: {

Diff for: test/compress/hoist_props.js

+16
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@ issue_2473_1: {
462462
var x = {};
463463
var y = [];
464464
}
465+
expect_warnings: [
466+
"INFO: Retaining variable x",
467+
"INFO: Retaining variable y",
468+
"WARN: Dropping unused variable z [test/compress/hoist_props.js:3,12]",
469+
]
465470
}
466471

467472
issue_2473_2: {
@@ -484,6 +489,11 @@ issue_2473_2: {
484489
var x = {};
485490
var y = [];
486491
}
492+
expect_warnings: [
493+
"INFO: Retaining variable x",
494+
"INFO: Retaining variable y",
495+
"WARN: Dropping unused variable z [test/compress/hoist_props.js:3,12]",
496+
]
487497
}
488498

489499
issue_2473_3: {
@@ -509,6 +519,9 @@ issue_2473_3: {
509519
console.log(o.a, o.b);
510520
}
511521
expect_stdout: "1 2"
522+
expect_warnings: [
523+
"INFO: Retaining variable o",
524+
]
512525
}
513526

514527
issue_2473_4: {
@@ -535,6 +548,9 @@ issue_2473_4: {
535548
})();
536549
}
537550
expect_stdout: "1 2"
551+
expect_warnings: [
552+
"INFO: Dropping unused variable o [test/compress/hoist_props.js:2,16]",
553+
]
538554
}
539555

540556
issue_2508_1: {

Diff for: test/compress/issue-1770.js

+13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ mangle_props: {
5353
);
5454
}
5555
expect_stdout: "1 1 1 2 2 2 3 3 3 4 4 4 5 5"
56+
expect_warnings: [
57+
"INFO: Preserving reserved property undefined",
58+
"INFO: Preserving reserved property NaN",
59+
"INFO: Preserving reserved property Infinity",
60+
"INFO: Preserving reserved property -Infinity",
61+
"INFO: Preserving reserved property null",
62+
"INFO: Preserving reserved property log",
63+
]
5664
}
5765

5866
numeric_literal: {
@@ -106,6 +114,11 @@ numeric_literal: {
106114
"4 5 4 4",
107115
"8 7 8",
108116
]
117+
expect_warnings: [
118+
"INFO: Preserving reserved property log",
119+
"INFO: Mapping property 0x25 to o",
120+
"INFO: Mapping property 1E42 to b",
121+
]
109122
}
110123

111124
identifier: {

Diff for: test/compress/issue-747.js

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ dont_reuse_prop: {
1919
console.log(obj.a);
2020
}
2121
expect_stdout: "123"
22+
expect_warnings: [
23+
"INFO: Preserving excluded property a",
24+
"INFO: Preserving reserved property log",
25+
"INFO: Mapping property asd to b",
26+
]
2227
}
2328

2429
unmangleable_props_should_always_be_reserved: {
@@ -42,4 +47,9 @@ unmangleable_props_should_always_be_reserved: {
4247
console.log(obj.a);
4348
}
4449
expect_stdout: "123"
50+
expect_warnings: [
51+
"INFO: Preserving excluded property a",
52+
"INFO: Preserving reserved property log",
53+
"INFO: Mapping property asd to b",
54+
]
4555
}

0 commit comments

Comments
 (0)