Skip to content

Instantly share code, notes, and snippets.

@willhlaw
Created August 16, 2013 21:17
Show Gist options
  • Save willhlaw/6253592 to your computer and use it in GitHub Desktop.
Save willhlaw/6253592 to your computer and use it in GitHub Desktop.
This code snippet helps in reply to a JSHINT pull request here: https://github.com/jshint/jshint/pull/1221. The code is part of a node.js file that programmatically looks through a project's files and outputs the results of jshint for each file.
//get node specific and less strict options from .jshintrc file
var jsHintOPTIONS = JSON.parse(JSON.minify(fs.readFileSync('./.jshintrc', 'utf8')));
/*Purpose: To run jsHint on a str.js
Params: error message passed up from callback caller, string is path and filename to a .js file
Output: Outputs to console a very similar output as "$ jshint myfile.js". After a file finishe with errors, script exits with code 1
Async: false - because call to readFile uses readFileSync version
*/
function hint(err, str) {
if (err) {
console.log(err);
} else {
//var fileContents = fs.readFile(str, 'utf8', )
var result = jshint.JSHINT(readFile(str), jsHintOPTIONS);
if (result) {
console.log("jsHinting " + str + "...OK!");
}
else {
var jshErrors = jshint.JSHINT.data().errors;
for (var i = 0; i < jshErrors.length; i++) {
//var errMsg = jshError;
var errMsg = str + ": line " + jshErrors[i].line + ", col " + jshErrors[i].character + ", " + jshErrors[i].reason;
console.log(errMsg);
}
process.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment