如何使用 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,请执行以下操作:

  1. 下载并安装 Python for Windows
  2. 执行以下命令:
> 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 之前,您应备份文件或将其存储在源代码控制系统中,以防脚本进行您不希望进行的更改。