【问题标题】:eslint-plugin-flowtype does not validateeslint-plugin-flowtype 不验证
【发布时间】:2017-04-07 16:29:24
【问题描述】:

我正在尝试配置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;的检查类型?

【问题讨论】:

    标签: javascript eslint flowtype


    【解决方案1】:

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

    linter-plugin 的目的是强制执行样式或约定(例如始终注释函数返回类型),而不是检查类型本身。

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

    【讨论】:

      【解决方案2】:

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

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

      但是,要真正进行类型检查,您需要安装 Flow,并按照那里的说明进行设置。简而言之,它涉及确保您在项目根目录中有一个.flowconfig 文件,确保所有文件上都有// @flow 标头,然后从命令行运行flow status(或使用@987654323 @ 或其他支持 Flow 的编辑器)。

      【讨论】:

      • 感谢您的回答!你知道可以使用flowtype + eslint吗?我的意思是当我运行eslint App.jseslint 会显示flowtype 的错误吗?
      • 这是可能的,但据我所知,没有人写过东西来在 ESLint 中显示 Flow 结果。个人认为这不是一个好主意。最好按预期使用 Flow 并按预期使用 ESLint,作为单独的工具。
      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2017-07-13
        • 2016-05-27
        • 2019-07-30
        • 2018-11-19
        • 1970-01-01
        • 2018-07-04
        • 1970-01-01
        • 2020-02-09
        • 2023-03-14
        相关资源
        最近更新 更多