使用grunt-contrib-jshint插件
jshint:A Static Code Analysis Tool for JavaScript(javascript验证工具)。它有很多种安装方法。http://jshint.com/install/。这篇文章主要介绍作为grunt插件的安装方法。
1、安装jshint
安装前提:需要安装node和grunt。http://blog.csdn.net/wangfupeng1988/article/details/46418203
①windows平台下:npm install grunt-contrib-jshint --save-dev
②其他平台前面加sudo
③注:--save-dev是为了在package.json自动构建devDependencies。可手动设置。
2、配置
①可在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'] }, } }, });
②可使用外部jshintrc文件,加下划线的代码表示的是与Gruntfile.js相同目录的jshintrc文件,也可以是其他目录下的jshintrc文件,使用相对目录就行了。
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文件(标准JSON文件):
{ "curly": true, "eqeqeq": true, "eqnull": true, "browser": true, "globals": { "jQuery": true } }
3、配置参数解释
bitwise:是否禁止使用位运算,因为在javascript使用位运算的时候很少,并且很多时候会把&&错写为&。
camelCase:验证变量是否是骆驼式或UPPER_CASE写法;过时的,在后面的jshint中会移除。
curly:使用大括号,比如if(true) dosomething();需要使用大括号,if(true) {dosomething();}
eqeqeq:强制==(!=)为===(!==)
es3:按照ECMAScript 3标准执行,针对IE6/7/8/9
es5:按照ECMAScript 5.1标准执行,对于低级的浏览器不适用
forin:验证循环的属性是否是对象自己的,而不是继承的。如果没有使用hasOwnproperty会报错
freeze:禁止重写本地对象的原型链,如禁止Array.property.someAttr = function(){};
funcscope:如果局部作用域使用了外部变量,则提示。
futurehostile:提示是否使用了未来可能会出现的标识符
globals:设置全局变量白名单
iterator:迭代器,所有浏览器都不支持。
latedef:This option prohibits the use of a variable before it was defined.
maxcomplexity:最大复杂度
maxdepth:最大深度
maxerr:最大警告数,默认为50
maxlen:每一行代码的最大长度;过时的,将被移除
maxparams:每个函数的最大参数个数
maxstatements:每一个函数最多包含的statement的数目
noarg:禁止使用arguments.caller
和 arguments.callee
.
nocomma:在一个statement中禁止使用逗号,好无理的要求
noempty:禁止空的block,过时的,将被移除
nonbsp:禁止空格
nonew:禁止使用不赋值的构造函数,例:new NewConstructor();
notypeof:在使用typeof的时候,对比的值不存在typeof结果列表中,警告
shadow:http://jshint.com/docs/options/#shadow
singleGroups:http://jshint.com/docs/options/#singleGroups
strict:ECMAScript 5严格模式
undef:未声明变量
unused:未使用变量
varstmt:禁止使用var申明变量
asi:分号
boss:http://jshint.com/docs/options/#boss
debug:如果出现debugger statement,则报错
elision:uses ES3 array elision elements
eqnull:如果代码中使用了==null,则报错,无理的要求。
esnext:使用ECMAScript 6语法解析,可惜的是ECMAScript还未定稿,所以这个验证也不标准。并且没有任何浏览器实现ECMAScript 6.
evil:当使用eval的时候报错
expr:http://jshint.com/docs/options/#expr
gloablstrict:当使用全局严格模式时报错
lastsemic:允许在块的最后不适用分号
loopfunc:http://jshint.com/docs/options/#loopfunc
moz:Mozilla JavaScript 扩展
plusplus:禁止使用++和--
proto:当使用__proto__时报错
scripturl:http://jshint.com/docs/options/#scripturl
supernew:禁止使用这些操作:new function () { ... }
and new Object;
.
validthis:验证this变量
withstmt:禁止使用with操作
browser:浏览器环境
其他生产环境:http://jshint.com/docs/options/#environments
4、常用的参数
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
生产环境:browser、jquery、dojo、node、qunit、prototypejs、yui
5、加载插件,在grunt.initConfig代码之后
grunt.loadNpmTasks('grunt-contrib-jshint');
6、注册任务,在加载插件之后
grunt.registerTask('core-js', ['jshint:core']);
7、使用,在终端中输入以下代码
grunt core-js
8、例子:
1 // Gruntfile.js文件 2 module.exports = function( grunt ) { 3 'use strict'; 4 5 grunt.initConfig({ 6 7 pkg : grunt.file.readJSON('package.json'), 8 9 jshint : { 10 options : { 11 jshintrc : '.jshintrc' 12 }, 13 core : { 14 src : 'src/js/**/*.js' 15 } 16 } 17 18 }); 19 20 grunt.loadNpmTasks('grunt-contrib-jshint'); 21 22 grunt.registerTask('core-js', ['jshint:core']); 23 24 grunt.registerTask('default', []); 25 };
1 // .jshintrc文件代码,标准json格式,与Gruntfile.js同一级目录 2 { 3 "bitwise": true, 4 "curly": false, 5 "eqeqeq": false, 6 "es3": true, 7 "freeze": true, 8 "funcscrop": true, 9 "futurehostile": true, 10 "latedef": true, 11 "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, 21 "loopfunc": true, 22 "plusplus": false, 23 "validthis": true, 24 "withstmt": true, 25 "browser": true, 26 "jquery": true 27 }
相关推荐
grunt-contrib-jshint v3.0.0 使用JSHint验证文件 入门 如果您以前从未使用过 ,请务必查看《指南》,因为它说明了如何创建以及安装和使用Grunt插件。 熟悉该过程后,可以使用以下命令安装此插件: npm install ...
简单的咕噜声示例 为 grunt-contrib-jshint 和 grunt-contrib-watch 创建一个简单的例子
loadNpmTasks ( 'grunt-contrib-jshint' ) ; grunt . loadNpmTasks ( 'grunt-contrib-watch' ) ; grunt . loadNpmTasks ( 'grunt-...' ) ; grunt . loadTasks ( 'foo' ) ; grunt . loadTasks ( 'bar' ) ; grunt . ...
watch --save-devnpm install grunt-contrib-jshint --save-devnpm install grunt-contrib-uglify --save-devnpm install grunt-contrib-compass --save-devnpm install grunt-contrib-copy --save-devnpm inst
jshint-json JSON报告程序 使JSHint与其他工具一起使用变得容易。 安装 npm install --save-dev jshint-json 用法 JSHint CLI $ jshint --reporter node_modules/jshint-json/json.js file.js grunt . ...
添加一个task非常简单,以grunt-contrib-jshint为例,阅读官方,在 terminal 执行: npm install grunt-contrib-jshint --save-dev npm会下载grunt-contrib-jshint到本地的node_modules目录并且把它加到package.json...
grunt-starterkit 用于前端工作流的 Grunt StarterKit,从 Matt Bailey 改编和扩展: 完整的信息和说明可以在 Matt Bailey... jshint-stylish grunt-contrib-watch grunt-contrib-connect grunt-contrib-jade 帮助
jshint-smart 回应记者 例子 在Linux上: 在Windows上: 仅当您使用标志--verbose运行JSHint时,才会显示“代码”列。 仅当有足够的空间时,才会显示“证据”列。 安装 $ npm install --save-dev jshint-smart ...
浏览器输出 在浏览器的弹出窗口中显示严重错误。... 重要提示:将此插件与grunt-contrib-watch和livereload需要grunt-contrib-watch版本0.6.0或更高版本,并且您必须配置 。 “ browser_output”任务 概述 在项目的
npm install grunt-contrib-jshint —> 为代码质量标准安装 jshint npm install grunt-contrib-watch —> 用于监视项目中的文件更改 npm install grunt-contrib-jasmine —> 安装 jasmine 测试框架 下一步创建...
加载 grunt-initconfig 将您的长配置分成一口大小的块。 安装 npm install load-grunt-initconfig --save-dev 用法 load-grunt-initconfig将在.initconfig目录中查找... loadNpmTasks ( 'grunt-contrib-copy' ) ; g
第1步-Grunt基础知识和jshint npm install grunt --save-devnpm install grunt-contrib-jshint --save-dev请注意,这更改了package.json文件-它添加了“ devDependencies”。 当其他人下载您的package.json文件并...
cssmin grunt-contrib-htmlmin grunt-contrib-imagemin grunt- contrib-jshint grunt-contrib-uglify grunt-exec grunt-mocha grunt-open grunt-requirejs grunt-usemin jshint-stylish matchdep mocha-phantomjs ...
Grunt-工作流依赖关系-grunt-contrib-compass -grunt-contrib-jshint -grunt-contrib-clean -grunt-contrib-jshint -grunt-contrib-express -grunt-contrib-watch -咕噜声通知
* grunt-contrib-jshint * grunt-contrib-less * grunt-contrib-uglify * grunt-patch * grunt-postcss * jit-grunt" * load-grunt-configs" * pixrem": "2.0.0 * postcss-zindex * time-grunt 凉亭依赖性: * ...
探索整合特征: * Uses Bower* Uses Grunt咕packages包*grunt-contrib-copy*grunt-contrib-cssmin*grunt-contrib-jshint*grunt-contrib-less*grunt-contrib-uglify*grunt-contrib-watch*jshint-stylish凉亭套餐*...
很快部署 URL缩短器。 目录 要求 - bcrypt-nodejs: 0.0.3, ...- grunt-contrib-jshint: ^0.9.2, - grunt-contrib-uglify: ^0.4.0, - grunt-contrib-watch: ^0.6.1, - grunt-mocha-test: ^0.10.0, - grunt-nodem
笔记作者:林东日期:美国东部...-save-dev安装咕噜声 npm install -g grunt-clinpm install grunt --save-devnpm install grunt-contrib-jshint --save-dev咕噜文件创建一个 grunt模板文件 npm install grunt-init -
2)安装咕噜声 npm install -g grunt-clinpm install grunt --save-dev3) 安装 grunt 插件 npm install grunt-contrib-uglify --save-devnpm install grunt-contrib-jshint --save-devnpm install grunt-contrib-...
:运行npm install -g grunt-cli 安装 :运行npm install -g bower安装依赖节点包依赖运行npm install (将安装 grunt-contrib-connect / grunt-contrib-watch / grunt-contrib-compass / grunt-contrib-jshint)...