Skip to content

[Feature Request] Verbose log mangled and skipped props due to regex mismatch #5618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mirfatif opened this issue Aug 16, 2022 · 6 comments · Fixed by #5621 or #5622
Closed

[Feature Request] Verbose log mangled and skipped props due to regex mismatch #5618

mirfatif opened this issue Aug 16, 2022 · 6 comments · Fixed by #5621 or #5622

Comments

@mirfatif
Copy link

Please consider if it makes sense. Excuse my ignorance if this can already be done in a better way.

~$ uglifyjs source.js -o uglified.js --verbose --warn -c -m --mangle-props regex=/_$/
...
INFO: Skipping expiry
INFO: Mangling connected_
INFO: Skipping track
INFO: Mangling afterHtmlSet_
INFO: Mangling rootElement_
INFO: Mangling url_
...
--- lib/propmangle.js
+++ lib/propmangle.js
@@ -175,6 +175,9 @@
         cache = new Dictionary();
     }
 
+    // --name-cache doesn't log skipped names
+    var verbose_cache = new Dictionary();
+
     var regex = options.regex;
 
     // note debug is either false (disabled), or a string of the debug suffix to use (enabled).
@@ -261,8 +264,15 @@
 
     function should_mangle(name) {
         if (reserved.has(name)) return false;
-        if (regex && !regex.test(name)) return false;
-        return cache.has(name) || names_to_mangle.has(name);
+        const logged = verbose_cache.has(name);
+        verbose_cache.set(name);
+        if (regex && !regex.test(name)) {
+            logged || AST_Node.info('Skipping ' + name);
+            return false;
+        }
+        const ret = cache.has(name) || names_to_mangle.has(name);
+        ret && !logged && AST_Node.info('Mangling ' + name);
+        return ret;
     }
 
     function add(name) {
@alexlamsl
Copy link
Collaborator

Please try #5621 and see if it works for you.

As for your proposed patch − for starters there's no need for verbose_cache since AST_Node.info() already has built-in de-duplication:

UglifyJS/lib/ast.js

Lines 188 to 206 in 8602d1b

(AST_Node.log_function = function(fn, verbose) {
if (typeof fn != "function") {
AST_Node.info = AST_Node.warn = noop;
return;
}
var printed = Object.create(null);
AST_Node.info = verbose ? function(text, props) {
log("INFO: " + string_template(text, props));
} : noop;
AST_Node.warn = function(text, props) {
log("WARN: " + string_template(text, props));
};
function log(msg) {
if (printed[msg]) return;
printed[msg] = true;
fn(msg);
}
})();

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Aug 17, 2022
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Aug 17, 2022
alexlamsl added a commit that referenced this issue Aug 17, 2022
@mirfatif
Copy link
Author

Thank you for looking into it.

Actually this gives excessive verbosity. I don't think one would be interested in mangling window.location:

INFO: Preserving property location

I was after getting a list of properties and variables which shows if regex was applied or not (with mangle: { properties: { regex: /_$/ } } and compress: { top_retain: /[^_]$/ }). This could help in fixing bad variable/prop names quickly.

But I think it's not possible at the moment.

@alexlamsl
Copy link
Collaborator

I don't think one would be interested in mangling window.location

window.location will be preserved if you specify domprops, since builtins refers to JavaScript runtime rather than DOM environment.

I was after getting a list of properties and variables which shows if regex was applied or not

Since mangle.properties.regex applies to all properties under consideration, you will be able to see what gets mangled/preserved to determine if you need to tweak your regex.

For the record, compress.top_retain has nothing to do with name mangling.

@mirfatif
Copy link
Author

window.location will be preserved if you specify domprops, since builtins refers to JavaScript runtime rather than DOM environment.

DOM props are preserved. My point is that they are unnecessarily logged:

INFO: Preserving property classList
INFO: Preserving property contains
INFO: Preserving property display
INFO: Preserving property fontWeight
INFO: Preserving property getElementsByClassName
INFO: Preserving property href
INFO: Preserving property location
INFO: Preserving property parentElement
INFO: Preserving property scrollIntoView
INFO: Preserving property style
INFO: Preserving property tagName

There's no way to prevent logging of preserved builtins and domprops, right?

For the record, compress.top_retain has nothing to do with name mangling.

Correct. I was referring to the regex part i.e. get a list of top-level variables which have or have not been gone through "unused removal" due to regex (mis)match.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Aug 18, 2022
@alexlamsl
Copy link
Collaborator

IMHO there are merits in reporting properties which are affected by builtins etc. − however we can improve readability by differentiating the messages between those and regex.

Please take a look at #5622 which also logs actions related to compress.top_retain

@alexlamsl
Copy link
Collaborator

Patches released in [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants