在Eclipse中使用JSLint保证JavaScript代码质量

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插件的几种方法

  笔者在安装JSLint的时候参考了网上的几种方法,现整理如下:

  方法一:

Step One: Open Eclipse up on your machine and go to ‘Help’ -> ‘Install New Software’
Step Two: Click ‘add’ and to stuff the location with the following link: 
https://svn.codespot.com/a/eclipselabs.org/mobile-web-development-with-phonegap/tags/jslint4java1/download then click ‘ok’.
Step Three: Selecting ‘Javascript Development Tools’ and ‘jslint4java’ and click next.

  方法二:

Step One: Download jslint4java
Step Two: Put jslint4java.jar somewhere
Step Three: Add an external tool configuration in Eclipse:
                   Location: /usr/bin/java(or your path to javaw.exe)
                   Arguments: -jar /path/to/jslint4java.jar ${resource_loc}

  方法三:

1. Help -> Install new software
2. Click the 'Add' button
    Name : Rockstar Apps (or whatever you want)
    Location : http://update.rockstarapps.com/site.xml
3. expend the selection and check Rockstar JsLint
4. Click 'Next'...again
5. Accept and install
    (you'll have to hit okay when it prompts you about installing unsigned content).
6. Restart Eclipse...

资料参考

  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

博主推荐阅读

 

 

本文地址:在Eclipse中使用JSLint保证JavaScript代码质量

文章来源:梦想天空 ◆ 关注前端开发技术 ◆ 分享网页设计资源

posted @ 2012-02-20 09:03  梦想天空(山边小溪)  阅读(7796)  评论(0编辑  收藏  举报