在這篇blog之前寫過同類的校正JS代碼工具:JavaScript Lint 的在 Notepad++ 中如何使用,google 提供的 Closure Linter 給了我們更多的工具選擇。
Closure Linter是在Python上啟動並執行,所以先在系統中配置安裝Python的運行環境:
- 安裝 Python。本人安裝的版本為 Python 2.5.4
- 安裝 Easy Install 模組。也就是要安裝 setuptools windows下本人安裝的為: setuptools-0.6c11.win32-py2.5.exe
- 確保 Easy Install 配置到Path環境變數。
然後,在命令列中執行如下命令:
easy_install E:\closure_linter\closure_linter-2-1.2.6.tar.gz
如果出現啪啦啪啦如下的畫面:
則說明Closure Linter 安裝成功!OK,在Notepad++ 的NppExec執行如下命令:
gjslint --strict $(FULL_CURRENT_PATH)
可以將這個命令儲存為 Closure Linter。點擊“OK”試試,如果在控制台視窗輸出如下結果:
gjslint –-strict D:\work\js\WOYO\event.jsProcess started >>>----- FILE : D:\work\js\WOYO\event.js -----Line 3, E:0217: Missing @return JsDoc in function with non-trivial returnLine 4, E:0002: Missing space before "{"Line 5, E:0005: Illegal tab in whitespace before "if"Line 5, E:0002: Missing space before "("Line 6, E:0005: Illegal tab in whitespace before "return"Line 7, E:0005: Illegal tab in whitespace before "}"Line 8, E:0005: Illegal tab in whitespace before "else"Line 8, E:0002: Missing space before "("Line 9, E:0005: Illegal tab in whitespace before "var"Line 9, E:0002: Missing space before "="Line 9, E:0002: Missing space before "["Line 9, E:0131: Single-quoted string preferred over double-quoted string.Line 9, E:0131: Single-quoted string preferred over double-quoted string.Line 9, E:0131: Single-quoted string preferred over double-quoted string.Line 10, E:0005: Illegal tab in whitespace before ""Line 10, E:0001: Extra space at end of lineLine 11, E:0005: Illegal tab in whitespace before "for"Line 11, E:0002: Missing space before "("Line 11, E:0002: Missing space before "="Line 11, E:0002: Missing space after "="Line 11, E:0002: Missing space before "<"Line 11, E:0002: Missing space after "<"Line 12, E:0005: Illegal tab in whitespace before "try"Line 13, E:0005: Illegal tab in whitespace before "var"Line 14, E:0005: Illegal tab in whitespace before "return"Line 15, E:0005: Illegal tab in whitespace before "}"Line 16, E:0005: Illegal tab in whitespace before "catch"Line 16, E:0002: Missing space before "("Line 16, E:0002: Missing space before "{"Line 17, E:0005: Illegal tab in whitespace before "//"Line 18, E:0005: Illegal tab in whitespace before "}"Line 19, E:0005: Illegal tab in whitespace before "}"Line 20, E:0005: Illegal tab in whitespace before "}"Line 21, E:0005: Illegal tab in whitespace before "throw"Line 21, E:0131: Single-quoted string preferred over double-quoted string.Line 24, E:0220: No docs found for member 'Ajax.Request'Line 24, E:0002: Missing space before "{"Line 25, E:0005: Illegal tab in whitespace before "var"Line 26, E:0005: Illegal tab in whitespace before "var"Line 27, E:0005: Illegal tab in whitespace before "if"Line 27, E:0002: Missing space before "("Line 28, E:0005: Illegal tab in whitespace before "var"Line 28, E:0001: Extra space at end of lineLine 29, E:0005: Illegal tab in whitespace before "if"Line 29, E:0002: Missing space before "("Line 30, E:0005: Illegal tab in whitespace before "requestType"Line 31, E:0005: Illegal tab in whitespace before "}"Line 32, E:0005: Illegal tab in whitespace before "var"Line 33, E:0005: Illegal tab in whitespace before "req.open"Line 34, E:0005: Illegal tab in whitespace before "req.onreadystatechange"Line 35, E:0005: Illegal tab in whitespace before "if"Line 36, E:0005: Illegal tab in whitespace before "callbackData"Line 36, E:0002: Missing space after "+"Line 36, E:0002: Missing space before "+"Line 37, E:0005: Illegal tab in whitespace before "}"Line 38, E:0005: Illegal tab in whitespace before "}"Line 39, E:0005: Illegal tab in whitespace before "this.onSuccess"Line 39, E:0002: Missing space before "{"Line 41, E:0002: Missing space before "{"Line 42, E:0005: Illegal tab in whitespace before "$"Line 42, E:0002: Missing space before "{"Line 43, E:0005: Illegal tab in whitespace before "var"Line 44, E:0005: Illegal tab in whitespace before "url"Line 45, E:0005: Illegal tab in whitespace before "dataType"Line 46, E:0005: Illegal tab in whitespace before "}"Line 47, E:0005: Illegal tab in whitespace before "req.onSuccess"Line 47, E:0002: Missing space before "{"Line 48, E:0005: Illegal tab in whitespace before ""Line 48, E:0001: Extra space at end of lineLine 49, E:0005: Illegal tab in whitespace before "}"Line 50, E:0005: Illegal tab in whitespace before "$.ajax"Line 51, E:0005: Illegal tab in whitespace before "url"Line 52, E:0005: Illegal tab in whitespace before "dataType"Line 53, E:0005: Illegal tab in whitespace before "success"Line 54, E:0005: Illegal tab in whitespace before "$"Line 55, E:0005: Illegal tab in whitespace before "}"Line 56, E:0005: Illegal tab in whitespace before "}"Line 57, E:0005: Illegal tab in whitespace before "return"Line 58, E:0005: Illegal tab in whitespace before "}"Line 66, E:0210: Missing docs for parameter: "file"Line 66, E:0210: Missing docs for parameter: "postData"Line 67, E:0005: Illegal tab in whitespace before "var"Line 68, E:0005: Illegal tab in whitespace before "if"Line 68, E:0002: Missing space before "("Line 69, E:0005: Illegal tab in whitespace before "req.onreadystatechange"Line 70, E:0005: Illegal tab in whitespace before "if"Line 71, E:0005: Illegal tab in whitespace before "if"Line 72, E:0005: Illegal tab in whitespace before "alert"Line 73, E:0005: Illegal tab in whitespace before "return"Line 74, E:0005: Illegal tab in whitespace before "}"Line 75, E:0005: Illegal tab in whitespace before ""Line 75, E:0001: Extra space at end of lineLine 76, E:0005: Illegal tab in whitespace before "updateobj"Line 76, E:0110: Line too long (94 characters).Line 77, E:0005: Illegal tab in whitespace before "}"Line 78, E:0005: Illegal tab in whitespace before "req.open"Line 79, E:0005: Illegal tab in whitespace before "req.setRequestHeader"Line 79, E:0002: Missing space after ","Line 80, E:0005: Illegal tab in whitespace before "if"Line 81, E:0005: Illegal tab in whitespace before "req.send"Line 84, E:0005: Illegal tab in whitespace before "document.getElementsByTagName"Line 88, E:0005: Illegal tab in whitespace before "function"Line 89, E:0005: Illegal tab in whitespace before "function"Line 89, E:0001: Extra space after "function"Line 89, E:0131: Single-quoted string preferred over double-quoted string.Line 90, E:0005: Illegal tab in whitespace before "function"Line 90, E:0001: Extra space after "function"Line 90, E:0131: Single-quoted string preferred over double-quoted string.Line 91, E:0005: Illegal tab in whitespace before "function"Line 91, E:0001: Extra space after "function"Line 91, E:0131: Single-quoted string preferred over double-quoted string.Line 95, E:0005: Illegal tab in whitespace before "var"Line 96, E:0005: Illegal tab in whitespace before "for"Line 96, E:0002: Missing space before "("Line 96, E:0002: Missing space before "="Line 96, E:0002: Missing space after "="Line 96, E:0002: Missing space before "<"Line 96, E:0002: Missing space after "<"Line 97, E:0005: Illegal tab in whitespace before "try"Line 98, E:0005: Illegal tab in whitespace before "xmlhttp"Line 99, E:0005: Illegal tab in whitespace before "}"Line 100, E:0005: Illegal tab in whitespace before "catch"Line 100, E:0002: Missing space before "("Line 101, E:0005: Illegal tab in whitespace before "continue"Line 102, E:0005: Illegal tab in whitespace before "}"Line 103, E:0005: Illegal tab in whitespace before "break"Line 104, E:0005: Illegal tab in whitespace before "}"Line 105, E:0005: Illegal tab in whitespace before "return"Line 106, E:0300: File does not end with new line. (})Found 129 errors, including 0 new errors, in 1 files (0 files OK).Some of the errors reported by GJsLint may be auto-fixable using the scriptfixjsstyle. Please double check any changes it makes and report any bugs. Thescript can be run by executing:fixjsstyle –-strict D:\work\js\WOYO\event.js<<< Process finished.================ READY ================
則說明JS代碼已經被 Closure Linter工具校正。居然這麼多 bug!不過,修複 bug 也可以利用工具來完成,哈哈!執行如下的命令即可:
fixjsstyle –-strict $(FULL_CURRENT_PATH)
在Console視窗中顯示的結果:
fixjsstyle –-strict D:\work\js\WOYO\event.jsProcess started >>>Fixed 44 errors in D:\work\js\WOYO\event.js<<< Process finished.================ READY ================
129個bug修複了44個!因為選擇了strict參數,果然很嚴苛!哈哈!
(完)