如何使用 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,请执行以下操作:
- 下载并安装 Python for Windows。
- 执行以下命令:
> pip install https://github.com/google/closure-linter/zipball/master
注意:Windows 对 Closure Linter 的支持尚处于实验阶段。
查找样式问题
如需对单个文件运行 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 注释的检查,但代码检查器仍会验证现有 JsDoc 注释是否正确使用。
--strict 标志
如果您希望 Closure Linter 在样式规则(例如间距、JsDoc 类型和作者标记)方面更加严格,请使用 --strict
命令行标志。如果您要为 Closure 库贡献代码,则必须使用 --strict
。
修正样式问题
在上述任何命令中,您都可以将 gjslint
替换为 fixjsstyle
,以自动修复 gjslint
检查出的许多错误。
例如:
$ fixjsstyle path/to/file1.js path/to/file2.js
在运行 fixjsstyle
之前,您应备份文件或将其存储在源代码控制系统中,以防脚本进行您不希望进行的更改。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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."]]