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
显示:
If I launch eslint src/App.js
then eslint
does not show any messages.
If I add "flowtype/require-return-type": 2
into .eslintrc
then eslint
shows:
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;
?
But I don't understand why const a: string = 1;
is valid.
How to enable checking type of const a: string = 1;
?
推荐答案
eslint-plugin-flowtype
不流量。它是ESLint的扩展,它具有一系列仅与Flow的附加语法相关的lint规则。
eslint-plugin-flowtype
is not Flow. It is an extension to ESLint that has a collection of lint rules relevant only to Flow's additional syntax.
例如,有一个规则,允许您强制使用流对象类型中的逗号或分号(例如类型Foo = {bar:string,baz:number}
vs type Foo = {bar:string; baz:number}
)
For example, there is a rule that lets you enforce whether to use commas or semicolons in Flow object types (e.g. type Foo = {bar: string, baz: number}
vs type Foo = {bar: string; baz: number}
)
然而,要实际获得类型检查,您需要安装 Flow ,并按照那里的说明进行设置。简而言之,它涉及确保您的项目根目录中有 .flowconfig
文件,确保您拥有 // @flow 所有文件的code>标题,然后从命令行运行
流状态
(或使用 Nuclide 或其他具有Flow支持的编辑器。
However, to actually get typechecking you need to install 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 or another editor with Flow support).
这篇关于eslint-plugin-flowtype不验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!