在 Notepad++ 运行 Closure Linter 来校验JS代码
在这篇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.js Process started >>> ----- FILE : D:\work\js\WOYO\event.js ----- Line 3, E:0217: Missing @return JsDoc in function with non-trivial return Line 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 line Line 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 line Line 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 line Line 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 line Line 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 script fixjsstyle. Please double check any changes it makes and report any bugs. The script 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.js Process started >>> Fixed 44 errors in D:\work\js\WOYO\event.js <<< Process finished. ================ READY ================
129个bug修复了44个!因为选择了strict参数,果然很严苛!哈哈!
(完)