Skip to main content

Linting 概述

Flow 包含一个 linting 框架,它可以告诉你的不仅仅是类型错误。该框架具有高度可配置性,可以向你显示你想要的信息并隐藏你不需要的信息。

¥Flow contains a linting framework that can tell you about more than just type errors. This framework is highly configurable in order to show you the information you want and hide the information you don't.

.flowconfig 中配置 Lint

¥Configuring Lints in the .flowconfig

Lint 设置可以在 .flowconfig[lints] 部分中指定为 rule=severity 对的列表。这些设置全局适用于整个项目。

¥Lint settings can be specified in the [lints] section of the .flowconfig as a list of rule=severity pairs. These settings apply globally to the entire project.


从 CLI 配置 Lint

¥Configuring Lints from the CLI

可以使用 Flow 服务器命令的 --lints 标志将 Lint 设置指定为以逗号分隔的 rule=severity 对列表。这些设置全局适用于整个项目。

¥Lint settings can be specified using the --lints flag of a Flow server command as a comma-delimited list of rule=severity pairs. These settings apply globally to the entire project.

flow start --lints "all=warn, untyped-type-import=error, sketchy-null-bool=off"

使用注释配置 Lint

¥Configuring Lints with Comments

可以使用 flowlint 注释在文件内指定 Lint 设置。这些设置适用于文件的某个区域、单行或行的一部分。欲了解更多详情,请参阅 Flowlint 注释

¥Lint settings can be specified inside a file using flowlint comments. These settings apply to a region of a file, or a single line, or part of a line. For more details see Flowlint Comments.

1// flowlint sketchy-null:error2const x: ?number = 0;3
4if (x) {} // Error5
6// flowlint-next-line sketchy-null:off7if (x) {} // No Error8
9if (x) {} /* flowlint-line sketchy-null:off */ // No Error10
11// flowlint sketchy-null:off12if (x) {} // No Error13if (x) {} // No Error

Lint 设置优先级

¥Lint Settings Precedence

flowlint 注释中的 Lint 设置具有最高优先级,其次是 --lints 标志中的 lint 规则,最后是 .flowconfig。此顺序允许你使用 flowlint 注释进行细粒度的 lint 控制,使用 --lints 标志来尝试新的 lint 设置,使用 .flowconfig 进行稳定的项目范围设置。

¥Lint settings in flowlint comments have the highest priority, followed by lint rules in the --lints flag, followed by the .flowconfig. This order allows you to use flowlint comments for fine-grained linting control, the --lints flag for trying out new lint settings, and the .flowconfig for stable project-wide settings.

--lints 标志和 .flowconfig 中,较低的规则覆盖较高的规则,允许你编写类似的内容

¥Within the --lints flag and the .flowconfig, rules lower down override rules higher up, allowing you to write things like

# warn on all sketchy-null checks
# ... except for booleans

lint 设置解析器相当智能,如果你编写冗余规则、被完全覆盖的规则或未使用的 flowlint 抑制,它会阻止你。这应该可以防止 lint 规则的大多数意外错误配置。

¥The lint settings parser is fairly intelligent and will stop you if you write a redundant rule, a rule that gets completely overwritten, or an unused flowlint suppression. This should prevent most accidental misconfigurations of lint rules.


¥Severity Levels and Meanings

离开:棉绒被忽略。将 lint 设置为 off 类似于使用抑制注释抑制类型错误,只不过粒度更细。

¥off: The lint is ignored. Setting a lint to off is similar to suppressing a type error with a suppression comment, except with much more granularity.

警告:警告是 linting 框架引入的新严重级别。它们在以下几个方面与错误的处理方式不同:

¥warn: Warnings are a new severity level introduced by the linting framework. They are treated differently than errors in a couple of ways:

  • 警告不会影响 Flow 的退出代码。如果 Flow 发现警告但没有错误,它仍然返回 0。

    ¥Warnings don't affect the exit code of Flow. If Flow finds warnings but no errors, it still returns 0.

  • 默认情况下,CLI 上不会显示警告,以避免出现错误。可以通过将 --include-warnings 标志传递到 Flow 服务器或 Flow 客户端,或者通过在 .flowconfig 中设置 include_warnings=true 来启用 CLI 警告。这对于想要立即查看所有项目警告的小型项目很有用。

    ¥Warnings aren't shown on the CLI by default, to avoid spew. CLI warnings can be enabled by passing the --include-warnings flag to the Flow server or the Flow client, or by setting include_warnings=true in the .flowconfig. This is good for smaller projects that want to see all project warnings at once.

错误:严重程度为 error 的 lint 的处理方式与任何其他 Flow 错误完全相同。

¥error: Lints with severity error are treated exactly the same as any other Flow error.