eslint-config-chain-able
Version:
an opinionated ESLint configuration
2,068 lines (1,478 loc) • 94.6 kB
Markdown
<a name="eslint-plugin-flowtype"></a>
# eslint-plugin-flowtype
[](https://www.npmjs.org/package/eslint-plugin-flowtype)
[](https://travis-ci.org/gajus/eslint-plugin-flowtype)
[](https://github.com/gajus/canonical)
[Flow type](http://flowtype.org/) linting rules for ESLint.
* [eslint-plugin-flowtype](#eslint-plugin-flowtype)
* [Installation](#eslint-plugin-flowtype-installation)
* [Configuration](#eslint-plugin-flowtype-configuration)
* [Shareable configurations](#eslint-plugin-flowtype-configuration-shareable-configurations)
* [Settings](#eslint-plugin-flowtype-settings)
* [`onlyFilesWithFlowAnnotation`](#eslint-plugin-flowtype-settings-onlyfileswithflowannotation)
* [Rules](#eslint-plugin-flowtype-rules)
* [`boolean-style`](#eslint-plugin-flowtype-rules-boolean-style)
* [`define-flow-type`](#eslint-plugin-flowtype-rules-define-flow-type)
* [`delimiter-dangle`](#eslint-plugin-flowtype-rules-delimiter-dangle)
* [`generic-spacing`](#eslint-plugin-flowtype-rules-generic-spacing)
* [`no-dupe-keys`](#eslint-plugin-flowtype-rules-no-dupe-keys)
* [`no-primitive-constructor-types`](#eslint-plugin-flowtype-rules-no-primitive-constructor-types)
* [`no-types-missing-file-annotation`](#eslint-plugin-flowtype-rules-no-types-missing-file-annotation)
* [`no-weak-types`](#eslint-plugin-flowtype-rules-no-weak-types)
* [`object-type-delimiter`](#eslint-plugin-flowtype-rules-object-type-delimiter)
* [`require-parameter-type`](#eslint-plugin-flowtype-rules-require-parameter-type)
* [`require-return-type`](#eslint-plugin-flowtype-rules-require-return-type)
* [`require-valid-file-annotation`](#eslint-plugin-flowtype-rules-require-valid-file-annotation)
* [`require-variable-type`](#eslint-plugin-flowtype-rules-require-variable-type)
* [`semi`](#eslint-plugin-flowtype-rules-semi)
* [`sort-keys`](#eslint-plugin-flowtype-rules-sort-keys)
* [`space-after-type-colon`](#eslint-plugin-flowtype-rules-space-after-type-colon)
* [`space-before-generic-bracket`](#eslint-plugin-flowtype-rules-space-before-generic-bracket)
* [`space-before-type-colon`](#eslint-plugin-flowtype-rules-space-before-type-colon)
* [`type-id-match`](#eslint-plugin-flowtype-rules-type-id-match)
* [`union-intersection-spacing`](#eslint-plugin-flowtype-rules-union-intersection-spacing)
* [`use-flow-type`](#eslint-plugin-flowtype-rules-use-flow-type)
* [`valid-syntax`](#eslint-plugin-flowtype-rules-valid-syntax)
<a name="eslint-plugin-flowtype-installation"></a>
## Installation
1. Install [ESLint](https://www.github.com/eslint/eslint).
1. Install [`babel-eslint`](https://github.com/babel/babel-eslint) parser (ESLint parser [does not support type annotations](https://github.com/eslint/eslint/issues/2157)).
1. Install [`eslint-plugin-flowtype`](https://github.com/gajus/eslint-plugin-flowtype) plugin.
<!-- -->
```sh
npm install eslint --save-dev
npm install babel-eslint --save-dev
npm install eslint-plugin-flowtype --save-dev
```
<a name="eslint-plugin-flowtype-configuration"></a>
## Configuration
1. Set `parser` property to `babel-eslint`.
1. Add `plugins` section and specify `eslint-plugin-flowtype` as a plugin.
1. Enable rules.
<!-- -->
```json
{
"parser": "babel-eslint",
"plugins": [
"flowtype"
],
"rules": {
"flowtype/boolean-style": [
2,
"boolean"
],
"flowtype/define-flow-type": 1,
"flowtype/delimiter-dangle": [
2,
"never"
],
"flowtype/generic-spacing": [
2,
"never"
],
"flowtype/no-primitive-constructor-types": 2,
"flowtype/no-types-missing-file-annotation": 2,
"flowtype/no-weak-types": 2,
"flowtype/object-type-delimiter": [
2,
"comma"
],
"flowtype/require-parameter-type": 2,
"flowtype/require-return-type": [
2,
"always",
{
"annotateUndefined": "never"
}
],
"flowtype/require-valid-file-annotation": 2,
"flowtype/semi": [
2,
"always"
],
"flowtype/space-after-type-colon": [
2,
"always"
],
"flowtype/space-before-generic-bracket": [
2,
"never"
],
"flowtype/space-before-type-colon": [
2,
"never"
],
"flowtype/type-id-match": [
2,
"^([A-Z][a-z0-9]+)+Type$"
],
"flowtype/union-intersection-spacing": [
2,
"always"
],
"flowtype/use-flow-type": 1,
"flowtype/valid-syntax": 1
},
"settings": {
"flowtype": {
"onlyFilesWithFlowAnnotation": false
}
}
}
```
<a name="eslint-plugin-flowtype-configuration-shareable-configurations"></a>
### Shareable configurations
<a name="eslint-plugin-flowtype-configuration-shareable-configurations-recommended"></a>
#### Recommended
This plugin exports a [recommended configuration](./src/configs/recommended.json) that enforces Flow type good practices.
To enable this configuration use the extends property in your `.eslintrc` config file:
```json
{
"extends": [
"plugin:flowtype/recommended"
],
"plugins": [
"flowtype"
]
}
```
See [ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files) for more information about extending configuration files.
<a name="eslint-plugin-flowtype-settings"></a>
## Settings
<a name="eslint-plugin-flowtype-settings-onlyfileswithflowannotation"></a>
### <code>onlyFilesWithFlowAnnotation</code>
When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/docs/about-flow.html#gradual) in the first comment.
```js
{
"settings": {
"flowtype": {
"onlyFilesWithFlowAnnotation": true
}
}
}
```
<a name="eslint-plugin-flowtype-rules"></a>
## Rules
<!-- Rules are sorted alphabetically. -->
<a name="eslint-plugin-flowtype-rules-boolean-style"></a>
### <code>boolean-style</code>
_The `--fix` option on the command line automatically fixes problems reported by this rule._
Enforces a particular style for boolean type annotations. This rule takes one argument.
If it is `'boolean'` then a problem is raised when using `bool` instead of `boolean`.
If it is `'bool'` then a problem is raised when using `boolean` instead of `bool`.
The default value is `'boolean'`.
The following patterns are considered problems:
```js
type X = bool
// Message: Use "boolean", not "bool"
// Options: ["boolean"]
type X = bool
// Message: Use "boolean", not "bool"
// Options: ["bool"]
type X = boolean
// Message: Use "bool", not "boolean"
```
The following patterns are not considered problems:
```js
type X = boolean
// Options: ["boolean"]
type X = boolean
// Options: ["bool"]
type X = bool
// Options: ["boolean"]
type X = bool
```
<a name="eslint-plugin-flowtype-rules-define-flow-type"></a>
### <code>define-flow-type</code>
Marks Flow type identifiers as defined.
Used to suppress [`no-undef`](http://eslint.org/docs/rules/no-undef) reporting of type identifiers.
The following patterns are not considered problems:
```js
var a: AType
// Additional rules: {"no-undef":2}
var a: AType; var b: AType
// Additional rules: {"no-undef":2}
var a; (a: AType)
// Additional rules: {"no-undef":2}
var a: AType<BType>
// Additional rules: {"no-undef":2}
type A = AType
// Additional rules: {"no-undef":2}
function f(a: AType) {}
// Additional rules: {"no-undef":2}
function f(a: AType.a) {}
// Additional rules: {"no-undef":2}
function f(a: AType.a.b) {}
// Additional rules: {"no-undef":2}
function f(a): AType {}; var a: AType
// Additional rules: {"no-undef":2}
function f(a): AType {}
// Additional rules: {"no-undef":2}
class C { a: AType }
// Additional rules: {"no-undef":2}
class C { a: AType.a }
// Additional rules: {"no-undef":2}
class C { a: AType.a.b }
// Additional rules: {"no-undef":2}
class C implements AType {}
// Additional rules: {"no-undef":2}
interface AType {}
// Additional rules: {"no-undef":2}
({ a: ({b() {}}: AType) })
// Additional rules: {"no-undef":2}
type X = {Y<AType>(): BType}
// Additional rules: {"no-undef":2}
interface AType<BType> {}
// Additional rules: {"no-undef":2}
var a: AType
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
var a: AType; var b: AType
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
var a; (a: AType)
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
var a: AType<BType>
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
type A = AType
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
function f(a: AType) {}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
function f(a: AType.a) {}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
function f(a: AType.a.b) {}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
function f(a): AType {}; var a: AType
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
function f(a): AType {}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
class C { a: AType }
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
class C { a: AType.a }
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
class C { a: AType.a.b }
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
class C implements AType {}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
interface AType {}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
({ a: ({b() {}}: AType) })
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
type X = {Y<AType>(): BType}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
interface AType<BType> {}
// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
```
<a name="eslint-plugin-flowtype-rules-delimiter-dangle"></a>
### <code>delimiter-dangle</code>
_The `--fix` option on the command line automatically fixes problems reported by this rule._
Enforces consistent use of trailing commas in Object and Tuple annotations.
This rule takes one argument which mirrors ESLint's default `comma-dangle` rule.
If it is `'never'` then a problem is raised when there is a trailing comma.
If it is `'always'` then a problem is raised when there is no trailing comma.
If it is `'always-multiline'` then a problem is raised when there is no trailing comma on a multi-line definition, or there _is_ a trailing comma on a single-line definition.
If it is `'only-multiline'` then a problem is raised when there is a trailing comma on a single-line definition. It allows, but does not enforce, trailing commas on multi-line definitions.
The default value is `'never'`.
The following patterns are considered problems:
```js
type X = { foo: string, }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = { foo: string, }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = { foo: string; }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = {
foo: string,
}
// Message: Unexpected trailing delimiter
// Options: ["always"]
type X = { foo: string }
// Message: Missing trailing delimiter
// Options: ["always"]
type X = {
foo: string
}
// Message: Missing trailing delimiter
// Options: ["always-multiline"]
type X = { foo: string, }
// Message: Unexpected trailing delimiter
// Options: ["always-multiline"]
type X = {
foo: string
}
// Message: Missing trailing delimiter
// Options: ["only-multiline"]
type X = { foo: string; }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = { [key: string]: number, }
// Message: Unexpected trailing delimiter
// Options: ["always"]
type X = { [key: string]: number }
// Message: Missing trailing delimiter
// Options: ["always-multiline"]
type X = { [key: string]: number, }
// Message: Unexpected trailing delimiter
// Options: ["always-multiline"]
type X = {
[key: string]: number
}
// Message: Missing trailing delimiter
// Options: ["only-multiline"]
type X = { [key: string]: number; }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = { [key: string]: number, foo: string, }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = {
[key: string]: number,
foo: string,
}
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = {
[key: string]: number,
aReallyLongPropertyNameHere: string,
}
// Message: Unexpected trailing delimiter
// Options: ["always"]
type X = { [key: string]: number, foo: string }
// Message: Missing trailing delimiter
// Options: ["always"]
type X = {
[key: string]: number;
foo: string
}
// Message: Missing trailing delimiter
// Options: ["always-multiline"]
type X = { [key: string]: number, foo: string, }
// Message: Unexpected trailing delimiter
// Options: ["always-multiline"]
type X = {
[key: string]: number,
foo: string
}
// Message: Missing trailing delimiter
// Options: ["only-multiline"]
type X = { [key: string]: number, foo: string, }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = { foo: string, [key: string]: number, }
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = {
foo: string,
[key: string]: number,
}
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = {
aReallyLongPropertyNameHere: string,
[key: string]: number,
}
// Message: Unexpected trailing delimiter
// Options: ["always"]
type X = { foo: string, [key: string]: number }
// Message: Missing trailing delimiter
// Options: ["always"]
type X = { foo: string; [key: string]: number }
// Message: Missing trailing delimiter
// Options: ["always-multiline"]
type X = { foo: string, [key: string]: number; }
// Message: Unexpected trailing delimiter
// Options: ["always-multiline"]
type X = {
foo: string,
[key: string]: number
}
// Message: Missing trailing delimiter
// Options: ["only-multiline"]
type X = { foo: string, [key: string]: number; }
// Message: Unexpected trailing delimiter
type X = [string, number,]
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = [string, number,]
// Message: Unexpected trailing delimiter
// Options: ["never"]
type X = [
string,
number,
]
// Message: Unexpected trailing delimiter
// Options: ["always"]
type X = [string, number]
// Message: Missing trailing delimiter
// Options: ["always"]
type X = [
string,
number
]
// Message: Missing trailing delimiter
// Options: ["always-multiline"]
type X = [string, number,]
// Message: Unexpected trailing delimiter
// Options: ["always-multiline"]
type X = [
foo, string
]
// Message: Missing trailing delimiter
// Options: ["only-multiline"]
type X = [ number, string, ]
// Message: Unexpected trailing delimiter
```
The following patterns are not considered problems:
```js
type X = { foo: string }
// Options: ["never"]
type X = { foo: string }
// Options: ["always"]
type X = { foo: string, }
// Options: ["always"]
type X = { foo: string; }
// Options: ["never"]
type X = {
foo: string
}
// Options: ["always"]
type X = {
foo: string,
}
// Options: ["always-multiline"]
type X = { foo: string }
// Options: ["always-multiline"]
type X = {
foo: string,
}
// Options: ["always-multiline"]
type X = {
foo: string;
}
// Options: ["only-multiline"]
type X = { foo: string }
// Options: ["only-multiline"]
type X = {
foo: string
}
// Options: ["only-multiline"]
type X = {
foo: string,
}
// Options: ["only-multiline"]
type X = {
foo: string;
}
// Options: ["never"]
type X = {}
// Options: ["always"]
type X = {}
// Options: ["always-multiline"]
type X = {}
// Options: ["only-multiline"]
type X = {}
// Options: ["never"]
type X = { [key: string]: number }
// Options: ["always"]
type X = { [key: string]: number, }
// Options: ["always"]
type X = { [key: string]: number; }
// Options: ["always-multiline"]
type X = { [key: string]: number }
// Options: ["always-multiline"]
type X = {
[key: string]: number,
}
// Options: ["only-multiline"]
type X = {
[key: string]: number,
}
// Options: ["only-multiline"]
type X = {
[key: string]: number
}
// Options: ["only-multiline"]
type X = { [key: string]: number }
// Options: ["never"]
type X = { [key: string]: number, foo: string }
// Options: ["always"]
type X = { [key: string]: number, foo: string, }
// Options: ["always"]
type X = { [key: string]: number; foo: string; }
// Options: ["always-multiline"]
type X = { [key: string]: number, foo: string }
// Options: ["always-multiline"]
type X = {
[key: string]: number,
foo: string,
}
// Options: ["only-multiline"]
type X = {
[key: string]: number,
foo: string,
}
// Options: ["only-multiline"]
type X = {
[key: string]: number;
foo: string
}
// Options: ["only-multiline"]
type X = { [key: string]: number, foo: string }
// Options: ["never"]
type X = { foo: string, [key: string]: number }
// Options: ["always"]
type X = { foo: string, [key: string]: number, }
// Options: ["always"]
type X = { foo: string; [key: string]: number; }
// Options: ["always-multiline"]
type X = { foo: string, [key: string]: number }
// Options: ["always-multiline"]
type X = {
foo: string,
[key: string]: number,
}
// Options: ["only-multiline"]
type X = {
foo: string,
[key: string]: number,
}
// Options: ["only-multiline"]
type X = {
foo: string;
[key: string]: number
}
// Options: ["only-multiline"]
type X = { foo: string, [key: string]: number }
type X = [string, number]
// Options: ["never"]
type X = [string, number]
// Options: ["never"]
type X = [
string,
number
]
// Options: ["always"]
type X = [string, number,]
// Options: ["always"]
type X = [
string,
number,
]
// Options: ["always-multiline"]
type X = [ foo, string ]
// Options: ["always-multiline"]
type X = [
foo, string,
]
// Options: ["only-multiline"]
type X = [ number, string ]
// Options: ["only-multiline"]
type X = [
number,
string
]
// Options: ["only-multiline"]
type X = [
number,
string,
]
// Options: ["never"]
type X = []
// Options: ["always"]
type X = []
// Options: ["always-multiline"]
type X = []
// Options: ["only-multiline"]
type X = []
```
<a name="eslint-plugin-flowtype-rules-generic-spacing"></a>
### <code>generic-spacing</code>
_The `--fix` option on the command line automatically fixes problems reported by this rule._
Enforces consistent spacing within generic type annotation parameters.
This rule takes one argument. If it is `'never'` then a problem is raised when there is a space surrounding the generic type parameters. If it is `'always'` then a problem is raised when there is no space surrounding the generic type parameters.
The default value is `'never'`.
The following patterns are considered problems:
```js
type X = Promise< string>
// Message: There must be no space at start of "Promise" generic type annotation
// Options: ["never"]
type X = Promise< string>
// Message: There must be no space at start of "Promise" generic type annotation
type X = FooBar<string >
// Message: There must be no space at end of "FooBar" generic type annotation
type X = Promise< string >
// Message: There must be no space at start of "Promise" generic type annotation
// Message: There must be no space at end of "Promise" generic type annotation
type X = Promise< (foo), bar, (((baz))) >
// Message: There must be no space at start of "Promise" generic type annotation
// Message: There must be no space at end of "Promise" generic type annotation
// Options: ["always"]
type X = Promise<string >
// Message: There must be a space at start of "Promise" generic type annotation
// Options: ["always"]
type X = FooBar< string>
// Message: There must be a space at end of "FooBar" generic type annotation
// Options: ["always"]
type X = Promise<string>
// Message: There must be a space at start of "Promise" generic type annotation
// Message: There must be a space at end of "Promise" generic type annotation
// Options: ["always"]
type X = Promise<(foo), bar, (((baz)))>
// Message: There must be a space at start of "Promise" generic type annotation
// Message: There must be a space at end of "Promise" generic type annotation
// Options: ["always"]
type X = FooBar< string >
// Message: There must be one space at start of "FooBar" generic type annotation
// Options: ["always"]
type X = FooBar< string >
// Message: There must be one space at end of "FooBar" generic type annotation
// Options: ["always"]
type X = Promise< (foo), bar, (((baz))) >
// Message: There must be one space at start of "Promise" generic type annotation
// Message: There must be one space at end of "Promise" generic type annotation
```
The following patterns are not considered problems:
```js
type X = Promise<string>
type X = Promise<(string)>
type X = Promise<(foo), bar, (((baz)))>
// Options: ["always"]
type X = Promise< string >
// Options: ["always"]
type X = Promise< (string) >
// Options: ["always"]
type X = Promise< (foo), bar, (((baz))) >
```
<a name="eslint-plugin-flowtype-rules-no-dupe-keys"></a>
### <code>no-dupe-keys</code>
Checks for duplicate properties in Object annotations.
This rule mirrors ESLint's [no-dupe-keys](http://eslint.org/docs/rules/no-dupe-keys) rule.
```js
{
"rules": {
"flowtype/no-dupe-keys": 2
}
}
```
The following patterns are considered problems:
```js
type f = { a: number, b: string, a: number }
// Message: Duplicate property.
type f = { a: number, b: string, a: string }
// Message: Duplicate property.
type f = { get(key: "a"): string, get(key: "a"): string }
// Message: Duplicate property.
type f = { get(key: 1): string, get(key: 1): string }
// Message: Duplicate property.
type f = { get(key: 1.1): string, get(key: 1.1): string }
// Message: Duplicate property.
type f = { get(key: true): string, get(key: true): string }
// Message: Duplicate property.
type f = { get(key: {a: 1}): string, get(key: {a: 1}):string }
// Message: Duplicate property.
var a = "a"; type f = { get(key: a): string, get(key: a): string }
// Message: Duplicate property.
var b = 1; type f = { get(key: b): string, get(key: b): string }
// Message: Duplicate property.
var c = true; type f = { get(key: c): string, get(key: c): string }
// Message: Duplicate property.
var d = {}; type f = { get(key: d): string, get(key: d): string }
// Message: Duplicate property.
var e = []; type f = { get(key: e): string, get(key: e): string }
// Message: Duplicate property.
var e = [1, "a"]; type f = { get(key: e): string, get(key: e): string }
// Message: Duplicate property.
function fn() {}; type f = { get(key: fn): string, get(key: fn): string }
// Message: Duplicate property.
```
The following patterns are not considered problems:
```js
type FooType = { a: number, b: string, c: number }
type FooType = { a: number, b: string, a: number }
type f = { get(key: "a"): string, get(key: "b"): string }
type f = { get(key: 1): string, get(key: 2): string }
type f = { get(key: 1.1): string, get(key: 1.2): string }
type f = { get(key: true): string, get(key: false): string }
type f = { get(key: ["a", 1]): string, get(key: ["a", 2]): string }
type f = { get(key: ["a", ["b", 1]]): string, get(key: ["a", ["b", 2]]): string }
type f = { a: number, b: string, c: number }
type f = { get(key: "a"): string, get(key: "b"): string }
type f = { get(key: "a"): string, get(key: "a", key2: "b"): string }
type f = { get(key: "a"): string, get(key: 1): string }
type f = { get(key: { a: 1 }): string, get(key: { a: 2 }): string}
var a = {}; var b = {}; type f = { get(key: a): string, get(key: b): string }
var a = 1; var b = 1; type f = { get(key: a): string, get(key: b): string }
```
<a name="eslint-plugin-flowtype-rules-no-primitive-constructor-types"></a>
### <code>no-primitive-constructor-types</code>
Disallows use of primitive constructors as types, such as `Boolean`, `Number` and `String`. [See more](https://flowtype.org/docs/builtins.html).
```js
{
"rules": {
"flowtype/no-primitive-constructor-types": 2
}
}
```
The following patterns are considered problems:
```js
type x = Number
// Message: Unexpected use of Number constructor type.
type x = String
// Message: Unexpected use of String constructor type.
type x = Boolean
// Message: Unexpected use of Boolean constructor type.
type x = { a: Number }
// Message: Unexpected use of Number constructor type.
type x = { a: String }
// Message: Unexpected use of String constructor type.
type x = { a: Boolean }
// Message: Unexpected use of Boolean constructor type.
(x: Number) => {}
// Message: Unexpected use of Number constructor type.
(x: String) => {}
// Message: Unexpected use of String constructor type.
(x: Boolean) => {}
// Message: Unexpected use of Boolean constructor type.
```
The following patterns are not considered problems:
```js
type x = number
type x = string
type x = boolean
type x = { a: number }
type x = { a: string }
type x = { a: boolean }
(x: number) => {}
(x: string) => {}
(x: boolean) => {}
type x = MyNumber
type x = MyString
type x = MyBoolean
```
<a name="eslint-plugin-flowtype-rules-no-types-missing-file-annotation"></a>
### <code>no-types-missing-file-annotation</code>
Disallows Flow type imports, exports, aliases, and annotations in files missing a valid Flow file declaration (or a @noflow annotation).
```js
{
"rules": {
"flowtype/no-types-missing-file-annotation": 2
}
}
```
The following patterns are considered problems:
```js
const x: number = 42;
// Message: Type annotations require valid Flow declaration.
type FooType = number;
// Message: Type aliases require valid Flow declaration.
import type A from "a"
// Message: Type imports require valid Flow declaration.
import type {A} from "a"
// Message: Type imports require valid Flow declaration.
import {type A} from "a"
// Message: Type imports require valid Flow declaration.
export type {A} from "a"
// Message: Type exports require valid Flow declaration.
function t<T>(): T{}
// Message: Type annotations require valid Flow declaration.
```
The following patterns are not considered problems:
```js
// @flow
const x: number = 42;
/* @flow weak */
type FooType = number;
/* @noflow */
type FooType = number;
/* @noflow */
import type A from "a"
/* @noflow */
import {type A} from "a"
/* @noflow */
export type {A} from "a"
```
<a name="eslint-plugin-flowtype-rules-no-weak-types"></a>
### <code>no-weak-types</code>
Warns against weak type annotations *any*, *Object* and *Function*.
These types can cause flow to silently skip over portions of your code,
which would have otherwise caused type errors.
This rule optionally takes one argument, an object to configure which type warnings to enable. By default, all of the
warnings are enabled. e.g. to disable the `any` warning (allowing it to exist in your code), while continuing to warn
about `Object` and `Function`:
```js
{
"rules": {
"flowtype/no-weak-types": [2, {
"any": false,
"Object": true,
"Function": true
}]
}
}
// or, the following is equivalent as default is true:
{
"rules": {
"flowtype/no-weak-types": [2, {
"any": false
}]
}
}
```
The following patterns are considered problems:
```js
function foo(thing): any {}
// Message: Unexpected use of weak type "any"
function foo(thing): Promise<any> {}
// Message: Unexpected use of weak type "any"
function foo(thing): Promise<Promise<any>> {}
// Message: Unexpected use of weak type "any"
function foo(thing): Object {}
// Message: Unexpected use of weak type "Object"
function foo(thing): Promise<Object> {}
// Message: Unexpected use of weak type "Object"
function foo(thing): Promise<Promise<Object>> {}
// Message: Unexpected use of weak type "Object"
function foo(thing): Function {}
// Message: Unexpected use of weak type "Function"
function foo(thing): Promise<Function> {}
// Message: Unexpected use of weak type "Function"
function foo(thing): Promise<Promise<Function>> {}
// Message: Unexpected use of weak type "Function"
(foo: any) => {}
// Message: Unexpected use of weak type "any"
(foo: Function) => {}
// Message: Unexpected use of weak type "Function"
(foo?: any) => {}
// Message: Unexpected use of weak type "any"
(foo?: Function) => {}
// Message: Unexpected use of weak type "Function"
(foo: { a: any }) => {}
// Message: Unexpected use of weak type "any"
(foo: { a: Object }) => {}
// Message: Unexpected use of weak type "Object"
(foo: any[]) => {}
// Message: Unexpected use of weak type "any"
type Foo = any
// Message: Unexpected use of weak type "any"
type Foo = Function
// Message: Unexpected use of weak type "Function"
type Foo = { a: any }
// Message: Unexpected use of weak type "any"
type Foo = { a: Object }
// Message: Unexpected use of weak type "Object"
type Foo = { (a: Object): string }
// Message: Unexpected use of weak type "Object"
type Foo = { (a: string): Function }
// Message: Unexpected use of weak type "Function"
function foo(thing: any) {}
// Message: Unexpected use of weak type "any"
function foo(thing: Object) {}
// Message: Unexpected use of weak type "Object"
var foo: Function
// Message: Unexpected use of weak type "Function"
var foo: Object
// Message: Unexpected use of weak type "Object"
class Foo { props: any }
// Message: Unexpected use of weak type "any"
class Foo { props: Object }
// Message: Unexpected use of weak type "Object"
var foo: any
// Message: Unexpected use of weak type "any"
// Options: [{"Function":false}]
type X = any; type Y = Function; type Z = Object
// Message: Unexpected use of weak type "any"
// Message: Unexpected use of weak type "Object"
// Options: [{"Object":false,"any":false}]
type X = any; type Y = Function; type Z = Object
// Message: Unexpected use of weak type "Function"
```
The following patterns are not considered problems:
```js
function foo(thing): string {}
function foo(thing): Promise<string> {}
function foo(thing): Promise<Promise<string>> {}
(foo?: string) => {}
(foo: ?string) => {}
(foo: { a: string }) => {}
(foo: { a: ?string }) => {}
(foo: string[]) => {}
type Foo = string
type Foo = { a: string }
type Foo = { (a: string): string }
function foo(thing: string) {}
var foo: string
class Foo { props: string }
// Options: [{"Object":false,"any":false}]
type X = any; type Y = Object
// Options: [{"Function":false}]
type X = Function
function foo(thing): Function {}
```
<a name="eslint-plugin-flowtype-rules-object-type-delimiter"></a>
### <code>object-type-delimiter</code>
_The `--fix` option on the command line automatically fixes problems reported by this rule._
Enforces consistent separators between properties in Flow object types.
This rule takes one argument.
If it is `'comma'` then a problem is raised when using `;` as a separator.
If it is `'semicolon'` then a problem is raised when using `,` as a separator.
The default value is `'comma'`.
_This rule is ported from `babel/flow-object-type`, however the default option was changed._
The following patterns are considered problems:
```js
// Options: ["semicolon"]
type Foo = { a: Foo, b: Bar }
// Message: Prefer semicolons to commas in object and class types
// Options: ["comma"]
type Foo = { a: Foo; b: Bar }
// Message: Prefer commas to semicolons in object and class types
// Options: ["semicolon"]
type Foo = { [a: string]: Foo, [b: string]: Bar }
// Message: Prefer semicolons to commas in object and class types
// Options: ["comma"]
type Foo = { [a: string]: Foo; [b: string]: Bar }
// Message: Prefer commas to semicolons in object and class types
// Options: ["semicolon"]
type Foo = { (): Foo, (): Bar }
// Message: Prefer semicolons to commas in object and class types
// Options: ["comma"]
type Foo = { (): Foo; (): Bar }
// Message: Prefer commas to semicolons in object and class types
// Options: ["semicolon"]
declare class Foo { a: Foo, }
// Message: Prefer semicolons to commas in object and class types
// Options: ["comma"]
declare class Foo { a: Foo; }
// Message: Prefer commas to semicolons in object and class types
// Options: ["semicolon"]
declare class Foo { [a: string]: Foo, }
// Message: Prefer semicolons to commas in object and class types
// Options: ["comma"]
declare class Foo { a: Foo; }
// Message: Prefer commas to semicolons in object and class types
// Options: ["semicolon"]
declare class Foo { (): Foo, }
// Message: Prefer semicolons to commas in object and class types
// Options: ["comma"]
declare class Foo { (): Foo; }
// Message: Prefer commas to semicolons in object and class types
// Options: ["semicolon"]
declare class Foo { static (): Foo, }
// Message: Prefer semicolons to commas in object and class types
// Options: ["comma"]
declare class Foo { static (): Foo; }
// Message: Prefer commas to semicolons in object and class types
```
The following patterns are not considered problems:
```js
// Options: ["semicolon"]
type Foo = { a: Foo; b: Bar }
// Options: ["comma"]
type Foo = { a: Foo, b: Bar }
// Options: ["semicolon"]
type Foo = { [a: string]: Foo; [b: string]: Bar }
// Options: ["comma"]
type Foo = { [a: string]: Foo, [b: string]: Bar }
// Options: ["semicolon"]
type Foo = { (): Foo; (): Bar }
// Options: ["comma"]
type Foo = { (): Foo, (): Bar }
type Foo = { a: Foo, b: Bar }
type Foo = { [a: string]: Foo, [b: string]: Bar }
type Foo = { (): Foo, (): Bar }
// Options: ["semicolon"]
declare class Foo { a: Foo; }
// Options: ["comma"]
declare class Foo { a: Foo, }
// Options: ["semicolon"]
declare class Foo { [a: string]: Foo; }
// Options: ["comma"]
declare class Foo { [a: string]: Foo, }
// Options: ["semicolon"]
declare class Foo { (): Foo; }
// Options: ["comma"]
declare class Foo { (): Foo, }
// Options: ["semicolon"]
type Foo = { a: Foo, b: Bar }
```
<a name="eslint-plugin-flowtype-rules-require-parameter-type"></a>
### <code>require-parameter-type</code>
Requires that all function parameters have type annotations.
<a name="eslint-plugin-flowtype-rules-require-parameter-type-options"></a>
#### Options
You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
Alternatively, you can want to exclude only concise arrow functions (e.g. `x => x * 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
```js
{
"rules": {
"flowtype/require-parameter-type": [
2,
{
"excludeArrowFunctions": true
}
]
}
}
{
"rules": {
"flowtype/require-parameter-type": [
2,
{
"excludeArrowFunctions": "expressionsOnly"
}
]
}
}
```
You can exclude parameters that match a certain regex by using `excludeParameterMatch`.
```js
{
"rules": {
"flowtype/require-parameter-type": [
2,
{
"excludeParameterMatch": "^_"
}
]
}
}
```
This excludes all parameters that start with an underscore (`_`).
The default pattern is `a^`, which doesn't match anything, i.e., all parameters are checked.
The following patterns are considered problems:
```js
(foo) => {}
// Message: Missing "foo" parameter type annotation.
function x(foo) {}
// Message: Missing "foo" parameter type annotation.
// Options: [{"excludeArrowFunctions":true}]
function x(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.
// @flow
(foo) => {}
// Message: Missing "foo" parameter type annotation.
// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
(foo) => {}
// Message: Missing "foo" parameter type annotation.
// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
function x(foo) {}
// Message: Missing "foo" parameter type annotation.
// Options: [{"excludeParameterMatch":"^_"}]
(_foo: number, bar) => {}
// Message: Missing "bar" parameter type annotation.
// Options: [{"excludeParameterMatch":"^_"}]
(_foo, bar) => {}
// Message: Missing "bar" parameter type annotation.
```
The following patterns are not considered problems:
```js
(foo: string) => {}
(foo: string = 'FOO') => {}
(...foo: string) => {}
({foo}: {foo: string}) => {}
([foo]: Array) => {}
(foo) => {}
// Options: [{"excludeArrowFunctions":true}]
(foo) => {}
// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
(foo) => 3
// Options: [{"excludeParameterMatch":"^_"}]
(_foo, bar: string) => {}
// Options: [{"excludeParameterMatch":"^_"}]
(_foo: number, bar: string) => {}
(foo) => {}
```
<a name="eslint-plugin-flowtype-rules-require-return-type"></a>
### <code>require-return-type</code>
Requires that functions have return type annotation.
<a name="eslint-plugin-flowtype-rules-require-return-type-options"></a>
#### Options
You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
Alternatively, you can exclude a concise arrow function (e.g. `() => 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
```js
{
"rules": {
"flowtype/require-return-type": [
2,
"always",
{
"excludeArrowFunctions": true
}
]
}
}
{
"rules": {
"flowtype/require-return-type": [
2,
"always",
{
"excludeArrowFunctions": "expressionsOnly"
}
]
}
}
```
You can exclude or include specific tests with the `includeOnlyMatching` and `excludeMatching` rules.
```js
{
"rules": {
"flowtype/require-return-type": [
2,
"always",
{
"includeOnlyMatching": [
"^F.*",
"Ba(r|z)"
]
}
]
}
}
{
"rules": {
"flowtype/require-return-type": [
2,
"always",
{
"excludeMatching": [
"^F.*",
"Ba(r|z)"
]
}
]
}
}
```
Both rules take an array that can contain either strings or valid RegExp statements.
The following patterns are considered problems:
```js
(foo) => { return "foo"; }
// Message: Missing return type annotation.
// Options: ["always"]
(foo) => { return "foo"; }
// Message: Missing return type annotation.
// Options: ["always"]
(foo) => "foo"
// Message: Missing return type annotation.
(foo) => ({})
// Message: Missing return type annotation.
(foo): undefined => { return; }
// Message: Must not annotate undefined return type.
(foo): void => { return; }
// Message: Must not annotate undefined return type.
(foo): undefined => { return undefined; }
// Message: Must not annotate undefined return type.
(foo): void => { return void 0; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"never"}]
(foo): undefined => { return; }
// Message: Must not annotate undefined return type.
// Options: ["always",{"annotateUndefined":"never"}]
(foo): void => { 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.
// Options: ["always",{"annotateUndefined":"always"}]
(foo) => { return void 0; }
// Message: Must annotate undefined return type.
// @flow
(foo) => { return 1; }
// Message: Missing return type annotation.
// Options: ["always",{"annotateUndefined":"always"}]
// @flow
(foo) => { return undefined; }
// Message: Must annotate undefined return type.
// Options: ["always"]
async () => { return 2; }
// Message: Missing return type annotation.
// Options: ["always",{"annotateUndefined":"always"}]
async () => {}
// Message: Missing return type annotation.
// Options: ["always",{"annotateUndefined":"always"}]
async function x() {}
// Message: Missing return type annotation.
// Options: ["always"]
async () => { return; }
// Message: Missing return type annotation.
// Options: ["always"]
function* x() {}
// Message: Missing return type annotation.
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
() => { return 3; }
// Message: Missing return type annotation.
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
async () => { return 4; }
// Message: Missing return type annotation.
// Options: ["always",{"includeOnlyMatching":["bar"]}]
function foo() { return 42; }
function bar() { return 42; }
// Message: Missing return type annotation.
// Options: ["always",{"includeOnlyMatching":["bar"]}]
const foo = () => { return 42; };
const bar = () => { return 42; }
// Message: Missing return type annotation.
```
The following patterns are not considered problems:
```js
return;
(foo): string => {}
// Options: ["always"]
(foo): string => {}
(foo) => { return; }
(foo): Object => ( {} )
(foo) => { return undefined; }
(foo) => { return void 0; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): undefined => { return; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): void => { return; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return undefined; }
// Options: ["always",{"annotateUndefined":"never"}]
(foo) => { return void 0; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): undefined => { return undefined; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo): void => { return void 0; }
// Options: ["always"]
(foo) => { return 1; }
// Options: ["always",{"annotateUndefined":"always"}]
(foo) => { return undefined; }
// Options: ["always",{"annotateUndefined":"always"}]
async function doThing(): Promise<void> {}
// Options: ["always",{"annotateUndefined":"always"}]
function* doThing(): Generator<number, void, void> { yield 2; }
async (foo): Promise<number> => { return 3; }
// Options: ["always",{"excludeArrowFunctions":true}]
() => 3
// Options: ["always",{"excludeArrowFunctions":true}]
() => { return 4; }
// Options: ["always",{"excludeArrowFunctions":true}]
() => undefined
// Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
() => undefined
// Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
() => { return undefined; }
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
() => 3
// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
async () => 3
// Options: ["always",{"excludeMatching":["foo"]}]
function foo() { return 42; }
// Options: ["always",{"includeOnlyMatching":["bar"]}]
function foo() { return 42; }
// Options: ["always",{"excludeMatching":["bar"]}]
function foo(): number { return 42; }
function bar() { return 42; }
// Options: ["always",{"includeOnlyMatching":["foo","baz"]}]
function foo(): number { return 42; }
function bar() { return 42; }
// Options: ["always",{"excludeMatching":["^b.*","qux"]}]
function foo(): number { return 42; }
function bar() { return 42; }
// Options: ["always",{"includeOnlyMatching":["^f.*"]}]
function foo(): number { return 42; }
function bar() { return 42; }
```
<a name="eslint-plugin-flowtype-rules-require-valid-file-annotation"></a>
### <code>require-valid-file-annotation</code>
This rule validates Flow file annotations.
This rule can optionally report missing or missed placed annotations, common typos (e.g. `// @floww`), and enforce a consistant annotation style.
<a name="eslint-plugin-flowtype-rules-require-valid-file-annotation-options"></a>
#### Options
The rule has a string option:
* `"never"` (default): Never report files that are missing an `@flow` annotation.
* `"always"`: Always report files that are missing an `@flow` annotation
This rule has an object option:
* `"annotationStyle"` - Enforce a consistant file annotation style.
* `"none"` (default): Either annotation style is accepted.
* `"line"`: Require single line annotations (i.e. `// @flow`).
* `"block"`: Require block annotations (i.e. `/* @flow */`).
```js
{
"rules": {
"flowtype/require-valid-file-annotation": [
2,
"always"
]
}
}
{
"rules": {
"flowtype/require-valid-file-annotation": [
2,
"always", {
"annotationStyle": "block"
}
]
}
}
```
The following patterns are considered problems:
```js
;// @flow
// Message: Flow file annotation not at the top of the file.
;
// @flow
// Message: Flow file annotation not at the top of the file.
// @Flow
// Message: Malformed Flow file annotation.
// @NoFlow
// Message: Malformed Flow file annotation.
// @Noflow
// Message: Malformed Flow file annotation.
// @floweeeeeee
// Message: Misspelled or malformed Flow file annotation.
// @nofloweeeeeee
// Message: Misspelled or malformed Flow file annotation.
// Options: ["always"]
a;
// Message: Flow file annotation is missing.
// Options: ["always",{"annotationStyle":"line"}]
/* @flow */
// Message: Flow file annotation style must be `// @flow`
// Options: ["always",{"annotationStyle":"block"}]
// @flow
// Message: Flow file annotation style must be `/* @flow */`
// Options: ["always",{"annotationStyle":"line"}]
/* @noflow */
// Message: Flow file annotation style must be `// @noflow`
// Options: ["always",{"annotationStyle":"block"}]
// @noflow
// Message: Flow file annotation style must be `/* @noflow */`
```
The following patterns are not considered problems:
```js
a;
// @flow
a;
//@flow
a;
//**@flow
a;
/* foo @flow bar */
a;
// @flow
a;
// @flow
// @FLow
// @noflow
a;
// Options: ["always"]
a;
// Options: ["always",{"annotationStyle":"line"}]
// @flow
// Options: ["always",{"annotationStyle":"block"}]
/* @flow */
```
<a name="eslint-plugin-flowtype-rules-require-variable-type"></a>
### <code>require-variable-type</code>
Requires that all variable declarators have type annotations.
<a name="eslint-plugin-flowtype-rules-require-variable-type-options"></a>
#### Options
You can exclude variables that match a certain regex by using `excludeVariableMatch`.
This excludes all parameters that start with an underscore (`_`).
The default pattern is `a^`, which doesn't match anything, i.e., all parameters are checked.
```js
{
"rules": {
"flowtype/require-variable-type": [
2,
{
"excludeVariableMatch": "^_"
}
]
}
}
```
You can choose specific variable types (`var`, `let`, and `const`) to ignore using `excludeVariableTypes`.
This excludes `var` and `let` declarations from needing type annotations, but forces `const` declarations to have it.
By default, all declarations are checked.
```js
{
"rules": {
"flowtype/require-variable-type": [
2,
{
"excludeVariableTypes": {
"var": true,
"let": true,
"const": false,
}
}
]
}
}
```
The following patterns are considered problems:
```js
var foo = "bar"
// Message: Missing "foo" variable type annotation.
var foo : string = "bar", bar = 1
// Message: Missing "bar" variable type annotation.
// Options: [{"excludeVariableMatch":"^_"}]
var _foo = "bar", bar = 1
// Message: Missing "bar" variable type annotation.
// Options: [{"excludeVariableTypes":{"let":false,"var":true}}]
var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"
// Message: Missing "hey" variable type annotation.
```
The following patterns are not considered problems:
```js
var foo : string = "bar"
var foo : string = "bar", bar : number = 1
// Options: [{"excludeVariableMatch":"^_"}]
var _foo = "bar", bar : number = 1
// Options: [{"excludeVariableTypes":{"var":true}}]
var foo = "bar", bar = 1
// Options: [{"excludeVariableTypes":{"let":true,"var":true}}]
var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"
```
<a name="eslint-plugin-flowtype-rules-semi"></a>
### <code>semi</code>
_The `--fix` option on the command line automatically fixes problems reported by this rule._
Enforces consistent use of semicolons after type aliases.
This rule takes one argument. If it is `'never'` then a problem is raised when there is a semicolon after a type alias. If it is `'always'` then a problem is raised when there is no semicolon after a type alias.
The default value is `'always'`.
The following patterns are considered problems:
```js
// Options: []
type FooType = {}
// Message: Missing semicolon.
// Options: ["always"]
type FooType = {}
// Message: Missing semicolon.
// Options: ["never"]
type FooType = {};
// Message: Extra semicolon.
```
The following patterns are not considered problems:
```js
type FooType = {};
// Options: ["always"]
type FooType = {};
// Options: ["always"]
type FooType = { a: number;
b: string;
};
// Opt