Skip to main content

在你的项目中启用枚举

升级工具

¥Upgrade tooling

要在存储库中启用 Flow Enums,你必须首先更新以下软件包:

¥To enable Flow Enums in your repo, you must first update the following packages:

  • 至少升级到 Flow 0.159

    ¥Upgrade to at least Flow 0.159

    • 流程需要设置一些配置才能启用枚举 - 见下文。

      ¥Flow needs to have some configuration set to enable enums - see below.

  • Prettier 升级到至少版本 2.2

    ¥Upgrade Prettier to at least version 2.2

    • 从该版本开始,Prettier 可以处理开箱即用的解析和漂亮打印 Flow 枚举。

      ¥As of that version, Prettier can handle parsing and pretty printing Flow Enums out of the box.

    • 你必须使用 JavaScript 文件的 flow 解析器选项 才能格式化 Flow 枚举。

      ¥You must use the flow parser option for JavaScript files to be able to format Flow Enums.

  • Babel 升级到至少版本 7.13.0

    ¥Upgrade Babel to at least version 7.13.0

    • 从该版本开始,Babel 可以解析 Flow Enum。但是,为了启用此解析,需要提供一些配置,而且它不包括所需的转换 - 见下文。

      ¥As of that version, Babel can parse Flow Enums. However, to enable this parsing some configuration needs to be supplied, and additionally it does not include the transform required - see below.

  • jscodeshift 升级到至少版本 0.11.0

    ¥Upgrade jscodeshift to at least version 0.11.0

  • hermes-parser 升级到至少版本 0.4.8

    ¥Upgrade hermes-parser to at least version 0.4.8

  • 对于 ESLint,可以:

    ¥For ESLint, either:

    • 使用 hermes-eslint 作为 ESLint 解析器,至少版本 0.4.8

      ¥Use hermes-eslint as your ESLint parser, at least version 0.4.8

    • 或者将 babel-eslint 升级到 10.1.0 版本

      ¥Or upgrade babel-eslint to version 10.1.0

      • 从该版本开始,babel-eslint 可以开箱即用地处理 Flow 枚举。

        ¥As of that version, babel-eslint can handle Flow Enums out of the box.

      • 不要升级到 11.x,该分支不支持 Flow Enums。

        ¥Do not upgrade to 11.x, this branch does not support Flow Enums.

    • 或者使用 Babel 7.13.0 或更高版本并启用 Flow 的其他解决方案 - 这也可能有效

      ¥Or use another solution using Babel 7.13.0 or later, with Flow enabled - this may also work

如果你有任何其他工具来检查你的代码,你也需要更新它。如果使用 flow-parserhermes-parser@babel/parser,请按照上述说明进行升级。如果它使用其他解析器,你将需要在该解析器中实现解析 Flow 枚举。你可以查看 Babel、Flow 和 Hermes 解析器中的现有代码来指导你的工作。

¥If you have any other tool which examines your code, you need to update it as well. If it uses flow-parser, hermes-parser or @babel/parser, upgrade those as per the instructions above. If it uses some other parser, you will need to implement parsing Flow Enums in that parser. You can look at the existing code in Babel, Flow, and Hermes parsers to guide your work.

启用枚举

¥Enable enums

  • .flowconfig[options] 标题下,添加 enums=true

    ¥In your .flowconfig, under the [options] heading, add enums=true

  • 添加 Flow Enums Babel 转换。它将枚举声明 AST 节点转换为对运行时的调用:babel-plugin-transform-flow-enums。将其添加到你的开发依赖中并调整 Babel 配置以使用转换。默认情况下,转换直接需要运行时包(如下),但你可以对此进行配置。

    ¥Add the Flow Enums Babel transform. It turns enum declaration AST nodes into calls to the runtime: babel-plugin-transform-flow-enums. Add it to your development dependencies and adjust your Babel configuration to use the transform. The transform by default requires the runtime package directly (below), but you can configure this.

  • 将 Flow Enum 运行时包添加到你的生产依赖中。这将是必需的,并在运行时使用它来创建 Flow 枚举:flow-enums-runtime

    ¥Add the Flow Enum runtime package to your production dependencies. This will be required and used at runtime to create Flow Enums: flow-enums-runtime

启用建议的 ESLint 规则

¥Enable suggested ESLint rules

可以在 switch 语句中彻底检查枚举,因此与以前相比可能会增加 switch 语句的使用。为了防止 switch 语句出现常见问题,我们建议你启用这些 ESLint 规则(至少作为警告):

¥Enums can be exhaustively checked in switch statements, so may increase the use of switch statements compared to before. To prevent common issues with switch statements, we suggest you enable these ESLint rules (at least as warnings):

  • no-fallthrough:这可以防止用户意外忘记 switch case 末尾的 break 语句,同时支持常见用例。

    ¥no-fallthrough: This prevents the user from accidentally forgetting a break statement at the end of their switch case, while supporting common use-cases.

  • no-case-declarations:这可以防止在 switch case 中引入词法作用域声明 (let, const),而不将该 case 封装在新块中。否则,不同情况下的声明可能会发生冲突。

    ¥no-case-declarations: This prevents lexicaly scoped declarations (let, const) from being introduced in a switch case, without wrapping that case in a new block. Otherwise, declarations in different cases could conflict.

作为 eslint-plugin-fb-flow 的一部分,我们还有一些 Flow 枚举特定规则:

¥We also have some Flow Enums specific rules as part of eslint-plugin-fb-flow: