diff --git a/lib/compress.js b/lib/compress.js index bcfee5a4..c421525e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); } diff --git a/lib/propmangle.js b/lib/propmangle.js index cf5e3781..f034a091 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -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); } @@ -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) { diff --git a/test/compress/classes.js b/test/compress/classes.js index 39e26631..32c4d74e 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -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" } diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index d34b2bd9..1f4d2c92 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -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: { @@ -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: { @@ -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: { @@ -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: { @@ -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: { @@ -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: { diff --git a/test/compress/hoist_props.js b/test/compress/hoist_props.js index b3c93f7d..35d9127b 100644 --- a/test/compress/hoist_props.js +++ b/test/compress/hoist_props.js @@ -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: { @@ -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: { @@ -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: { @@ -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: { diff --git a/test/compress/issue-1770.js b/test/compress/issue-1770.js index 3b2f8cba..ad8b22a2 100644 --- a/test/compress/issue-1770.js +++ b/test/compress/issue-1770.js @@ -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: { @@ -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: { diff --git a/test/compress/issue-747.js b/test/compress/issue-747.js index e074305a..8d5289bb 100644 --- a/test/compress/issue-747.js +++ b/test/compress/issue-747.js @@ -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: { @@ -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", + ] }