Skip to content

Commit

Permalink
Almost everything lints now. The only outstanding lint issues seem to…
Browse files Browse the repository at this point in the history
… be bugs in JSHint.
  • Loading branch information
cowboy committed Oct 6, 2011
1 parent 5d9fd1d commit a3dc4a5
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 38 deletions.
5 changes: 3 additions & 2 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ config.init({
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
eqnull: true,
node: true
Expand All @@ -52,7 +53,7 @@ config.init({
});

// Tasks.
task.registerTask('default', 'Run "test" task.', function() {
this.task('test');
task.registerTask('default', 'Run "lint" and "test" tasks.', function() {
this.task('lint').task('test');
});

6 changes: 6 additions & 0 deletions lib/grunt/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ exports.fatal = function(e) {
exit(1);
};

// The ABORT proptery attempts to work around the whole output-buffering
// process.exit() issue. which is a total mess.
exports.ABORT = null;

// Keep track of error and warning counts.
exports.errors = 0;
exports.warns = 0;

Expand All @@ -29,6 +34,7 @@ exports.warn = function(e) {
// If -f or --force aren't used, stop script processing.
if (!option('force')) {
log.indent(0).fail('Aborted due to warnings.');
exports.ABORT = true;
exit(1);
}
};
Expand Down
9 changes: 2 additions & 7 deletions lib/grunt/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@ col1len += 2;
// reached, avoiding splitting words whenever possible.
var re = new RegExp('.{1,' + (80 - col1len - 1) + '}(?=\\s|$)', 'g');

// Get a string `str` repeated `n` times.
function repeat(n, str) {
return Array(n + 1).join(str || ' ');
}

// Actually write output, padding and wrapping as necessary..
function writeln(arr) {
var col1 = arr[0];
var col2 = arr[1].replace(re, function(s) {
return '\n' + repeat(col1len + 1) + s.replace(/^\s+/, '');
return '\n' + log.pad(col1len + 1) + s.replace(/^\s+/, '');
}).replace(/^\s+/, '');
log.writeln(' ' + col1 + repeat(col1len - col1.length) + col2);
log.writeln(' ' + col1 + log.pad(col1len - col1.length) + col2);
}

log.writeln('grunt: a command line build tool for JavaScript projects.\n');
Expand Down
22 changes: 16 additions & 6 deletions lib/grunt/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ exports.unindent = function() {

// The actual indent string, based on the indentCount.
function indentString() {
return suppressIndent ? '' : Array(indentCount + 1).join(' ');
return suppressIndent ? '' : exports.pad(indentCount);
}

// Write output.
exports.write = function(msg) {
// Actually write output.
if (!suppressOutput) {
process.stdout.write(indentString() + msg);
process.stdout.write(indentString() + (msg || ''));
}
// Indentaton is suppressed.
// Indentation is suppressed.
suppressIndent = true;
// Chainable!
return this;
Expand All @@ -59,7 +59,7 @@ exports.write = function(msg) {
// Write a line of output.
exports.writeln = function(msg) {
// Actually write output.
this.write(msg + '\n');
this.write((msg || '') + '\n');
// A newline was written, indentaton is no longersuppressed.
suppressIndent = false;
// Chainable!
Expand Down Expand Up @@ -108,15 +108,15 @@ Object.keys(exports).filter(function(key) {
exports[key].apply(this, arguments);
suppressOutput = false;
return this;
}
};
// Like any other log function, but suppresses output (and indenting) if
// the "verbose" option IS set.
exports.notverbose[key] = function() {
suppressOutput = option('verbose');
exports[key].apply(this, arguments);
suppressOutput = false;
return this;
}
};
});

// A way to switch between verbose and notverbose modes. For example, this will
Expand All @@ -133,3 +133,13 @@ exports.wordlist = function(arr, separator) {
return item.cyan;
}).join(separator || ', ');
};

// Return a string, uncolored (suitable for testing .length, etc).
exports.uncolor = function(str) {
return str.replace(/\x1B\[\d+m/g, '');
};

// Get a string `str` repeated `n` times.
exports.pad = function(n, str) {
return new Array(n + 1).join(str || ' ');
};
4 changes: 2 additions & 2 deletions lib/grunt/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var data = {};

// Get an option value.
var exports = module.exports = function(key) {
exports = module.exports = function(key) {
var no = key.match(/^no-(.+)$/);
return no ? data[no[1]] === false : data[key];
};
Expand All @@ -12,7 +12,7 @@ exports.get = exports;

// Initialize option data.
exports.init = function(obj) {
return data = obj || {};
return (data = obj || {});
};

// List of options as flags.
Expand Down
1 change: 1 addition & 0 deletions lib/grunt/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports.registerTask = function(name, info, fn) {
registry.tasks.push(name);
// Register task.
return exports.__proto__.registerTask(name, function() {
if (fail.ABORT) { return; }
// A little logging.
verbose.indent(0).header('Running "' + name + '" task').indent();
// Actually run task.
Expand Down
13 changes: 8 additions & 5 deletions lib/grunt/tasks/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var uglifyjs = require('uglify-js');
var Buffer = require('buffer').Buffer;
var zlib = require('zlib');
var handlebars = require('handlebars');
var dateformat = require('dateformat');
Expand All @@ -10,9 +9,13 @@ var dateformat = require('dateformat');

task.registerTask('build', 'Run the "build:max" and "build:min" tasks, as-appropriate.', function() {
// Run "build:max" task if build.max config is defined.
config('build.max') && this.task('build:max');
if (config('build.max')) {
this.task('build:max');
}
// Run "build:min" task if build.min config is defined.
config('build.min') && this.task('build:min');
if (config('build.min')) {
this.task('build:min');
}
});

task.registerTask('build:max', 'Concatenate and write source.', function() {
Expand Down Expand Up @@ -114,7 +117,7 @@ task.registerHelper('banner', function() {
verbose.write('Generating banner...');
try {
banner = handlebars.compile(config('build.banner'))(config('meta')) + '\n';
verbose.ok()
verbose.ok();
} catch(e) {
banner = '';
verbose.error().indent(function() {
Expand All @@ -131,7 +134,7 @@ task.registerHelper('banner', function() {

// Banner helpers.
handlebars.registerHelper('today', function(format) {
return dateformat(new Date, format);
return dateformat(new Date(), format);
});

handlebars.registerHelper('join', function(items, separator) {
Expand Down
14 changes: 11 additions & 3 deletions lib/grunt/tasks/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ task.registerHelper('lint', function(src, extraMsg) {
log.error().indent(function() {
jshint.errors.forEach(function(e) {
if (!e) { return; }
var str = e.evidence ? e.evidence.inverse + ' <- ' : '';
log.error('[L' + e.line + ':C' + e.character + '] ' + str + e.reason);
// var str = e.evidence ? e.evidence.inverse + ' <- ' : '';
// log.error('[L' + e.line + ':C' + e.character + '] ' + str + e.reason);
var pos;
if (e.evidence) {
pos = '['.red + ('L' + e.line) + ':'.red + ('C' + e.character) + ']'.red;
log.writeln(pos + ' ' + e.reason);
log.writeln(e.evidence.inverse);
} else {
log.error(e.reason);
}
});
});
}).writeln();
}
verbose.unindent();
});
4 changes: 2 additions & 2 deletions lib/util/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
var prop = parts.pop();
obj = exports.get(obj, parts, true);
if (obj && typeof obj === 'object') {
return obj[prop] = value;
return (obj[prop] = value);
}
};

Expand All @@ -37,7 +37,7 @@
var prop = parts.pop();
obj = exports.get(obj, parts);

return obj != null && typeof obj === 'object' && prop in obj;
return typeof obj === 'object' && obj && prop in obj;
};

}(typeof exports === 'object' && exports || this));
18 changes: 10 additions & 8 deletions lib/util/task.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
(function(exports) {

// The only reason this is done using prototypal inheritance is so that it
// can be tested by grunt, which also uses this task interface.
exports.create = function() {
return new Task();
};

// Construct-o-rama.
function Task() {
// Current queue of tasks to run.
Expand All @@ -22,6 +16,12 @@
this.helpers = {};
}

// The only reason this is done using prototypal inheritance is so that it
// can be tested by grunt, which also uses this task interface.
exports.create = function() {
return new Task();
};

// Register a new helper.
Task.prototype.registerHelper = function(name, fn) {
// Add task into cache.
Expand Down Expand Up @@ -51,7 +51,7 @@
};

// Enqueue a task.
Task.prototype.task = function(names) {
Task.prototype.task = function() {
// Use the first argument if it's an Array, otherwise convert the arguments
// object to an array and use that.
var names = arguments[0] instanceof Array ? arguments[0] : [].slice.call(arguments);
Expand Down Expand Up @@ -110,7 +110,9 @@
// If queue was empty, we're all done.
if (!task) {
this._running = false;
this._doneFn && this._doneFn();
if (this._doneFn) {
this._doneFn();
}
return;
}
// Add a placeholder to the front of the queue.
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
"dependencies": {
"colors": "~0.5.0",
"dateformat": "1.0.1-1.2.3",
"glob": "~2.0.9",
"handlebars": "1.0.2beta",
"jshint": "~0.2.3",
"jshint": "~0.3",
"nodeunit": "~0.5.5",
"nopt": "~1.0.8",
"pkginfo": "~0.2.2",
"uglify-js": "~1.0.7",
"zlib": "~1.0.5",
"nodeunit": "~0.5.5"
"underscore": "~1.1.7",
"zlib": "~1.0.5"
},
"bin": "./bin/grunt",
"engines": {
Expand Down

0 comments on commit a3dc4a5

Please sign in to comment.