GithubHelp home page GithubHelp logo

jamesshore / simplebuild-jshint Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 1.0 87 KB

A simple library for automating JSHint. Now with multi-core capability.

JavaScript 99.50% Shell 0.26% Batchfile 0.24%

simplebuild-jshint's Introduction

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 the undef option). Each variable should be set to true or false. 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 the message 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 the undef option). Each variable should be set to true or false. 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 the message 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 the undef option). Each variable should be set to true or false. 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 the message 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

  1. Update version history in readme and check in
  2. Ensure clean build: ./jake.sh
  3. Remove temporary branches: git branch, git branch -d [branch]
  4. Update npm version: npm version [major|minor|patch]
  5. Release to npm: npm publish
  6. 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.

simplebuild-jshint's People

Contributors

jamesshore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

tralston

simplebuild-jshint's Issues

.jshintignore not supported

Thanks for making this and your great videos.

Would love to use jshints .jshintignore functionality with this script, so far looks like it's not supported.

.jshintrc file not honored

I have a .jshintrc file in the root of the project. Running jshint via this module doesn't see the .jshintrc file. So I have to specify all my rules inline on each file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.