如何使用 Closure Linter
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
注意: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
前,請先備份檔案或將檔案儲存在來源控管系統中,以免指令碼進行您不想要的變更。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003eClosure Linter, although deprecated and rarely used within Google, is a tool for identifying and fixing JavaScript style issues.\u003c/p\u003e\n"],["\u003cp\u003eIt can be installed using pip on Linux, Mac OS X, and Windows, with Windows support being experimental.\u003c/p\u003e\n"],["\u003cp\u003eClosure Linter can analyze single files or entire directories for style problems, providing detailed error messages.\u003c/p\u003e\n"],["\u003cp\u003eThe tool allows for automatic fixing of many style errors using the \u003ccode\u003efixjsstyle\u003c/code\u003e command, but it's recommended to back up files beforehand.\u003c/p\u003e\n"],["\u003cp\u003eFor stricter style checks and contributions to Closure Library, use the \u003ccode\u003e--strict\u003c/code\u003e flag.\u003c/p\u003e\n"]]],[],null,["# How to Use Closure Linter\n\nNote: Closure Linter is deprecated, and is rarely used anymore within Google.\nSee [this page](https://developers.google.com/closure/utilities/) for more information\n\n\nThis document describes how to install and use Closure Linter.\n\nInstall Closure Linter\n----------------------\n\nUse the Python `pip` utility to download and\ninstall Closure Linter.\n\n### Linux\n\nTo install Closure Linter on Linux, execute the following commands: \n\n```\n$ sudo apt-get install python-pip\n$ sudo pip install https://github.com/google/closure-linter/zipball/master\n```\n\n### Mac OS X\n\nTo install Closure Linter on Mac OS X, execute the following command: \n\n```\n$ sudo easy_install pip\n$ sudo pip install https://github.com/google/closure-linter/zipball/master\n```\n\n### Windows\n\nTo install Closure Linter on Windows:\n\n1. Download and install [Python for\n Windows.](http://www.python.org/download/windows/)\n2. Execute the following command:\n\n```\n\u003e pip install https://github.com/google/closure-linter/zipball/master\n```\n\n**Note:** Windows support for Closure Linter is\nexperimental.\n\nFind Style Problems\n-------------------\n\nTo run the Closure Linter on a single file, try: \n\n```\n$ gjslint path/to/my/file.js\n```\n\nYou should see results like this: \n\n```\nLine 46, E:0110: Line too long (87 characters).\nLine 54, E:0214: Missing description in @return tag\nLine 66, E:0012: Illegal semicolon after function declaration\nLine 150, E:0120: Binary operator should go on previous line \"+\"\nLine 175, E:0011: Missing semicolon after function assigned to a variable\nLine 210, E:0121: Illegal comma at end of array literal\nLine 220, E:0002: Missing space before \":\"\n```\n\nYou can also recursively check an entire directory, like this: \n\n```\n$ gjslint -r path/to/my/directory\n```\n\nBy default, the Closure Linter checks for the presence of correct\nJsDoc annotations. If you don't want to check for missing JsDoc\nannotations in your project, disable the check like this: \n\n```\n$ gjslint --nojsdoc -r path/to/my/directory\n```\n\nAlthough this flag disables checks for missing JsDoc annotations,\nthe linter still verifies that existing JsDoc annotations are used\ncorrectly.\n\n### The --strict Flag\n\nIf you want Closure Linter to be more strict about style rules like\nspacing, JsDoc types, and author tags, use the `--strict`\ncommand line flag. You must use `--strict` if you are contributing\ncode to the [Closure\nLibrary](/closure/library).\n\nFix Style Problems\n------------------\n\nIn any of the above commands, you can substitute `fixjsstyle` for\n`gjslint` to automatically fix many of the errors\nthat `gjslint` checks for.\n\nFor example: \n\n```\n$ fixjsstyle path/to/file1.js path/to/file2.js\n```\n\nYou should back up your files or store them in a source control\nsystem before using `fixjsstyle`, in case the script makes\nchanges that you don't want."]]