JSHINT:A Static Code Analysis tool for JavaScript (JavaScript verifier). It has many kinds of installation methods. http://jshint.com/install/. This article mainly describes the installation method as a grunt plugin.
1, installation Jshint
Installation Prerequisites: node and grunt need to be installed. http://blog.csdn.net/wangfupeng1988/article/details/46418203
①windows Platform: npm install Grunt-contrib-jshint--save-dev
② other platforms in front plus sudo
③ Note:--save-dev is designed to automatically build devdependencies in Package.json. can be set manually.
2. Configuration
The ① can be configured in the Gruntfile.js,
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 '] }, } },});
② can use an external jshintrc file, the underlined code represents a JSHINTRC file with the same directory as gruntfile.js, or it can be a jshintrc file in another directory, using a relative directory.
Grunt.initconfig ({ jshint: { options: { jshintrc: '. Jshintrc ' }, uses_defaults: [' dir1/** /*.js ', ' dir2/**/*.js '], with_overrides: { options: { curly:false, undef:true, }, Files: { src: [' dir3/**/*.js ', ' Dir4/**/*.js ' }, } },});
. jshintrc file (standard JSON file):
{ "Curly": True, "Eqeqeq": True, "Eqnull": True, "browser": True, "globals": { "jQuery": True }}
3. Explanation of configuration parameters
Bitwise: Is it forbidden to use bit operations, since JavaScript uses bit arithmetic very little and often writes && errors to &.
CamelCase: Verify that the variable is camel-or upper_case-obsolete, and will be removed in the jshint later.
Curly: Use curly braces, such as if (true) dosomething (), need to use curly braces, if (true) {dosomething ();}
Eqeqeq: Mandatory = = (! =) = = = = (!==)
ES3: Performed according to ECMAScript 3, for IE6/7/8/9
ES5: Performed according to ECMAScript 5.1, not applicable for low-level browsers
Forin: Verifies that the properties of the loop are the object's own, not inherited. If you don't use hasOwnProperty, you'll get an error
Freeze: Prohibits rewriting the prototype chain of a local object, such as prohibiting Array.property.someAttr = function () {};
Funcscope: Prompt If an external variable is used for the local scope.
Futurehostile: Prompt for identifiers that may appear in the future
Globals: Set global variable Whitelist
Iterator: iterators, which are not supported by all browsers.
Latedef:this option prohibits the use of a variable before it is defined.
Maxcomplexity: Maximum complexity
MaxDepth: Max Depth
Maxerr: Maximum number of warnings, default is 50
MaxLen: The maximum length of each line of code; obsolete, will be removed
Maxparams: Maximum number of parameters per function
Maxstatements: The maximum number of statement that each function contains
Noarg: Prohibit use arguments.caller
and arguments.callee
.
Nocomma: Prohibit the use of commas in a statement, a very unreasonable requirement
Noempty: Prohibit empty block, obsolete, will be removed
NONBSP: No spaces
Nonew: Disallow the use of unassigned constructors, for example: New Newconstructor ();
Notypeof: When using typeof, the value of the comparison does not exist in the TypeOf results list, warning
shadow:http://jshint.com/docs/options/#shadow
singlegroups:http://jshint.com/docs/options/#singleGroups
Strict:ecmascript 5 Strict mode
UNDEF: Variable not declared
Unused: No variables used
VARSTMT: Prohibit use of VAR declaration variable
ASI: Semicolon
boss:http://jshint.com/docs/options/#boss
Debug: If Debugger statement appears, an error is
Elision:uses ES3 Array elision elements
Eqnull: If the code used ==null, then error, unreasonable requirements.
Esnext: Using ECMAScript 6 syntax parsing, unfortunately ECMAScript is not finalized, so this verification is not standard. And there is no browser implementation ECMAScript 6.
Evil: Error when using eval
expr:http://jshint.com/docs/options/#expr
Gloablstrict: When using global strict mode times wrong
Lastsemic: Allowed at the end of the block not applicable semicolon
loopfunc:http://jshint.com/docs/options/#loopfunc
Moz:mozilla JavaScript Extensions
Plusplus: Prohibit the use of + + and--
Proto: When using __proto__ times wrong
scripturl:http://jshint.com/docs/options/#scripturl
Supernew: Prohibit the use of these actions: and new function () { ... }
new Object;
.
Validthis: Validating this variable
Withstmt: Disable with Operation
Browser: Browser Environment
Other production environment: http://jshint.com/docs/options/#environments
4. Common parameters
Bitwise, Curly, Eqeqeq, Es3, Forin, Freeze, Funcscrop, Futurehostile, Globals, Latedef, Noarg, nonbsp, notypeof, Strict, UNDEF, unused
ASI, Eqnull, evil, expr, globalstict, Loopfunc, Plusplus, Validthis, withstmt
Production environment: Browser, jquery, Dojo, node, Qunit, Prototypejs, Yui
5, load plug-in, after Grunt.initconfig code
Grunt.loadnpmtasks (' Grunt-contrib-jshint ');
6, registration tasks, after loading the plugin
Grunt.registertask (' Core-js ', [' Jshint:core ']);
7, use, enter the following code in the terminal
Grunt Core-js
8. Example:
1//gruntfile.js file 2 module.exports = function (grunt) {3 ' use strict '; 4 5 grunt.initconfig ({6 7 Pkg:grunt.file.readJSON (' Package.json '), 8 9 jshint: {Ten options: {One jshintrc: '. Jshintrc ' 12 },13 Core: {+ src: ' src/js/**/*.js ' }16 }17 }); grunt.loadnpmtasks ( ' Grunt-contrib-jshint '), grunt.registertask (' Core-js ', [' Jshint:core ']), and Grunt.registertask (' Default ', []); 25};
1//. jshintrc file code, standard JSON format, same level directory as Gruntfile.js 2 {3 "bitwise": TRUE, 4 "Curly": false, 5 "Eqeqeq": false, 6< c3/> "Es3": True, 7 "freeze": true, 8 "Funcscrop": true, 9 "Futurehostile": true,10 "latedef": true,11< c8/> "Noarg": true,12 "nonbsp": true,13 "notypeof": true,14 "strict": true,15 "undef": true,16 "unused": true,17 "ASI": true,18 "eqnull": true,19 "evil": true,20 "globalstrict": true, "Loopfunc": true,22 "Plusplus": false,23 "validthis": true,24 "withstmt": true,25 " Browser ": true,26 " jquery ": True27}
Using the Grunt-contrib-jshint plugin