2016-11-23 48 views
2

我想配置eslint + babel-eslint + eslint-plugin-react + eslint-plugin-flowtypeeslint-插件,流动型不验证

devDependenciespackage.json我有以下几点:

"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.eslintrceslint显示:

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;

回答

5

eslint-plugin-flowtype不是流程。它是ESLint的一个扩展,它具有仅与Flow的附加语法相关的lint规则集合。

例如,有一个rule,可以让你执行是否使用流对象类型的逗号或者分号(例如type Foo = {bar: string, baz: number} VS type Foo = {bar: string; baz: number}

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

+0

谢谢你的答案!你知道是否可以使用'flowtype + eslint'?我的意思是,当我运行'eslint App.js'时,eslint'可能会显示'flowtype'的错误? –

+0

这是可能的,但我没有意识到已经写了一些东西来显示ESLint中的Flow结果。我个人认为这不是一个好主意。更好地按预期使用Flow并按预期使用ESLint,作为单独的工具。 –

1

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

linter插件的目的是强制风格的东西或约定的东西(如总是注释函数返回类型),而不是检查自己的类型。

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