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
博主推薦閱讀
- Web開發人員和設計師必讀文章推薦【系列一】
- Web開發人員和設計師必讀文章推薦【系列二】
- Web開發人員和設計師必讀文章推薦【系列三】
- Web開發人員和設計師必讀文章推薦【系列四】
- Web開發人員和設計師必讀文章推薦【系列五】
- Web開發人員和設計師必讀文章推薦【系列六】