simplebuild-jshint
v1.3.0
Published
A simple library for automating JSHint
Downloads
123
Readme
Simplebuild-JSHint
A simple library for automating JSHint. Now with multi-core capability.
JSHint is a static analysis ("lint") tool for JavaScript. It analyzes JavaScript source code for common mistakes. This library provides a simple interface to JSHint that's convenient to use with task automation tools such as Grunt or Jake.
When you're linting a lot of files, this library will run JSHint across multiple cores. JSHint is CPU-bound, so this can result in a big speed boost. It only happens when you have a lot of files to lint (35 or more at the time of this writing) because spawning worker processes is slow.
Installation
This is a Node.js library. Install Node, then:
npm install simplebuild-jshint
(add --save
or --save-dev
if you want)
Note that this library uses your existing JSHint installation. (JSHint will be installed if needed.)
Usage
This library provides these functions:
checkFiles
: Run JSHint against a list of files.checkOneFile
: Run JSHint against a single file (it's useful for auto-generated build dependencies).checkCode
: Run JSHint against raw source code.
checkFiles(options, success, failure)
Run JSHint against a list of files. A dot will be written to stdout for each file processed. Any errors will be written to stdout. When there are a large number of files to process, additional worker processes will be spawned to take advantage of additional CPU cores.
options
: an object containing the following properties:files
: a string or array containing the files to check. Globs (*
) and globstars (**
) will be expanded to match files and directory trees respectively. Prepend!
to exclude files.options
(optional): JSHint options (see the JSHint documentation).globals
(optional): Permitted global variables (for use with theundef
option). Each variable should be set totrue
orfalse
. If false, the variable is considered read-only.
success()
: a function to call if the code validates successfully.failure(message)
: a function to call if the code does not validate successfully. A simple error message is provided in themessage
parameter, but detailed error messages are written to stdout.
checkOneFile(options, success, failure)
Run JSHint against a single file (it's useful for auto-generated build dependencies).
options
: an object containing the following properties:file
: a string containing the path to the file to check.options
(optional): JSHint options (see the JSHint documentation).globals
(optional): Permitted global variables (for use with theundef
option). Each variable should be set totrue
orfalse
. If false, the variable is considered read-only.
success()
: a function to call if the code validates successfully.failure(message)
: a function to call if the code does not validate successfully. A simple error message is provided in themessage
parameter, but detailed error messages are written to stdout.
checkCode(options, success, failure)
Run JSHint against raw source code. Any errors will be written to stdout.
options
: an object containing the following properties:code
: a string containing the source code to check.options
(optional): JSHint options (see the JSHint documentation).globals
(optional): Permitted global variables (for use with theundef
option). Each variable should be set totrue
orfalse
. If false, the variable is considered read-only.
success()
a function to call if the code validates successfully.failure(message)
a function to call if the code does not validate successfully. A simple error message is provided in themessage
parameter, but detailed error messages are output to stdout.
Examples
This library is designed to be easy to integrate with any task automation tool:
Grunt
var jshint = require("simplebuild-jshint");
module.exports = function(grunt) {
grunt.initConfig({
jshint: {
files: [ "*.js", "src/**/*.js", "test/**/*.js" ],
options: {
bitwise: true,
curly: false,
eqeqeq: true
// etc
}
}
});
grunt.registerTask("lint", "Lint everything", function() {
jshint.checkFiles(grunt.config("jshint"), this.async(), grunt.warn);
});
grunt.registerTask("default", [ "lint" ]);
};
Jake
var jshint = require("simplebuild-jshint");
task("default", [ "lint" ]);
task("lint", function() {
jshint.checkFiles({
files: [ "*.js", "src/**/*.js", "test/**/*.js" ],
options: {
bitwise: true,
curly: false,
eqeqeq: true
// etc
}
}, complete, fail);
}, { async: true });
Plain JavaScript
var jshint = require("simplebuild-jshint");
jshint.checkFiles({
files: [ "*.js", "src/**/*.js", "test/**/*.js" ],
options: {
bitwise: true,
curly: false,
eqeqeq: true
// etc
}
}, function() {
console.log("OK");
}, function(message) {
console.log(message);
});
About Simplebuild
This library is a simplebuild module. In addition to being used as a standalone library (as described above), it can also be used with simplebuild extensions and mappers. For more information about simplebuild, see the Simplebuild GitHub page.
Version History
1.3.0: checkFiles()
uses multiple cores when a lot of files need linting. This can result in a big speed boost.
1.2.0: checkFiles()
reads files asynchronously and in parallel, which makes it a bit faster.
1.1.0: Better error messages when options
parameter is incorrect.
1.0.1: Fix: doesn't try to report non-existent error codes (they're not present in old versions of JSHint).
1.0.0: Reports warning codes (and error codes) so they can be disabled more easily.
0.3.1: Fix: crashed when error objects had no evidence (first seen in JSHint 2.8.0).
0.3.0: Added jshint
as a peer dependency. It no longer needs to be installed separately.
0.2.0: checkOneFile()
.
0.1.1: Corrected documentation error: options.globals is not actually a JSHint option.
0.1.0: checkSource()
and checkFiles()
.
Contributors
Created by James Shore.
Release Process
- Update version history in readme and check in
- Ensure clean build:
./jake.sh
- Remove temporary branches:
git branch
,git branch -d [branch]
- Update npm version:
npm version [major|minor|patch]
- Release to npm:
npm publish
- Release to github:
git push && git push --tags
License
The MIT License (MIT)
Copyright (c) 2012-2015 James Shore
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.