Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
nodeunit: {
all: ["test/**/*.js"]
all: ["test/**/*.js", "!test/samples/*"]
},
watch: {
files: "<config:lint.files>",
tasks: "default"
},
jshint: {
all: ["grunt.js", "tasks/**/*.js", "test/**/*.js"],
all: ["grunt.js", "tasks/**/*.js", "test/**/*.js", "!test/samples/*"],
options: {
curly: true,
eqeqeq: true,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Defaults to `[ 'console', 'window.console' ]`. If you use a custom logger, like
`MyApp.logger.log(foo)`, you would set this option to `[MyApp.logger]`.
* `methods`: An array of method names to remove. Defaults to [all the methods](http://getfirebug.com/wiki/index.php/Console_API) in the Firebug console API. This option is useful if you want to strip out all `log` methods, but keep `warn` for example.
* `verbose`: Boolean value, whether to show count of logging statements removed for each file. Defaults to true. If false, a single summary line is logged to grunt instead.
* `forceProperLineEnd`: Boolean value, defaults to `false`. Set to true and it will add a semi-colon to the end of the line, if it is missing. This stops some cases from causing malformed JS. If you leave it as `false`, you will simply get a console warning, telling you where the missing semi-colon is located.

### Skipping Individual Statements

Expand Down
2 changes: 1 addition & 1 deletion tasks/grunt-remove-logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function(grunt) {
var statementCount = 0, fileCount = 0;

var process = function(srcFile) {
var result = task(grunt.file.read(srcFile), opts);
var result = task(grunt.file.read(srcFile), opts, srcFile);
statementCount += result.count;
fileCount++;
if (opts.verbose) {
Expand Down
54 changes: 53 additions & 1 deletion tasks/lib/removelogging.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exports.init = function(grunt) {

var _ = grunt.util._;

return function(src, opts) {
return function(src, opts, origin) {
var counter = 0;
var rConsole;

Expand All @@ -21,6 +21,58 @@ exports.init = function(grunt) {
opts.verbose = true;
}

/**
* This condition will check all console statement lines to see if they end with a semi-colon and throw warning if
* they don't, with a message pointing to the location of the problem.
* By default it doesn't fix the problem for you, just points you in the direction of the problem so you can fix your code.
* If option 'forceProperLineEnd' is set to true though, it will add the semi-colon to the end of the line.
* Addresses issue 18: https://github.com/ehynds/grunt-remove-logging/issues/18
*/
if( _.isArray(opts.methods) ) {
opts.methods.forEach(function(meth) {

var splitter = "console."+meth,
newSrc = "";
src.split( splitter ).forEach(function(str, i) {

newSrc += (i>0 ? splitter : "");

var modStr = null;

if( i>0 && str.indexOf("\n") !== -1 ) {
var thisLine = str.split("\n")[0].trim(),
lastChar = thisLine[ thisLine.length-1 ];

if( thisLine.indexOf("RemoveLogging:skip") === -1 && lastChar !== ";" && thisLine.indexOf("function(") === -1 ) {
if( opts.forceProperLineEnd ) {

modStr = str.replace( "\n", ";\n" );

grunt.log.warn( "WARNING: Added semi-colon to line ".yellow +
"\nconsole.".cyan + meth.cyan + thisLine.cyan +
"\nIn file ".yellow + origin.yellow +
"\n\n" );
} else {
grunt.log.warn( "WARNING: line with console statement does not finish with ';'. ".red+
"This will likely cause unexpected results in 'grunt-remove-logging'.".red+
"\nIn file ".yellow + origin.yellow + ", search for the following string to debug it: ".yellow +
"\nconsole.".cyan + meth.cyan + thisLine.cyan+
"\n\n" );
}
}
}

newSrc += modStr || str;

});

if( newSrc !== "" ) {
src = newSrc;
}

});
}

rConsole = new RegExp("(" + opts.namespace.join("|") + ")" + ".(?:" + opts.methods.join("|") + ")\\s{0,}\\([^;]*\\)(?!\\s*[;,]?\\s*\\/\\*\\s*RemoveLogging:skip\\s*\\*\\/)\\s{0,};?", "gi");

src = src.replace(rConsole, function() {
Expand Down
5 changes: 5 additions & 0 deletions test/samples/sample1-after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function() {
var a = 2;

var b = 8;
}()
6 changes: 6 additions & 0 deletions test/samples/sample1-before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function() {
var a = 2;

console.log( a )
var b = 8;
}()
6 changes: 6 additions & 0 deletions test/samples/sample2-after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function() {
var a = 2;

//edw
var b = 8;
}()
6 changes: 6 additions & 0 deletions test/samples/sample2-before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(function() {
var a = 2;

console.log( a )//edw
var b = 8;
}()
42 changes: 40 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,44 @@ var tests = [
'pre;console.log("foo") ;post;',
{ replaceWith: "" },
"pre;post;"
],

// Issue #18 - no ';' at line end breaking code
[
'var xxxx;console.log()\n',
{ methods: [ 'log' ], forceProperLineEnd: true },
"var xxxx;\n"
],
[ // if the keyword "function(" is found on the same line, it will ignore it
'var xxxx;console.log = function()\n',
{ methods: [ 'log' ], forceProperLineEnd: true },
"var xxxx;console.log = function()\n"
],
[
'var xxxx;console.log();\n',
{ methods: [ 'log' ] },
"var xxxx;\n"
],
// testing actual JS files, so that any discrepencies between OSs and line breaks can be captured
[
grunt.file.read("./test/samples/sample1-before.js"),
{ methods: [ 'log' ] },
grunt.file.read("./test/samples/sample1-after.js")
],
[
grunt.file.read("./test/samples/sample2-before.js"),
{ methods: [ 'log' ] },
grunt.file.read("./test/samples/sample2-after.js")
],
[
'var xxxx;console.warn()\n',
{ methods: [ 'warn' ], forceProperLineEnd: true },
"var xxxx;\n"
],
[
'var xxxx;console.warn();\n',
{ methods: [ 'warn' ] },
"var xxxx;\n"
]
];

Expand All @@ -216,8 +254,8 @@ exports.tests = {
remove_logging: function(test) {
test.expect(tests.length);

tests.forEach(function(t) {
var result = task(t[0], t[1]);
tests.forEach(function(t, i) {
var result = task(t[0], t[1], "test"+i);
test.equal(result.src, t[2]);
});

Expand Down