Flow type linting rules for ESLint.
- Install ESLint.
- Install
babel-eslint
parser (ESLint parser does not support type annotations). - Install
eslint-plugin-flowtype
plugin.
npm install eslint
npm install babel-eslint
npm install eslint-plugin-flowtype
- Set
parser
property tobabel-eslint
. - Add
plugins
section and specifyeslint-plugin-flowtype
as a plugin. - Enable rules.
{
"parser": "babel-eslint",
"plugins": [
"flowtype"
],
"rules": {
"flowtype/require-parameter-type": 1,
"flowtype/require-return-type": [
1,
"always",
{
"annotateUndefined": "never"
}
],
"flowtype/space-after-type-colon": [
1,
"always"
],
"flowtype/space-before-type-colon": [
1,
"never"
],
"flowtype/type-id-match": [
1,
"^([A-Z][a-z0-9]+)+Type$"
]
}
}
Requires that all function parameters have type annotations.
The following patterns are considered problems:
(foo) => {}
// Message: Missing "foo" parameter type annotation.
(foo = 'FOO') => {}
// Message: Missing "foo" parameter type annotation.
(...foo) => {}
// Message: Missing "foo" parameter type annotation.
({foo}) => {}
// Message: Missing "{foo}" parameter type annotation.
([foo]) => {}
// Message: Missing "[foo]" parameter type annotation.
({foo = 1} = {}) => {}
// Message: Missing "{foo = 1}" parameter type annotation.
The following patterns are not considered problems:
(foo: string) => {}
(foo: string = 'FOO') => {}
(...foo: string) => {}
({foo}: {foo: string}) => {}
([foo]: Array) => {}
Requires that functions have return type annotation.
The following patterns are considered problems:
(foo) => { return "foo"; }
// Message: Missing return type annotation.
// Options: ["always"]
(foo) => { return "foo"; }
// Message: Missing return type annotation.
(foo): undefined => { return; }
// Message: Must not annotate undefined return type.
(foo): undefined => { return undefined; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"never"}]
(foo): undefined => { return; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"always"}]
(foo) => { return; }
// Message: Must annotate undefined return type.
// Options: ["always",{"annotateUndefined":"never"}]
(foo): undefined => { return undefined; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"always"}]
(foo) => { return undefined; }
// Message: Must annotate undefined return type.
The following patterns are not considered problems:
(foo): string => {}
// Options: ["always"]
(foo): string => {}
(foo) => { return; }
(foo) => { return undefined; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): undefined => { return; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return undefined; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): undefined => { return undefined; }
Enforces consistent spacing after the type annotation colon.
This rule takes one argument. If it is 'always'
then a problem is raised when there is no space after the type annotation colon. If it is 'never'
then a problem is raised when there is a space after the type annotation colon. The default value is 'always'
.
The following patterns are considered problems:
// Options: ["never"]
(foo: string) => {}
// Message: There must be no space after "foo" parameter type annotation colon.
// Options: ["never"]
export default function (foo: string) {}
// Message: There must be no space after "foo" parameter type annotation colon.
// Options: ["never"]
function foo (foo: string) {}
// Message: There must be no space after "foo" parameter type annotation colon.
// Options: ["always"]
(foo:string) => {}
// Message: There must be a space after "foo" parameter type annotation colon.
// Options: ["always"]
(foo: string) => {}
// Message: There must be 1 space after "foo" parameter type annotation colon.
The following patterns are not considered problems:
(foo) => {}
(foo: string) => {}
// Options: ["never"]
(foo:string) => {}
// Options: ["always"]
(foo: string) => {}
Enforces consistent spacing before the type annotation colon.
This rule takes one argument. If it is 'always'
then a problem is raised when there is no space before the type annotation colon. If it is 'never'
then a problem is raised when there is a space before the type annotation colon. The default value is 'never'
.
The following patterns are considered problems:
// Options: ["never"]
(foo : string) => {}
// Message: There must be no space before "foo" parameter type annotation colon.
// Options: ["always"]
(foo: string) => {}
// Message: There must be a space before "foo" parameter type annotation colon.
// Options: ["always"]
(foo : string) => {}
// Message: There must be 1 space before "foo" parameter type annotation colon.
The following patterns are not considered problems:
(foo) => {}
(foo: string) => {}
// Options: ["never"]
(foo: string) => {}
// Options: ["always"]
(foo : string) => {}
Enforces a consistent naming pattern for type aliases.
This rule needs a text RegExp to operate with Its signature is as follows:
{
"rules": {
"flowtype/type-id-match": [
2,
"^([A-Z][a-z0-9]+)+Type$"
]
}
}
'^([A-Z][a-z0-9]+)+Type
is the default pattern.
The following patterns are considered problems:
type foo = {};
// Message: Type identifier 'foo' does not match pattern '/^Type([A-Z][a-z0-9]+)+$/'.
// Options: ["^foo$"]
type TypeFoo = {};
// Message: Type identifier 'TypeFoo' does not match pattern '/^foo$/'.
The following patterns are not considered problems:
type TypeFoo = {};
// Options: ["^foo$"]
type foo = {};