Giter VIP home page Giter VIP logo

grunt-contrib-jshint's Introduction

grunt-contrib-jshint Build Status

Validate files with JSHint.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-contrib-jshint --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-contrib-jshint');

Jshint task

Run this task with the grunt jshint command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

For more explanations of the lint errors JSHint will throw at you please visit jslinterrors.com.

Options

Any specified option will be passed through directly to JSHint, thus you can specify any option that JSHint supports. See the JSHint documentation for a list of supported options.

A few additional options are supported:

globals

Type: Object Default value: null

A map of global variables, with keys as names and a boolean value to determine if they are assignable. This is not a standard JSHint option, but is passed into the JSHINT function as its third argument. See the JSHint documentation for more information.

jshintrc

Type: String Default value: null

If this filename is specified, options and globals defined therein will be used. The jshintrc file must be valid JSON and looks something like this:

{
  "curly": true,
  "eqnull": true,
  "eqeqeq": true,
  "undef": true,
  "globals": {
    "jQuery": true
  }
}

Be aware that jshintrc settings are not merged with your Grunt options.

extensions

Type: String Default value: ''

A list of non-dot-js extensions to check.

ignores

Type: Array Default value: null

A list of files and dirs to ignore. This will override your .jshintignore file if set and does not merge.

force

Type: Boolean Default value: false

Set force to true to report JSHint errors but not fail the task.

reporter

Type: String Default value: null

Allows you to modify this plugins output. By default it will use a built-in Grunt reporter. Set the path to your own custom reporter or to one of the built-in JSHint reporters: jslint or checkstyle.

See also: Writing your own JSHint reporter.

reporterOutput

Type: String Default value: null

Specify a filepath to output the results of a reporter. If reporterOutput is specified then all output will be written to the given filepath instead of printed to stdout.

Usage examples

Wildcards

In this example, running grunt jshint:all (or grunt jshint because jshint is a multi task) will lint the project's Gruntfile as well as all JavaScript files in the lib and test directories and their subdirectores, using the default JSHint options.

// Project configuration.
grunt.initConfig({
  jshint: {
    all: ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
  }
});

Linting before and after concatenating

In this example, running grunt jshint will lint both the "beforeconcat" set and "afterconcat" sets of files. This is not ideal, because dist/output.js may get linted before it gets created via the grunt-contrib-concat plugin concat task.

In this case, you should lint the "beforeconcat" files first, then concat, then lint the "afterconcat" files, by running grunt jshint:beforeconcat concat jshint:afterconcat.

// Project configuration.
grunt.initConfig({
  concat: {
    dist: {
      src: ['src/foo.js', 'src/bar.js'],
      dest: 'dist/output.js'
    }
  },
  jshint: {
    beforeconcat: ['src/foo.js', 'src/bar.js'],
    afterconcat: ['dist/output.js']
  }
});

Specifying JSHint options and globals

In this example, custom JSHint options are specified. Note that when grunt jshint:uses_defaults is run, those files are linted using the default options, but when grunt jshint:with_overrides is run, those files are linted using merged task/target options.

// Project configuration.
grunt.initConfig({
  jshint: {
    options: {
      curly: true,
      eqeqeq: true,
      eqnull: true,
      browser: true,
      globals: {
        jQuery: true
      },
    },
    uses_defaults: ['dir1/**/*.js', 'dir2/**/*.js'],
    with_overrides: {
      options: {
        curly: false,
        undef: true,
      },
      files: {
        src: ['dir3/**/*.js', 'dir4/**/*.js']
      },
    }
  },
});

Ignoring specific warnings

If you would like to ignore a specific warning:

[L24:C9] W015: Expected '}' to have an indentation at 11 instead at 9.

You can toggle it by prepending - to the warning id as an option:

grunt.initConfig({
  jshint: {
    ignore_warning: {
      options: {
        '-W015': true,
      },
      src: ['**/*.js'],
    },
  },
});

Ignoring specific files

Occasionally application files and third party libraries share the same directory. To exclude third party code, but include all current and future application files, use a glob for files and specifically exclude libraries using ignores. In this example, the jQuery file is matched by the glob but subsequently ignored when JSHint does its analysis.

grunt.initConfig({
    jshint: {
        files: ['js/*.js'],
        options: {
            ignores: ['js/jquery.js']
        }
    }
});

Release History

  • 2013-06-02   v0.6.0   Dont always succeed the task when using a custom reporter. Bump jshint to 2.1.3.
  • 2013-05-22   v0.5.4   Fix default reporter to show offending file.
  • 2013-05-19   v0.5.3   Performance: Execute the reporter once rather than per file.
  • 2013-05-18   v0.5.2   Fix printing too many erroneous ignored file errors.
  • 2013-05-17   v0.5.1   Fix for when only 1 file is lint free.
  • 2013-05-17   v0.5.0   Bump to jshint 2.0. Add support for .jshintignore files and ignores option Add support for extensions option. Add support for custom reporters and output report to a file.
  • 2013-04-08   v0.4.3   Fix evaluation of predef option when it's an object.
  • 2013-04-08   v0.4.2   Avoid wiping force option when jshintrc is used.
  • 2013-04-06   v0.4.1   Fix to allow object type for deprecated predef.
  • 2013-04-04   v0.4.0   Revert task level options to override jshintrc files.
  • 2013-03-13   v0.3.0   Bump to JSHint 1.1.0. Add force option to report JSHint errors but not fail the task. Add error/warning code to message. Allow task level options to override jshintrc file.
  • 2013-02-26   v0.2.0   Bump to JSHint 1.0
  • 2013-02-15   v0.1.1   First official release for Grunt 0.4.0.
  • 2013-01-18   v0.1.1rc6   Updating grunt/gruntplugin dependencies to rc6. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
  • 2013-01-09   v0.1.1rc5   Updating to work with grunt v0.4.0rc5. Switching to this.filesSrc api.
  • 2012-10-18   v0.1.0   Work in progress, not yet officially released.

Task submitted by "Cowboy" Ben Alman

This file was generated on Wed Jul 24 2013 09:25:28.

grunt-contrib-jshint's People

Contributors

billwolckenlmco avatar buritica avatar cliffano avatar cowboy avatar ekashida avatar gruber76 avatar nickbabcock avatar nschonni avatar robwierzbowski avatar sebdeckers avatar shama avatar sprzybylski avatar xzyfer avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.