JSLint简介
JavaScript 作为一门语法灵活多变且对格式要求相对松散的语言,代码格式的混乱和某些语言特性的不正确使用,往往使得最终交付的产品中包含许多因编码风格约定造成的未预见的行为或错误,这种习惯性的问题如果不及时指出并修改,往往会在项目的迭代过程中不断的重现,严重影响 Web 产品的稳定性与安全性。
JSLint 正是 Douglas Crockford 为解决此类问题创建的工具,JSLint 除了能指出这些不合理的约定,还能标出结构方面的问题。虽然 JSLint 不能保证代码逻辑一定正确,但却有助于发现错误并教会开发人员一些好的编码实践。值得一提的是 JSLint 工具本身也是一段 JavaScript 代码,它是检验 JavaScript 代码质量的 JavaScript 脚本。
JSLint 对 JavaScript 脚本的质量检测主要包括以下几个方面:
- 检测语法错误:例如大括号“{}”的配对错误。
- 变量定义规范:例如未定义变量的检测。
- 代码格式规范:例如句末分号的缺失。
- 蹩脚语言特性的使用检测:如 eval 和 with 的使用限制。
目前,与 JSLint 功能类似的 JavaScript 代码检测工具有很多,包括:YUI Test、Firebug、MS Script Debugger 、CompanionJS 等等,它们中大多数都是以浏览器插件的形式存在于客户端浏览器进行 JavaScript 运行时的检测和调试,JSLint 与这些工具的重要区别在于其更加注重静态代码格式的检测,而这也正是当前火热的敏捷开发中持续构建所需要和提倡的。
Eclipse安装JSLint插件的几种方法
方法一:
|
|
方法二:
|
http://code.google.com/p/jslint4java/ jslint4java.jar and source
Usage: jslint4java [options] file.js ...
Options:
--anon
If the space may be omitted in anonymous function declarations
--bitwise
If bitwise operators should be allowed
--browser
If the standard browser globals should be predefined
--continue
If the continuation statement should be tolerated
--css
If css workarounds should be tolerated
--debug
If debugger statements should be allowed
--devel
If logging should be allowed (console, alert, etc.)
--encoding
Specify the input encoding
--eqeq
If == should be allowed
--es5
If es5 syntax should be allowed
--evil
If eval should be allowed
--forin
If for in statements need not filter
--fragment
If html fragments should be allowed
--help
Display usage information
Default: false
--indent
The indentation factor
--jslint
Specify an alternative version of jslint.js
--maxerr
The maximum number of errors to allow
--maxlen
The maximum length of a source line
--newcap
If constructor names capitalization is ignored
--node
If node.js globals should be predefined
--nomen
If names may have dangling _
--on
If html event handlers should be allowed
--passfail
If the scan should stop on first error
--plusplus
If increment/decrement should be allowed
--predef
The names of predefined global variables
--properties
If all property names must be declared with /*properties*/
--regexp
If the . should be allowed in regexp literals
--report
Display report in different formats: plain, xml, junit, checkstyle and
report
--rhino
If the rhino environment globals should be predefined
--sloppy
If the 'use strict'; pragma is optional
--stupid
If really stupid practices are tolerated
--sub
If all forms of subscript notation are tolerated
--timeout
Maximum number of seconds JSLint can run for
Default: 0
--todo
If todo comments are tolerated
--undef
If variables can be declared out of order
--unparam
If unused parameters should be tolerated
--vars
If multiple var statements per function should be allowed
--version
Show the version of JSLint in use.
Default: false
--warnings
Enable additional warnings (jslint4java)
--white
If sloppy whitespace is tolerated
--windows
If ms windows-specific globals should be predefined
using jslint version 2012-12-04
方法三:
|
资料参考
1、http://www.ibm.com/developerworks/cn/web/1105_linlin_jslint/
2、http://stackoverflow.com/questions/2741058/jslint-eclipse-plugin
3、http://www.andyjamesdavies.com/javascript/installing-jslint-on-eclipse-ide