Skip to content

Commit

Permalink
improve diagnostics on top_retain & mangle.properties (#5622)
Browse files Browse the repository at this point in the history
closes #5618
  • Loading branch information
alexlamsl authored Aug 18, 2022
1 parent ac002b6 commit 4653e8a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6841,6 +6841,7 @@ Compressor.prototype.compress = function(node) {
if (self instanceof AST_Toplevel && compressor.top_retain) {
self.variables.each(function(def) {
if (compressor.top_retain(def) && !(def.id in in_use_ids)) {
AST_Node.info("Retaining variable {name}", def);
in_use_ids[def.id] = true;
in_use.push(def);
}
Expand Down
15 changes: 9 additions & 6 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,14 @@ function mangle_properties(ast, options) {
}

function should_mangle(name) {
if (reserved.has(name)) return false;
if (regex && !regex.test(name)) return false;
if (reserved.has(name)) {
AST_Node.info("Preserving reserved property {this}", name);
return false;
}
if (regex && !regex.test(name)) {
AST_Node.info("Preserving excluded property {this}", name);
return false;
}
return cache.has(name) || names_to_mangle.has(name);
}

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

function mangle(name) {
if (!should_mangle(name)) {
AST_Node.info("Preserving property {this}", name);
return name;
}
if (!should_mangle(name)) return name;
var mangled = cache.get(name);
if (!mangled) {
if (debug) {
Expand Down
4 changes: 2 additions & 2 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2201,12 +2201,12 @@ mangle_properties: {
}
expect_stdout: "PASS 42"
expect_warnings: [
"INFO: Preserving reserved property q",
"INFO: Preserving reserved property log",
"INFO: Mapping property #P to #t",
"INFO: Mapping property Q to s",
"INFO: Mapping property #p to #i",
"INFO: Mapping property r to e",
"INFO: Preserving property q",
"INFO: Preserving property log",
]
node_version: ">=14.6"
}
Expand Down
49 changes: 49 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,15 @@ drop_toplevel_retain: {
a = 2;
console.log(3);
}
expect_stdout: "3"
expect_warnings: [
"INFO: Retaining variable a",
"INFO: Retaining variable f",
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
]
}

drop_toplevel_retain_array: {
Expand Down Expand Up @@ -442,6 +451,15 @@ drop_toplevel_retain_array: {
a = 2;
console.log(3);
}
expect_stdout: "3"
expect_warnings: [
"INFO: Retaining variable a",
"INFO: Retaining variable f",
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
]
}

drop_toplevel_retain_regex: {
Expand Down Expand Up @@ -471,6 +489,15 @@ drop_toplevel_retain_regex: {
a = 2;
console.log(3);
}
expect_stdout: "3"
expect_warnings: [
"INFO: Retaining variable a",
"INFO: Retaining variable f",
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
]
}

drop_toplevel_all_retain: {
Expand Down Expand Up @@ -501,6 +528,15 @@ drop_toplevel_all_retain: {
a = 2;
console.log(3);
}
expect_stdout: "3"
expect_warnings: [
"INFO: Retaining variable a",
"INFO: Retaining variable f",
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
"INFO: Dropping unused function g [test/compress/drop-unused.js:8,17]",
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
]
}

drop_toplevel_funcs_retain: {
Expand Down Expand Up @@ -532,6 +568,12 @@ drop_toplevel_funcs_retain: {
function g() {}
console.log(b = 3);
}
expect_stdout: "3"
expect_warnings: [
"INFO: Retaining variable a",
"INFO: Retaining variable f",
"WARN: Dropping unused function h [test/compress/drop-unused.js:9,17]",
]
}

drop_toplevel_vars_retain: {
Expand Down Expand Up @@ -564,6 +606,13 @@ drop_toplevel_vars_retain: {
function h() {}
console.log(3);
}
expect_stdout: "3"
expect_warnings: [
"INFO: Retaining variable a",
"INFO: Retaining variable f",
"INFO: Dropping unused variable b [test/compress/drop-unused.js:1,15]",
"INFO: Dropping unused variable c [test/compress/drop-unused.js:1,22]",
]
}

drop_toplevel_keep_assign: {
Expand Down
16 changes: 16 additions & 0 deletions test/compress/hoist_props.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ issue_2473_1: {
var x = {};
var y = [];
}
expect_warnings: [
"INFO: Retaining variable x",
"INFO: Retaining variable y",
"WARN: Dropping unused variable z [test/compress/hoist_props.js:3,12]",
]
}

issue_2473_2: {
Expand All @@ -484,6 +489,11 @@ issue_2473_2: {
var x = {};
var y = [];
}
expect_warnings: [
"INFO: Retaining variable x",
"INFO: Retaining variable y",
"WARN: Dropping unused variable z [test/compress/hoist_props.js:3,12]",
]
}

issue_2473_3: {
Expand All @@ -509,6 +519,9 @@ issue_2473_3: {
console.log(o.a, o.b);
}
expect_stdout: "1 2"
expect_warnings: [
"INFO: Retaining variable o",
]
}

issue_2473_4: {
Expand All @@ -535,6 +548,9 @@ issue_2473_4: {
})();
}
expect_stdout: "1 2"
expect_warnings: [
"INFO: Dropping unused variable o [test/compress/hoist_props.js:2,16]",
]
}

issue_2508_1: {
Expand Down
13 changes: 13 additions & 0 deletions test/compress/issue-1770.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ mangle_props: {
);
}
expect_stdout: "1 1 1 2 2 2 3 3 3 4 4 4 5 5"
expect_warnings: [
"INFO: Preserving reserved property undefined",
"INFO: Preserving reserved property NaN",
"INFO: Preserving reserved property Infinity",
"INFO: Preserving reserved property -Infinity",
"INFO: Preserving reserved property null",
"INFO: Preserving reserved property log",
]
}

numeric_literal: {
Expand Down Expand Up @@ -106,6 +114,11 @@ numeric_literal: {
"4 5 4 4",
"8 7 8",
]
expect_warnings: [
"INFO: Preserving reserved property log",
"INFO: Mapping property 0x25 to o",
"INFO: Mapping property 1E42 to b",
]
}

identifier: {
Expand Down
10 changes: 10 additions & 0 deletions test/compress/issue-747.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ dont_reuse_prop: {
console.log(obj.a);
}
expect_stdout: "123"
expect_warnings: [
"INFO: Preserving excluded property a",
"INFO: Preserving reserved property log",
"INFO: Mapping property asd to b",
]
}

unmangleable_props_should_always_be_reserved: {
Expand All @@ -42,4 +47,9 @@ unmangleable_props_should_always_be_reserved: {
console.log(obj.a);
}
expect_stdout: "123"
expect_warnings: [
"INFO: Preserving excluded property a",
"INFO: Preserving reserved property log",
"INFO: Mapping property asd to b",
]
}

0 comments on commit 4653e8a

Please sign in to comment.