JSLint with Grunt

Grunt is fantastic for automating linting tasks and enforcing coding standards by making it extremely easy to follow them!

JSLint has many different options that can be configured.

In order to get JSLint to run you can use the grunt-jslint npm package. This pacakge gives grunt configuration.

You can configure multiple sections for client, server, shared or whatever you would like. Rules can specified to include and exclude files. You can also configure the rules that should be applied for each section.

The example below includes config sections for client and server side linting.

module.exports = function(grunt) {  
  'use strict';

  // load grunt-jslint;
  grunt.loadNpmTasks('grunt-jslint');

  grunt.initConfig({
    jslint: {
      // lint the server code
      server: {
        // files to lint
        src: [
          'app.js',
          'server/*.js',
          'server/routes/*.js'
        ],
        // files to exclude
        exclude: [
          'server/config.js'
        ],
        // lint options
        directives: {
          // node environment
          node: true,
          // browser environment
          browser: false,
          // allow dangling underscores
          nomen: true,
          // allow todo statements
          todo: true,
          // allow unused parameters
          unparam: true,
          // don't require strcit pragma
          sloppy: true,
          // allow whitespace discrepencies
          white: true
        }
      },      
      // lint the client code
      client: {
        src: [
          'client/*.js'
        ],
        directives: {
          // node environment
          node: false,
          // browser environment
          browser: true,
          // allow dangling underscores
          nomen: true,
          // allow todo statements
          todo: true,
          // allow unused parameters
          unparam: true,
          // add predefined libraries
          predef: [
            '$',
            '_',
            'Handlebars',
            'Backbone',
          ]
        },
      }
    }
  });

  grunt.registerTask('lint', 'Run linting', ['jslint']);

}

With this grunt configuration you can then execute grunt lint and you code will be linted!

comments powered by Disqus