ehynds / grunt-remove-logging

Grunt.js task for removing console logging statements
MIT License
192 stars 33 forks source link

should have the ability to remove all leading namespaces #7

Open timotheeg opened 11 years ago

timotheeg commented 11 years ago

[feature request]

Some js libraries access console via the window namespace (which sometimes get minified to a one letter variable by uglyfier tools) as below:

window.console.log("foo");
a.console.log("foo")

e.g. isotope.js: http://isotope.metafizzy.co/jquery.isotope.min.js

var x=function(b){a.console&&a.console.error(b)};

the resulting code with grunt-remove-logging with default options is:

var x=function(b){a.console&&a.};

which gives a syntax error.

I know "replaceWith" can be used to replace the deleted statement with something else, but that doesn't help in this case (from a general point of view). The option "namespace" is also not good enough since I might not know before hand what the actual namespace is for pre-minified imported libs. Therefore, an additional option could be to match all leading namespaces, such that the following options:

{matchAllNamespaces: true, replaceWith: '1'}

would turn the example above into:

var x=function(b){a.console&&1};

possible regexp:

"(?:[a-zA-Z$_][a-zA-Z0-9$_]*\\.)*" + opts.namespace + ".(?:" + opts.methods.join("|") + ")