Wednesday, May 28, 2014

Suppress jshint "undefined" Warnings (and more)

If you are getting jshint errors like the following...

app/scripts/my_test.js
  line 8   col 1  'describe' is not defined.
  line 10  col 9  'it' is not defined.


... Just put the following globals section in you .jshintrc file

    "globals"   : {
        /* MOCHA */
        "after"      : false,
        "afterEach"  : false,
        "before"     : false,
        "beforeEach" : false,
        "describe"   : false,
        "it"         : false
    }   


Example .jshintrc file

{
    "node": true,
    "browser": true,
    "esnext": true,
    "bitwise": true,
    "camelcase": true,
    "curly": true,
    "eqeqeq": true,
    "immed": true,
    "indent": 4,
    "latedef": true,
    "newcap": true,
    "noarg": true,
    "quotmark": "single",
    "undef": true,
    "unused": true,
    "strict": true,
    "trailing": true,
    "smarttabs": true,
    "jquery": true,
    "globals"   : {
        /* MOCHA */
        "after"      : false,
        "afterEach"  : false,
        "before"     : false,
        "beforeEach" : false,
        "describe"   : false,
        "it"         : false
    }
}


More Granular

By File

Put this at top of file to suppress variable unused warnings:

/*jshint unused:false*/


If you get errors like the following...

'MyClass' is not defined.


... you can put this at the top of your js file

/*global MyClass*/

Warning

You should take a close look at your code now. Perhaps there is a good reason jshint is warning you that that variable was not defined.

Strict Warning

If you see the following jshint warning: Use the function form of "use strict".

You can disable it on a per file basis: /*jshint strict:false */

or adding this to your .jshintrc file: "strict" : false,

But that's probably not a good idea since it's warning you that if you put 'use strict' outside the function definition block, then if your build process concatonates files that mix strict and non-strict, there will be problems.


(function () {
   'use strict';
   // this function is strict...
}());

'use strict';
(function () {
   // but this function is sloppy...
}());


So, keep strict warning, but place the 'use strict' in the function.

'$' is not defined


If you see an error like:

app/js/main.js
  line 2   col 20   '$' is not defined.


You can put this at the top of that file (app/js/main.js in the example above).

/*global $:false */


By Section

Put this before and after statements you want jshint to ignore;

/* jshint ignore:start */
alert('code ignored by jshint');
/* jshint ignore:end */


By Line

Put this at the end of the line that you want jshint to ignore:

alert('code ignored by jshint'); // jshint ignore:line



Ignore entire file or directory

Create a .jshintignore file at the root of your project.

Ignore a file


src/components/datatables-responsive/datatables.responsive.js


Ignore a directory


src/components/*



References

http://www.jshint.com/docs/
http://www.yuiblog.com/blog/2010/12/14/strict-mode-is-coming-to-town/
http://jshint.com/docs/options/

No comments:

Post a Comment