2016-11-23 19 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本身來檢查類型是否真的正確。