Skip to main content

懒惰模式

默认情况下,Flow 服务器将对你的所有代码进行类型检查。这样就可以回答 "我的代码中是否有任何 Flow 错误" 这样的问题了。这对于工具非常有用,例如持续集成钩子,可以防止代码更改而导致 Flow 错误。

¥By default, the Flow server will typecheck all your code. This way it can answer questions like "are there any Flow errors anywhere in my code". This is very useful for tooling, like a continuous integration hook which prevents code changes which introduce Flow errors.

然而,有时 Flow 用户可能并不关心所有代码。如果他们正在编辑文件 foo.js,他们可能只希望 Flow 对回答有关 foo.js 的问题所需的存储库子集进行类型检查。由于 Flow 只会检查较少数量的文件,因此速度会更快。这就是 Flow 的惰性模式背后的动机。

¥However, sometimes a Flow user might not care about all the code. If they are editing a file foo.js, they might only want Flow to typecheck the subset of the repository needed to answer questions about foo.js. Since Flow would only check a smaller number of files, this would be faster. This is the motivation behind Flow's lazy mode.

对文件进行分类

¥Classifying Files

惰性模式将你的代码分为四类:

¥Lazy mode classifes your code into four categories:

  1. 重点文件。这些是用户关心的文件。

    ¥Focused files. These are the files which the user cares about.

  2. 依赖文件。这些是依赖于焦点文件的文件。对焦点文件的更改可能会导致相关文件出现类型错误。

    ¥Dependent files. These are the files which depend on the focused files. Changes to the focused files might cause type errors in the dependent files.

  3. 依赖文件。这些是对重点文件或相关文件进行类型检查所需的文件。

    ¥Dependency files. These are the files which are needed in order to typecheck the focused or dependent files.

  4. 未检查的文件。所有其他文件。

    ¥Unchecked files. All other files.

惰性模式仍然会找到所有 JavaScript 文件并解析它们。但它不会对未检查的文件进行类型检查。

¥Lazy mode will still find all the JavaScript files and parse them. But it won't typecheck the unchecked files.

选择重点文件

¥Choosing Focused Files

当文件在磁盘上发生更改时,Flow 将使用 Flow 的内置文件监视器 ("查找") 或 Watchman 来关注文件。

¥Flow will focus files when they change on disk, using Flow's built-in file watcher ("dfind") or Watchman.

因此,Flow 运行时发生更改的所有文件都将受到关注。但是 Flow 未运行时发生更改的文件怎么办?如果你使用 Git 或 Mercurial,Flow 将询问自 "主要的"(当前提交和主分支的共同祖级)合并库以来已更改的所有文件。

¥So, all files that change while Flow is running will be focused. But what about files that change when Flow is not running? If you're using Git or Mercurial, Flow will ask it for all of the files that have changed since the mergebase with "master" (the common ancestor of the current commit and the master branch).

如果你不使用 "主要的"(例如,使用 "主要的"),则可以使用 file_watcher.mergebase_with 配置更改此设置。如果你使用克隆进行工作,你可能需要将其设置为 "origin/master"(对于 Git),这将集中所有在本地更改的文件,即使你提交到本地 "主要的" 分支也是如此。

¥If you're not using "master" (e.g. "main" instead), you can change this with the file_watcher.mergebase_with config. If you're working from a clone, you might want to set this to "origin/master" (for Git), which will focus all files that have changed locally, even if you commit to your local "master" branch.

最终结果是,只要上游没有错误,Flow 将在惰性模式下发现与完整检查相同的错误。例如,如果你的 CI 确保 "主要的," 中没有错误,那么 Flow 检查所有未更改的文件是否存在不可能存在的错误是多余的。

¥The net result is that Flow will find the same errors in lazy mode as in a full check, so long as there are no errors upstream. For example, if your CI ensures that there are no errors in "master," then it's redundant for Flow to check all of the unchanged files for errors that can't exist.

使用惰性模式

¥Using Lazy Mode

要启用惰性模式,请在 .flowconfig 中设置 lazy_mode=true

¥To enable lazy mode, set lazy_mode=true in the .flowconfig.

要手动以惰性模式启动 Flow,请运行

¥To start Flow in lazy mode manually, run

flow start --lazy-mode true

强制 Flow 将文件视为焦点

¥Forcing Flow to Treat a File as Focused

你可以强制 Flow 将一个或多个文件视为来自 CLI 的焦点。

¥You can force Flow to treat one or more files as focused from the CLI.

flow force-recheck --focus path/to/A.js path/to/B.js