注意:Closure Linter 已淘汰,Google 內部已很少使用。 詳情請參閱這個頁面
本文說明如何安裝及使用 Closure Linter。
安裝 Closure Linter
使用 Python pip
公用程式下載及安裝 Closure Linter。
Linux
如要在 Linux 上安裝 Closure Linter,請執行下列指令:
$ sudo apt-get install python-pip $ sudo pip install https://github.com/google/closure-linter/zipball/master
Mac OS X
如要在 Mac OS X 上安裝 Closure Linter,請執行下列指令:
$ sudo easy_install pip $ sudo pip install https://github.com/google/closure-linter/zipball/master
Windows
如要在 Windows 上安裝 Closure Linter,請按照下列步驟操作:
- 下載並安裝 Windows 版 Python。
- 執行下列指令:
> pip install https://github.com/google/closure-linter/zipball/master
注意:Closure Linter 的 Windows 支援功能仍在實驗階段。
找出樣式問題
如要在單一檔案上執行 Closure Linter,請嘗試:
$ gjslint path/to/my/file.js
預期會得到以下結果:
Line 46, E:0110: Line too long (87 characters). Line 54, E:0214: Missing description in @return tag Line 66, E:0012: Illegal semicolon after function declaration Line 150, E:0120: Binary operator should go on previous line "+" Line 175, E:0011: Missing semicolon after function assigned to a variable Line 210, E:0121: Illegal comma at end of array literal Line 220, E:0002: Missing space before ":"
您也可以遞迴檢查整個目錄,如下所示:
$ gjslint -r path/to/my/directory
根據預設,Closure Linter 會檢查是否有正確的 JsDoc 註解。如果不想檢查專案中缺少的 JsDoc 註解,請停用檢查功能,如下所示:
$ gjslint --nojsdoc -r path/to/my/directory
雖然這個標記會停用缺少的 JsDoc 註解檢查,但 Linter 仍會驗證現有的 JsDoc 註解是否正確使用。
--strict 標記
如要讓 Closure Linter 更嚴格地執行樣式規則 (例如間距、JsDoc 型別和作者標記),請使用 --strict
指令列旗標。如果您要為 Closure 程式庫提供程式碼,就必須使用 --strict
。
修正樣式問題
在上述任一指令中,您都可以將 fixjsstyle
替換為 gjslint
,自動修正 gjslint
檢查出的許多錯誤。
例如:
$ fixjsstyle path/to/file1.js path/to/file2.js
使用 fixjsstyle
前,請先備份檔案或將檔案儲存在來源控管系統中,以免指令碼進行您不想要的變更。