A JSHint task with caching and asynchonous linting.
The task I want the grunt-contrib-jshint task to be.
I offered to merge but they didn't seem interested in saving puppies.
Here is an example config showing the default options.
var cfg =jshint2:options:// A path to your jshintrc file if you'd like to use onejshintrc: undefined// jshint options to be passed to jshint.lint.jshint:// A list of predefined variables passed to jshint.lintglobals:// What reporter you'd like to use; see Reporters for more inforeporter: "default"// Do not fail task on jshint errorsforce: false// Cache successful files; see Caching for more infocache: true// Use the 'async' or 'cluster' processor; see Processors for more infoprocessor: "async"// When using the async processor, the max number of files to process at a time.spawnLimit: 5;
Caching is done by keeping a computed sha1 hash of the file contents, the jshint options (stringified) and the globals passed to the jshint.lint function.
var sha1 = cryptocreateHash'sha1';// The hash should include the file contentssha1updatecontent;// And the jsHintOptssha1updateJSONstringifythisjsHintOpts;// And the globalssha1updateJSONstringifythisglobals;return sha1digest"hex";
After a file is successfully linted an empty file with a name matching it's computed
hash is created in a temp directory (using
os.tmpDir()). On subsequent jshint calls
an existence check of the file determines whether we can skip validating it a second time.
By default, the directory where cached files is stored is dependant on the module version. So updating the module will invalidate any already cached files.
Reporters offer a way to customize the output of the jshint2 task. Since jshint's reporters seem to be in a state of flux at the moment, I've copied their implementations over in order to maintain continuity.
A reporter can be specified in three ways
// Reporters interfacevar myReporter =// The task is starting to process files// A file was successfully linted// A file had errors// The task is finished processing;
Custom objects will have their
options attribute set to the task options. More information available from the
A processor takes a list of files, some jshint options and globals and runs jshint on each file. The default processor is an asynchronous processor that reads each file and runs jshint. Other processors like the cluster processor can take advantage of multi core machines in order to run jshint on different threads.
At this time it's not possible to write your own processor, but here is the interface that each processor should implement.
// Options contains; files, jsHintOpts, globals, cache, spawnLimitthisoptions = options;_extendMyProcessorprototype EventEmitterprototype;_extendMyProcessorprototype// Begin processing// Must emit the following events:// On lint successthisemit"success" filePath wasCached;// On lint failurethisemit"fail" filePath problems data;// On errorthisemit"error" err;// On finishthisemit"exit";;
Copyright 2013, 2014 Jacob Gable, MIT License.