Skip to content

Commit

Permalink
Cleaned up the lint task a bit. And other stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Oct 6, 2011
1 parent 9fc9aaa commit bbf9c02
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 49 deletions.
2 changes: 0 additions & 2 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ config.init({
latedef: true,
newcap: true,
noarg: true,
proto: true,
sub: true,
undef: true,
eqnull: true,
Expand All @@ -37,7 +36,6 @@ config.init({
setTimeout: true, // temp hack for https://github.com/jshint/jshint/issues/292
grequire: true,
urequire: true,
exit: true,
_: true,
task: true,
file: true,
Expand Down
6 changes: 4 additions & 2 deletions lib/grunt/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ exports.init = function(obj) {
// Test to see if required config params have been defined.
exports.needs = function() {
var props = [].slice.call(arguments);
var msg = 'Verifying option' + (props.length > 1 ? 's' : '') + ' ' + log.wordlist(props) + ' exist in config...';
var msg = 'Verifying option' + (props.length === 1 ? '' : 's') +
' ' + log.wordlist(props) + ' exist' + (props.length === 1 ? 's' : '') +
' in config...';
verbose.write(msg);
var initialized = config.initialized();
var failProps = initialized && props.filter(function(prop) { return !config(prop); });
Expand All @@ -48,7 +50,7 @@ exports.needs = function() {
log.error('Required property "' + prop + '" missing.');
});
}
fail.warn('Unable to process ' + this.name + ' task.');
fail.warn('Unable to continue.');
log.unindent();
}
};
2 changes: 1 addition & 1 deletion lib/grunt/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ exports.writeflags = function(obj, prefix) {
var wordlist;
if (obj instanceof Array) {
wordlist = log.wordlist(obj);
} else {
} else if (typeof obj === 'object' && obj) {
wordlist = log.wordlist(Object.keys(obj).map(function(key) {
var val = obj[key];
return key + (val === true ? '' : '=' + JSON.stringify(val));
Expand Down
53 changes: 24 additions & 29 deletions lib/grunt/tasks/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,36 @@ var jshint = require('jshint').JSHINT;
// ============================================================================

task.registerTask('lint', 'Validate code with JSHint.', function() {
var errors = fail.errors;

// Fail if any required config properties have been omitted.
if (!config.needs('lint')) { return; }
// Enable debugging if requested.
if (option('debug')) {
config.set('jshint.options.devel', true);
config.set('jshint.options.debug', true);
}

var errors = fail.errors;

// Get flags and globals.
var options = config('jshint.options');
var globals = config('jshint.globals');

// Display flags and globals.
verbose.writeflags(config('jshint.options') || {});
verbose.writeflags(config('jshint.globals') || {}, 'Globals');
verbose.writeflags(options, 'Options');
verbose.writeflags(globals, 'Globals');

// If lint files specified, lint them all.
if (config('lint.files')) {
file.files(config('lint.files')).forEach(function(filepath) {
this.helper('lint', file.read(filepath), filepath);
this.helper('lint', file.read(filepath), options, globals, filepath);
}.bind(this));
}

// If pre-lint enabled, lint each individual file.
if (config('lint.pre') && config('build.src')) {
config('build.src').forEach(function(filepath) {
this.helper('lint', file.read(filepath), filepath);
this.helper('lint', file.read(filepath), options, globals, filepath);
}.bind(this));
}

// If post-lint enabled, lint the concatenated source.
if (config('lint.post')) {
this.helper('lint', this.helper('concat', config('build.src')), 'concatenated source');
this.helper('lint', this.helper('concat', config('build.src')), options, globals, 'concatenated source');
}

// Status messages.
Expand All @@ -47,40 +45,37 @@ task.registerTask('lint', 'Validate code with JSHint.', function() {
}
});

// var task = grequire('task');
//
// task.registerTask('lint', 'OVERRIDE', function() {
// console.log('LINTING START');
// this.prop = 123;
// this.super(456);
// console.log('LINTING FINISH');
// });

// ============================================================================
// HELPERS
// ============================================================================

// Lint source code with JSHint.
task.registerHelper('lint', function(src, extraMsg) {
task.registerHelper('lint', function(src, options, globals, extraMsg) {
// Enable debugging if requested.
if (option('debug')) {
options.devel = options.debug = true;
}
// Lint.
var msg = 'Linting' + (extraMsg ? ' ' + extraMsg : '') + '...';
verbose.indent().write(msg);
if (jshint(src, config('jshint.options'), config('jshint.globals'))) {
if (jshint(src, options || {}, globals || {})) {
// Success!
verbose.ok();
} else {
// Something went wrong.
verbose.or.write(msg);
log.error().indent(function() {
// Iterate over all errors.
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);
fail.errors++;
var pos;
if (e.evidence) {
// Descriptive code error.
pos = '['.red + ('L' + e.line) + ':'.red + ('C' + e.character) + ']'.red;
log.writeln(pos + ' ' + e.reason);
log.writeln(e.evidence.inverse);
log.writeln(pos + ' ' + e.reason).writeln(e.evidence.inverse);
} else {
// Generic "Whoops, too many errors" error.
log.error(e.reason);
}
});
Expand Down
36 changes: 21 additions & 15 deletions lib/grunt/tasks/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var nodeunit = require('nodeunit');

// Ugh. Monkey patching nodeunit so that arbitrary code can be run when it's
// done testing.
// Ugh. Monkey patching nodeunit so that arbitrary code (like the next task)
// can be run when it's done testing. Good times.
nodeunit._done = function() {};
nodeunit._runFiles = nodeunit.runFiles;
nodeunit.runFiles = function(files, options) {
Expand All @@ -18,20 +18,31 @@ nodeunit.runFiles = function(files, options) {
// TASKS
// ============================================================================

// The default task. Override this in your grunt.js if you feel like it.
task.registerTask('test', 'Run unit tests.', function() {
// Only run this task if tests have been defined.
if (!config('test')) {
verbose.writeln('No tests defined, skipping.');
return;
// Fail if any required config properties have been omitted.
if (!config.needs('test')) { return; }

// Use a nodeunit reporter if specified.
var name = config('test.reporter');
// Otherwise use "default" if --verbose, and "minimal" if not.
if (!name) {
name = option('verbose') ? 'default' : 'minimal';
}

var name = config('test.reporter') || (option('verbose') ? 'default' : 'minimal');
// The actual nodeunit reporter.
var reporter = nodeunit.reporters[name];

// Nodeunit tests run asynchronously.
// Abort if reporter can't be found.
if (!reporter) {
fail.warn('Invalid nodeunit reporter "' + name + '" specified.');
return;
}

// Nodeunit tests run asynchronously!
var done = this.async();
// When done, do some stuff and then continue processing other tasks.

// When done, do some stuff and then continue processing other tasks. See
// the delightful monkey patch at the top of this file.
nodeunit._done = function(arr) {
if (arr.failures() > 0) {
fail.warn('Test failure.');
Expand All @@ -43,8 +54,3 @@ task.registerTask('test', 'Run unit tests.', function() {
var filepaths = file.files(config('test.files'));
reporter.run(filepaths);
});

// ============================================================================
// HELPERS
// ============================================================================

0 comments on commit bbf9c02

Please sign in to comment.