eslint-plugin-flowtype 不验证

eslint-plugin-flowtype does not validate

我正在尝试配置 eslint + babel-eslint + eslint-plugin-react + eslint-plugin-flowtype

我在 package.json 中有以下 devDependencies

"babel-eslint": "^7.1.1",
"eslint": "^3.10.2",
"eslint-config-airbnb": "^13.0.0",
"eslint-plugin-flowtype": "^2.25.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.7.1"

以及以下.eslinrc

{
  "parser": "babel-eslint",
  "plugins": [
    "flowtype"
  ],
  "extends": [
    "airbnb",
    "plugin:flowtype/recommended"
  ],
  "rules": {
    "max-len": [1, 80, 2, {"ignoreComments": true}],
    "prop-types": [0],
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
    "react/prefer-stateless-function": [
      0,
      {
        "ignorePureComponents": true
      }
    ],
    "no-console": 0
  },
  "settings": {
    "flowtype": {
      "onlyFilesWithFlowAnnotation": true
    }
  }
}

我在App.js中写了简单的代码示例:

function foo() {
  const a: string = 1;
  return a;
}

async function bar() {
  const b = await foo();
  return b;
}

如果我启动 eslint src/App.js,那么 eslint 不会显示任何消息。 如果我将 "flowtype/require-return-type": 2 添加到 .eslintrc 中,则 eslint 显示:

error  Missing return type annotation  flowtype/require-return-type
error  Missing return type annotation  flowtype/require-return-type
✖ 2 problems (2 errors, 0 warnings)

但我不明白为什么 const a: string = 1; 有效。 如何启用检查 const a: string = 1; 的类型?

eslint-plugin-flowtypedoesn't appear to have a rule to check that

linter-plugin 的目的是加强风格或约定(例如总是注释函数 return 类型),而不是检查类型主题。

您需要 运行 Flow 本身来检查类型是否确实正确。

eslint-plugin-flowtype 不是 流。它是 ESLint 的扩展,包含一组仅与 Flow 的附加语法相关的 lint 规则。

例如,有一个 rule 可让您强制在 Flow 对象类型中使用逗号还是分号(例如 type Foo = {bar: string, baz: number}type Foo = {bar: string; baz: number}

但是,要真正进行类型检查,您需要安装 Flow, and follow the instructions there to get set up. In short, it involves making sure that you have a .flowconfig file at your project root, making sure that you have the // @flow header on all your files, and then running flow status from the command line (or using Nuclide 或其他支持 Flow 的编辑器)。

如果你想让 ESLint 使用 Flow 来验证你的代码,正确的插件是 eslint-plugin-flowtype-errors

https://www.npmjs.com/package/eslint-plugin-flowtype-errors

正如其他人所写,eslint-plugin-flowtype 仅验证带有 FlowType 语句的代码在语法上是否正确。它实际上并不进行流类型验证。