Hi there,
I'm using Perl regular expressions to try to find logging statements in my compressed JavaScript code and strip them out of my code. Here's what my regular expression looks like at the moment:
foreach my $line (@file_lines) {
# strip the line of console.log statements
$line =~ s/console\.[warn|log|error|con|info]+\((.*?)\)+;+/;/gi;
print DATAOUT "$line";
}
Basically, I want to strip all console.log|warn|etc statements from my code and just replace them with a semi-colon. Here's a sample of the text I need to strip:
console.warn("_onValidateFailure: ",_4);class.common.hide(this.loadingNode);class.common.enable(this.submitButton);this.createInFlight=false;topic.publish("/class/error",[{alignNode:this.,errorType:"_validate",title:this._prompts.unableVal,detail:_4.message}]);},_createAccount:function(){topic.publish("/class/error/close",["all"]);if(this._executeValidations()){app.apiService.required["Login"]=this.email1.value;var _5=app..getInsertedData();console.log("*** CREATING ACCOUNT WITH "+this.email1.value+" "+this.password1.value+" "+this.firstName.value+" "+this.lastName.value+" "+_5.id+" "+_5.RedKey);var _6=app.apiService.createAccount({Login:this.email1.value,Password:this.password1.value,FirstName:this.firstName.value,LastName:this.lastName.value,:_5.id,RegKey:_5.regKey,Add:1});_6.addCallback(this,"_onCreateAccount");_6.addErrback(this,"_onCreateAccountFailure");}},_onCreateAccount:function(_7){class.common.hide(this.loadingNode);this.createInFlight=false;topic.publish("/class/error/close",["all"]);topic.publish("/class/createAccount",[_7]);},_onCreateAccountFailure:function(_8){console.warn("_onCreateAccountFailure: ",_8);
This regular expression has been working for me save for two situations.
1. This following is valid BLOCKED SCRIPT
console.log("This is a ) logging statement;")
Note the lack of a semi-colon at the end of this statement and the fact that a right-parenthesis is a viable as part of a String parameter to the console.log statement. I've tried altering my regular expression to look like this:
foreach my $line (@file_lines) {
# strip the line of console.log statements
$line =~ s/console\.[warn|log|error|con|info]+\((.*?)\)+;?/;/gi;
print DATAOUT "$line";
}
but that doesnt result in a favorable solution as it leaves 'logging statement;")' right in the middle of my output.
2. The following is also valid BLOCKED SCRIPT
(1 == 0)?console.log("What?????"):console.log("OK");
However, I cant really strip these console statements as a tri-conditional statement in JavaScript has to have more substance than just (i == 0) ? ; : ;. So, for the most part, I just want to ignore these console.log statements which are trapped in tri-conditional blocks. I've tried altering my regular expression to look like this:
foreach my $line (@file_lines) {
# strip the line of console.log statements
$line =~ s/[^\?:]console\.[warn|log|error|con|info]+\((.*?)\)+;?/;/gi;
print DATAOUT "$line";
}
I'm attempting to ignore any console.log statements which are prefixed with '?' or ':', but I cant seem to get that syntax correct either.
Any help is greatly appreciated
Sean