UNPKG

35 kBMarkdownView Raw
1<h1 id="eslint-plugin-flowtype">eslint-plugin-flowtype</h1>
2
3[![NPM version](http://img.shields.io/npm/v/eslint-plugin-flowtype.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-flowtype)
4[![Travis build status](http://img.shields.io/travis/gajus/eslint-plugin-flowtype/master.svg?style=flat-square)](https://travis-ci.org/gajus/eslint-plugin-flowtype)
5[![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
6
7[Flow type](http://flowtype.org/) linting rules for ESLint.
8
9* [eslint-plugin-flowtype](#eslint-plugin-flowtype)
10 * [Installation](#eslint-plugin-flowtype-installation)
11 * [Configuration](#eslint-plugin-flowtype-configuration)
12 * [Settings](#eslint-plugin-flowtype-settings)
13 * [`onlyFilesWithFlowAnnotation`](#eslint-plugin-flowtype-settings-onlyfileswithflowannotation)
14 * [Rules](#eslint-plugin-flowtype-rules)
15 * [`define-flow-type`](#eslint-plugin-flowtype-rules-define-flow-type)
16 * [`require-parameter-type`](#eslint-plugin-flowtype-rules-require-parameter-type)
17 * [`require-return-type`](#eslint-plugin-flowtype-rules-require-return-type)
18 * [`require-valid-file-annotation`](#eslint-plugin-flowtype-rules-require-valid-file-annotation)
19 * [`space-after-type-colon`](#eslint-plugin-flowtype-rules-space-after-type-colon)
20 * [`space-before-type-colon`](#eslint-plugin-flowtype-rules-space-before-type-colon)
21 * [`type-id-match`](#eslint-plugin-flowtype-rules-type-id-match)
22 * [`use-flow-type`](#eslint-plugin-flowtype-rules-use-flow-type)
23 * [`valid-syntax`](#eslint-plugin-flowtype-rules-valid-syntax)
24
25
26<h2 id="eslint-plugin-flowtype-installation">Installation</h2>
27
281. Install [ESLint](https://www.github.com/eslint/eslint).
291. Install [`babel-eslint`](https://github.com/babel/babel-eslint) parser (ESLint parser [does not support type annotations](https://github.com/eslint/eslint/issues/2157)).
301. Install [`eslint-plugin-flowtype`](https://github.com/gajus/eslint-plugin-flowtype) plugin.
31
32<!-- -->
33
34```sh
35npm install eslint
36npm install babel-eslint
37npm install eslint-plugin-flowtype
38```
39
40<h2 id="eslint-plugin-flowtype-configuration">Configuration</h2>
41
421. Set `parser` property to `babel-eslint`.
431. Add `plugins` section and specify `eslint-plugin-flowtype` as a plugin.
441. Enable rules.
45
46<!-- -->
47
48```json
49{
50 "parser": "babel-eslint",
51 "plugins": [
52 "flowtype"
53 ],
54 "rules": {
55 "flowtype/define-flow-type": 1,
56 "flowtype/require-parameter-type": 1,
57 "flowtype/require-return-type": [
58 1,
59 "always",
60 {
61 "annotateUndefined": "never"
62 }
63 ],
64 "flowtype/space-after-type-colon": [
65 1,
66 "always"
67 ],
68 "flowtype/space-before-type-colon": [
69 1,
70 "never"
71 ],
72 "flowtype/type-id-match": [
73 1,
74 "^([A-Z][a-z0-9]+)+Type$"
75 ],
76 "flowtype/use-flow-type": 1,
77 "flowtype/valid-syntax": 1
78 },
79 "settings": {
80 "flowtype": {
81 "onlyFilesWithFlowAnnotation": false
82 }
83 }
84}
85```
86
87<h2 id="eslint-plugin-flowtype-settings">Settings</h2>
88
89<h3 id="eslint-plugin-flowtype-settings-onlyfileswithflowannotation"><code>onlyFilesWithFlowAnnotation</code></h3>
90
91When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/docs/about-flow.html#gradual) in the first comment.
92
93```js
94{
95 "settings": {
96 "flowtype": {
97 "onlyFilesWithFlowAnnotation": true
98 }
99 }
100}
101```
102
103<h2 id="eslint-plugin-flowtype-rules">Rules</h2>
104
105<h3 id="eslint-plugin-flowtype-rules-define-flow-type"><code>define-flow-type</code></h3>
106
107Marks Flow type identifiers as defined.
108
109Used to suppress [`no-undef`](http://eslint.org/docs/rules/no-undef) reporting of type identifiers.
110
111The following patterns are not considered problems:
112
113```js
114var a: AType
115// Additional rules: {"no-undef":2}
116
117var a: AType; var b: AType
118// Additional rules: {"no-undef":2}
119
120var a; (a: AType)
121// Additional rules: {"no-undef":2}
122
123var a: AType<BType>
124// Additional rules: {"no-undef":2}
125
126type A = AType
127// Additional rules: {"no-undef":2}
128
129function f(a: AType) {}
130// Additional rules: {"no-undef":2}
131
132function f(a: AType.a) {}
133// Additional rules: {"no-undef":2}
134
135function f(a: AType.a.b) {}
136// Additional rules: {"no-undef":2}
137
138function f(a): AType {}; var a: AType
139// Additional rules: {"no-undef":2}
140
141function f(a): AType {}
142// Additional rules: {"no-undef":2}
143
144class C { a: AType }
145// Additional rules: {"no-undef":2}
146
147class C { a: AType.a }
148// Additional rules: {"no-undef":2}
149
150class C { a: AType.a.b }
151// Additional rules: {"no-undef":2}
152
153class C implements AType {}
154// Additional rules: {"no-undef":2}
155
156interface AType {}
157// Additional rules: {"no-undef":2}
158
159({ a: ({b() {}}: AType) })
160// Additional rules: {"no-undef":2}
161
162type X = {Y<AType>(): BType}
163// Additional rules: {"no-undef":2}
164
165interface AType<BType> {}
166// Additional rules: {"no-undef":2}
167
168var a: AType
169// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
170
171var a: AType; var b: AType
172// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
173
174var a; (a: AType)
175// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
176
177var a: AType<BType>
178// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
179
180type A = AType
181// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
182
183function f(a: AType) {}
184// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
185
186function f(a: AType.a) {}
187// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
188
189function f(a: AType.a.b) {}
190// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
191
192function f(a): AType {}; var a: AType
193// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
194
195function f(a): AType {}
196// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
197
198class C { a: AType }
199// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
200
201class C { a: AType.a }
202// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
203
204class C { a: AType.a.b }
205// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
206
207class C implements AType {}
208// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
209
210interface AType {}
211// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
212
213({ a: ({b() {}}: AType) })
214// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
215
216type X = {Y<AType>(): BType}
217// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
218
219interface AType<BType> {}
220// Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
221```
222
223
224
225<h3 id="eslint-plugin-flowtype-rules-require-parameter-type"><code>require-parameter-type</code></h3>
226
227Requires that all function parameters have type annotations.
228
229<h4 id="eslint-plugin-flowtype-rules-require-parameter-type-options">Options</h4>
230
231You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
232
233Alternatively, you can want to exclude only function expressions (e.g. `x => x * 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
234
235```js
236{
237 "rules": {
238 "flowtype/require-parameter-type": [
239 2,
240 {
241 "excludeArrowFunctions": true
242 }
243 ]
244 }
245}
246
247{
248 "rules": {
249 "flowtype/require-parameter-type": [
250 2,
251 {
252 "excludeArrowFunctions": "expressionsOnly"
253 }
254 ]
255 }
256}
257```
258
259The following patterns are considered problems:
260
261```js
262(foo) => {}
263// Message: Missing "foo" parameter type annotation.
264
265function x(foo) {}
266// Message: Missing "foo" parameter type annotation.
267
268// Options: [{"excludeArrowFunctions":true}]
269function x(foo) {}
270// Message: Missing "foo" parameter type annotation.
271
272(foo = 'FOO') => {}
273// Message: Missing "foo" parameter type annotation.
274
275(...foo) => {}
276// Message: Missing "foo" parameter type annotation.
277
278({foo}) => {}
279// Message: Missing "{foo}" parameter type annotation.
280
281([foo]) => {}
282// Message: Missing "[foo]" parameter type annotation.
283
284({foo = 1} = {}) => {}
285// Message: Missing "{foo = 1}" parameter type annotation.
286
287// @flow
288(foo) => {}
289// Message: Missing "foo" parameter type annotation.
290
291// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
292(foo) => {}
293// Message: Missing "foo" parameter type annotation.
294
295// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
296function x(foo) {}
297// Message: Missing "foo" parameter type annotation.
298```
299
300The following patterns are not considered problems:
301
302```js
303(foo: string) => {}
304
305(foo: string = 'FOO') => {}
306
307(...foo: string) => {}
308
309({foo}: {foo: string}) => {}
310
311([foo]: Array) => {}
312
313(foo) => {}
314
315// Options: [{"excludeArrowFunctions":true}]
316(foo) => {}
317
318// Options: [{"excludeArrowFunctions":"expressionsOnly"}]
319(foo) => 3
320```
321
322
323
324<h3 id="eslint-plugin-flowtype-rules-require-return-type"><code>require-return-type</code></h3>
325
326Requires that functions have return type annotation.
327
328<h4 id="eslint-plugin-flowtype-rules-require-return-type-options">Options</h4>
329
330You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
331
332Alternatively, you can want to exclude only function expressions (e.g. `() => 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
333
334```js
335{
336 "rules": {
337 "flowtype/require-return-type": [
338 2,
339 "always",
340 {
341 "excludeArrowFunctions": true
342 }
343 ]
344 }
345}
346
347{
348 "rules": {
349 "flowtype/require-return-type": [
350 2,
351 "always",
352 {
353 "excludeArrowFunctions": "expressionsOnly"
354 }
355 ]
356 }
357}
358```
359
360The following patterns are considered problems:
361
362```js
363(foo) => { return "foo"; }
364// Message: Missing return type annotation.
365
366// Options: ["always"]
367(foo) => { return "foo"; }
368// Message: Missing return type annotation.
369
370// Options: ["always"]
371(foo) => "foo"
372// Message: Missing return type annotation.
373
374(foo) => ({})
375// Message: Missing return type annotation.
376
377(foo): undefined => { return; }
378// Message: Must not annotate undefined return type.
379
380(foo): void => { return; }
381// Message: Must not annotate undefined return type.
382
383(foo): undefined => { return undefined; }
384// Message: Must not annotate undefined return type.
385
386(foo): void => { return void 0; }
387// Message: Must not annotate undefined return type.
388
389// Options: ["always",{"annotateUndefined":"never"}]
390(foo): undefined => { return; }
391// Message: Must not annotate undefined return type.
392
393// Options: ["always",{"annotateUndefined":"never"}]
394(foo): void => { return; }
395// Message: Must not annotate undefined return type.
396
397// Options: ["always",{"annotateUndefined":"always"}]
398(foo) => { return; }
399// Message: Must annotate undefined return type.
400
401// Options: ["always",{"annotateUndefined":"never"}]
402(foo): undefined => { return undefined; }
403// Message: Must not annotate undefined return type.
404
405// Options: ["always",{"annotateUndefined":"always"}]
406(foo) => { return undefined; }
407// Message: Must annotate undefined return type.
408
409// Options: ["always",{"annotateUndefined":"always"}]
410(foo) => { return void 0; }
411// Message: Must annotate undefined return type.
412
413// @flow
414(foo) => { return 1; }
415// Message: Missing return type annotation.
416
417// Options: ["always",{"annotateUndefined":"always"}]
418// @flow
419 (foo) => { return undefined; }
420// Message: Must annotate undefined return type.
421
422// Options: ["always"]
423async () => { return 2; }
424// Message: Missing return type annotation.
425
426// Options: ["always",{"annotateUndefined":"always"}]
427async () => {}
428// Message: Missing return type annotation.
429
430// Options: ["always",{"annotateUndefined":"always"}]
431async function x() {}
432// Message: Missing return type annotation.
433
434// Options: ["always"]
435async () => { return; }
436// Message: Missing return type annotation.
437
438// Options: ["always"]
439function* x() {}
440// Message: Missing return type annotation.
441
442// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
443() => { return 3; }
444// Message: Missing return type annotation.
445
446// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
447async () => { return 4; }
448// Message: Missing return type annotation.
449```
450
451The following patterns are not considered problems:
452
453```js
454(foo): string => {}
455
456// Options: ["always"]
457(foo): string => {}
458
459(foo) => { return; }
460
461(foo): Object => ( {} )
462
463(foo) => { return undefined; }
464
465(foo) => { return void 0; }
466
467// Options: ["always",{"annotateUndefined":"always"}]
468(foo): undefined => { return; }
469
470// Options: ["always",{"annotateUndefined":"always"}]
471(foo): void => { return; }
472
473// Options: ["always",{"annotateUndefined":"never"}]
474(foo) => { return; }
475
476// Options: ["always",{"annotateUndefined":"never"}]
477(foo) => { return undefined; }
478
479// Options: ["always",{"annotateUndefined":"never"}]
480(foo) => { return void 0; }
481
482// Options: ["always",{"annotateUndefined":"always"}]
483(foo): undefined => { return undefined; }
484
485// Options: ["always",{"annotateUndefined":"always"}]
486(foo): void => { return void 0; }
487
488// Options: ["always"]
489(foo) => { return 1; }
490
491// Options: ["always",{"annotateUndefined":"always"}]
492(foo) => { return undefined; }
493
494// Options: ["always",{"annotateUndefined":"always"}]
495async function doThing(): Promise<void> {}
496
497// Options: ["always",{"annotateUndefined":"always"}]
498function* doThing(): Generator<number, void, void> { yield 2; }
499
500async (foo): Promise<number> => { return 3; }
501
502// Options: ["always",{"excludeArrowFunctions":true}]
503() => 3
504
505// Options: ["always",{"excludeArrowFunctions":true}]
506() => { return 4; }
507
508// Options: ["always",{"excludeArrowFunctions":true}]
509() => undefined
510
511// Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
512() => undefined
513
514// Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
515() => { return undefined; }
516
517// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
518() => 3
519
520// Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
521async () => 3
522```
523
524
525
526<h3 id="eslint-plugin-flowtype-rules-require-valid-file-annotation"><code>require-valid-file-annotation</code></h3>
527
528Makes sure that files have a valid `@flow` annotation. It will report annotations with typos (such as `// @floww`) or not placed at the top of the file, and optionaly missing annotations.
529
530<h4 id="eslint-plugin-flowtype-rules-require-valid-file-annotation-options">Options</h4>
531
532By default, this rule won't complain if there is no `@flow` annotation at all in the file. Passing a `"always"` option reports files missing those annotations as well.
533
534```js
535{
536 "rules": {
537 "flowtype/require-valid-file-annotation": [
538 2,
539 "always"
540 ]
541 }
542}
543```
544
545The following patterns are considered problems:
546
547```js
548;// @flow
549// Message: Flow file annotation not at the top of the file.
550
551;
552// @flow
553// Message: Flow file annotation not at the top of the file.
554
555// @Flow
556// Message: Malformed flow file annotation.
557
558// @floweeeeeee
559// Message: Malformed flow file annotation.
560
561// Options: ["always"]
562a;
563// Message: Flow file annotation is missing.
564```
565
566The following patterns are not considered problems:
567
568```js
569a;
570
571// @flow
572a;
573
574//@flow
575a;
576
577//**@flow
578a;
579
580/* foo @flow bar */
581a;
582
583
584
585// @flow
586a;
587
588// @flow
589// @FLow
590
591// Options: ["always"]
592a;
593```
594
595
596
597<h3 id="eslint-plugin-flowtype-rules-space-after-type-colon"><code>space-after-type-colon</code></h3>
598
599_The `--fix` option on the command line automatically fixes problems reported by this rule._
600
601Enforces consistent spacing after the type annotation colon.
602
603This 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'`.
604
605The following patterns are considered problems:
606
607```js
608// Options: ["never"]
609(foo: string) => {}
610// Message: There must be no space after "foo" parameter type annotation colon.
611
612// Options: ["never"]
613export default function (foo: string) {}
614// Message: There must be no space after "foo" parameter type annotation colon.
615
616// Options: ["never"]
617function foo (foo: string) {}
618// Message: There must be no space after "foo" parameter type annotation colon.
619
620// Options: ["always"]
621(foo:string) => {}
622// Message: There must be a space after "foo" parameter type annotation colon.
623
624function foo (foo:string) {}
625// Message: There must be a space after "foo" parameter type annotation colon.
626
627// Options: ["always"]
628(foo: string) => {}
629// Message: There must be 1 space after "foo" parameter type annotation colon.
630
631// Options: ["always"]
632(foo:(() => void)) => {}
633// Message: There must be a space after "foo" parameter type annotation colon.
634
635// Options: ["never"]
636(foo: (() => void)) => {}
637// Message: There must be no space after "foo" parameter type annotation colon.
638
639// Options: ["always"]
640(foo: (() => void)) => {}
641// Message: There must be 1 space after "foo" parameter type annotation colon.
642
643// Options: ["always"]
644():Object => {}
645// Message: There must be a space after return type colon.
646
647// Options: ["never"]
648(): Object => {}
649// Message: There must be no space after return type colon.
650
651// Options: ["always"]
652(): Object => {}
653// Message: There must be 1 space after return type colon.
654
655// Options: ["always"]
656():(() => void) => {}
657// Message: There must be a space after return type colon.
658
659// Options: ["never"]
660(): (() => void) => {}
661// Message: There must be no space after return type colon.
662
663// Options: ["always"]
664(): (() => void) => {}
665// Message: There must be 1 space after return type colon.
666
667async function foo({ lorem, ipsum, dolor }:SomeType) {}
668// Message: There must be a space after "{ lorem, ipsum, dolor }" parameter type annotation colon.
669
670({ lorem, ipsum, dolor } : SomeType) => {}
671// Message: There must be 1 space after "{ lorem, ipsum, dolor }" parameter type annotation colon.
672
673(foo:{ a: string, b: number }) => {}
674// Message: There must be a space after "foo" parameter type annotation colon.
675
676({ a, b } :{ a: string, b: number }) => {}
677// Message: There must be a space after "{ a, b }" parameter type annotation colon.
678
679([ a, b ] :string[]) => {}
680// Message: There must be a space after "[ a, b ]" parameter type annotation colon.
681
682type X = { foo:string }
683// Message: There must be a space after "foo" type annotation colon.
684
685// Options: ["always"]
686type X = { foo:string }
687// Message: There must be a space after "foo" type annotation colon.
688
689// Options: ["never"]
690type X = { foo: string }
691// Message: There must be no space after "foo" type annotation colon.
692
693type X = { foo: string }
694// Message: There must be 1 space after "foo" type annotation colon.
695
696type X = { foo?:string }
697// Message: There must be a space after "foo" type annotation colon.
698
699// Options: ["never"]
700type X = { foo?: string }
701// Message: There must be no space after "foo" type annotation colon.
702
703type X = { foo?:?string }
704// Message: There must be a space after "foo" type annotation colon.
705
706type X = { foo?: ?string }
707// Message: There must be 1 space after "foo" type annotation colon.
708
709class X { foo:string }
710// Message: There must be a space after "foo" class property type annotation colon.
711
712// Options: ["never"]
713class X { foo: string }
714// Message: There must be no space after "foo" class property type annotation colon.
715
716class X { foo:?string }
717// Message: There must be a space after "foo" class property type annotation colon.
718
719// Options: ["never"]
720class X { foo: ?string }
721// Message: There must be no space after "foo" class property type annotation colon.
722
723type X = (foo:number) => string
724// Message: There must be a space after "foo" parameter type annotation colon.
725
726// Options: ["never"]
727type X = (foo: number) => string
728// Message: There must be no space after "foo" parameter type annotation colon.
729
730type X = (foo: number) => string
731// Message: There must be 1 space after "foo" parameter type annotation colon.
732
733type X = (foo:?number) => string
734// Message: There must be a space after "foo" parameter type annotation colon.
735
736class X { static foo:number }
737// Message: There must be a space after "foo" class property type annotation colon.
738
739// Options: ["never"]
740class X { static foo: number }
741// Message: There must be no space after "foo" class property type annotation colon.
742
743class X { static foo :number }
744// Message: There must be a space after "foo" class property type annotation colon.
745
746// Options: ["never"]
747class X { static foo : number }
748// Message: There must be no space after "foo" class property type annotation colon.
749
750declare class X { static foo:number }
751// Message: There must be a space after "foo" type annotation colon.
752
753// Options: ["never"]
754declare class X { static foo: number }
755// Message: There must be no space after "foo" type annotation colon.
756
757declare class X { static foo :number }
758// Message: There must be a space after "foo" type annotation colon.
759
760// Options: ["never"]
761declare class X { static foo : number }
762// Message: There must be no space after "foo" type annotation colon.
763```
764
765The following patterns are not considered problems:
766
767```js
768(foo) => {}
769
770(foo: string) => {}
771
772function x(foo: string) {}
773
774class Foo { constructor(foo: string) {} }
775
776(foo: (string|number)) => {}
777
778// Options: ["never"]
779(foo:string) => {}
780
781// Options: ["never"]
782function x(foo:string) {}
783
784// Options: ["never"]
785class Foo { constructor(foo:string) {} }
786
787// Options: ["always"]
788(foo: string) => {}
789
790// Options: ["never"]
791(foo:(() => void)) => {}
792
793// Options: ["always"]
794(foo: (() => void)) => {}
795
796// Options: ["never"]
797():Object => {}
798
799// Options: ["always"]
800(): Object => {}
801
802// Options: ["never"]
803():(number | string) => {}
804
805// Options: ["always"]
806(): (number | string) => {}
807
808// Options: ["never"]
809():number|string => {}
810
811// Options: ["always"]
812(): number|string => {}
813
814// Options: ["never"]
815():(() => void) => {}
816
817// Options: ["always"]
818(): (() => void) => {}
819
820// Options: ["never"]
821():( () => void ) => {}
822
823// Options: ["always"]
824(): ( () => void ) => {}
825
826async function foo({ lorem, ipsum, dolor }: SomeType) {}
827
828({ lorem, ipsum, dolor }: SomeType) => {}
829
830(foo: { a: string, b: number }) => {}
831
832({ a, b }: ?{ a: string, b: number }) => {}
833
834function x({ a, b }: { a: string, b: number }) {}
835
836(): { a: number, b: string } => {}
837
838// Options: ["never"]
839() :{ a:number, b:string } => {}
840
841([ a, b ]: string[]) => {}
842
843type X = { foo: string }
844
845// Options: ["never"]
846type X = { foo:string }
847
848type X = { foo?: string }
849
850type X = { foo?: ?string }
851
852// Options: ["never"]
853type X = { foo?:?string }
854
855class Foo { bar }
856
857class Foo { bar = 3 }
858
859class Foo { bar: string }
860
861class Foo { bar: ?string }
862
863// Options: ["never"]
864class Foo { bar:string }
865
866// Options: ["never"]
867class Foo { bar:?string }
868
869type X = (foo: number) => string;
870
871type X = (foo : number) => string;
872
873type X = (foo: ?number) => string;
874
875type X = (foo? : ?number) => string;
876
877type X = (foo: ?{ x: number }) => string;
878
879// Options: ["never"]
880type X = (foo:number) => string;
881
882// Options: ["never"]
883type X = (foo:?{ x:number }) => string;
884
885class X { static foo : number }
886
887// Options: ["never"]
888class X { static foo :number }
889
890declare class X { static foo : number }
891
892// Options: ["never"]
893declare class X { static foo :number }
894```
895
896
897
898<h3 id="eslint-plugin-flowtype-rules-space-before-type-colon"><code>space-before-type-colon</code></h3>
899
900_The `--fix` option on the command line automatically fixes problems reported by this rule._
901
902Enforces consistent spacing before the type annotation colon.
903
904This 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'`.
905
906The following patterns are considered problems:
907
908```js
909// Options: ["never"]
910(foo : string) => {}
911// Message: There must be no space before "foo" parameter type annotation colon.
912
913// Options: ["never"]
914(foo ? : string) => {}
915// Message: There must be no space before "foo" parameter type annotation colon.
916
917// Options: ["always"]
918(foo: string) => {}
919// Message: There must be a space before "foo" parameter type annotation colon.
920
921// Options: ["always"]
922(foo : string) => {}
923// Message: There must be 1 space before "foo" parameter type annotation colon.
924
925// Options: ["always"]
926(foo?: string) => {}
927// Message: There must be a space before "foo" parameter type annotation colon.
928
929// Options: ["always"]
930(foo ? : string) => {}
931// Message: There must be 1 space before "foo" parameter type annotation colon.
932
933// Options: ["always"]
934(foo ?: string) => {}
935// Message: There must be a space before "foo" parameter type annotation colon.
936
937function x(foo : string) {}
938// Message: There must be no space before "foo" parameter type annotation colon.
939
940// Options: ["always"]
941function x(foo: string) {}
942// Message: There must be a space before "foo" parameter type annotation colon.
943
944var x = function (foo : string) {}
945// Message: There must be no space before "foo" parameter type annotation colon.
946
947// Options: ["always"]
948var x = function (foo: string) {}
949// Message: There must be a space before "foo" parameter type annotation colon.
950
951class Foo { constructor(foo : string ) {} }
952// Message: There must be no space before "foo" parameter type annotation colon.
953
954// Options: ["always"]
955class Foo { constructor(foo: string ) {} }
956// Message: There must be a space before "foo" parameter type annotation colon.
957
958async function foo({ lorem, ipsum, dolor } : SomeType) {}
959// Message: There must be no space before "{ lorem, ipsum, dolor }" parameter type annotation colon.
960
961({ lorem, ipsum, dolor } : SomeType) => {}
962// Message: There must be no space before "{ lorem, ipsum, dolor }" parameter type annotation colon.
963
964(foo : { a: string, b: number }) => {}
965// Message: There must be no space before "foo" parameter type annotation colon.
966
967({ a, b } : { a: string, b: number }) => {}
968// Message: There must be no space before "{ a, b }" parameter type annotation colon.
969
970([ a, b ] : string[]) => {}
971// Message: There must be no space before "[ a, b ]" parameter type annotation colon.
972
973type X = { foo : string }
974// Message: There must be no space before "foo" type annotation colon.
975
976// Options: ["never"]
977type X = { foo : string }
978// Message: There must be no space before "foo" type annotation colon.
979
980// Options: ["always"]
981type X = { foo: string }
982// Message: There must be a space before "foo" type annotation colon.
983
984// Options: ["always"]
985type X = { foo : string }
986// Message: There must be 1 space before "foo" type annotation colon.
987
988type X = { foo? : string }
989// Message: There must be no space before "foo" type annotation colon.
990
991// Options: ["always"]
992type X = { foo?: string }
993// Message: There must be a space before "foo" type annotation colon.
994
995// Options: ["always"]
996type X = { foo? : string }
997// Message: There must be 1 space before "foo" type annotation colon.
998
999// Options: ["always"]
1000type X = { foo ?: string }
1001// Message: There must be a space before "foo" type annotation colon.
1002
1003class X { foo :string }
1004// Message: There must be no space before "foo" class property type annotation colon.
1005
1006// Options: ["always"]
1007class X { foo: string }
1008// Message: There must be a space before "foo" class property type annotation colon.
1009
1010class X { foo :?string }
1011// Message: There must be no space before "foo" class property type annotation colon.
1012
1013// Options: ["always"]
1014class X { foo: ?string }
1015// Message: There must be a space before "foo" class property type annotation colon.
1016
1017type X = (foo :string) => string;
1018// Message: There must be no space before "foo" parameter type annotation colon.
1019
1020// Options: ["always"]
1021type X = (foo:string) => string;
1022// Message: There must be a space before "foo" parameter type annotation colon.
1023
1024// Options: ["always"]
1025type X = (foo :string) => string;
1026// Message: There must be 1 space before "foo" parameter type annotation colon.
1027
1028type X = (foo? :string) => string;
1029// Message: There must be no space before "foo" parameter type annotation colon.
1030
1031type X = (foo? :string) => string;
1032// Message: There must be no space before "foo" parameter type annotation colon.
1033
1034// Options: ["always"]
1035type X = (foo?:string) => string;
1036// Message: There must be a space before "foo" parameter type annotation colon.
1037
1038type X = (foo? :?string) => string;
1039// Message: There must be no space before "foo" parameter type annotation colon.
1040
1041class X { static foo : number }
1042// Message: There must be no space before "foo" class property type annotation colon.
1043
1044class X { static foo :number }
1045// Message: There must be no space before "foo" class property type annotation colon.
1046
1047// Options: ["always"]
1048class X { static foo: number }
1049// Message: There must be a space before "foo" class property type annotation colon.
1050
1051// Options: ["always"]
1052class X { static foo:number }
1053// Message: There must be a space before "foo" class property type annotation colon.
1054
1055declare class Foo { static bar :number; }
1056// Message: There must be no space before "bar" type annotation colon.
1057
1058declare class Foo { static bar : number; }
1059// Message: There must be no space before "bar" type annotation colon.
1060
1061// Options: ["always"]
1062declare class Foo { static bar:number; }
1063// Message: There must be a space before "bar" type annotation colon.
1064
1065// Options: ["always"]
1066declare class Foo { static bar: number; }
1067// Message: There must be a space before "bar" type annotation colon.
1068```
1069
1070The following patterns are not considered problems:
1071
1072```js
1073(foo) => {}
1074
1075(foo: string) => {}
1076
1077(foo?: string) => {}
1078
1079(foo ?: string) => {}
1080
1081// Options: ["never"]
1082(foo: string) => {}
1083
1084// Options: ["always"]
1085(foo : string) => {}
1086
1087// Options: ["always"]
1088(foo? : string) => {}
1089
1090// Options: ["always"]
1091(foo ? : string) => {}
1092
1093// Options: ["always"]
1094(foo ? : string) => {}
1095
1096function x(foo: string) {}
1097
1098// Options: ["always"]
1099function x(foo : string) {}
1100
1101var x = function (foo: string) {}
1102
1103// Options: ["always"]
1104var x = function (foo : string) {}
1105
1106class X { foo({ bar }: Props = this.props) {} }
1107
1108class Foo { constructor(foo: string ) {} }
1109
1110// Options: ["always"]
1111class Foo { constructor(foo : string ) {} }
1112
1113async function foo({ lorem, ipsum, dolor }: SomeType) {}
1114
1115({ lorem, ipsum, dolor }: SomeType) => {}
1116
1117(foo: { a: string, b: number }) => {}
1118
1119({ a, b }: ?{ a: string, b: number }) => {}
1120
1121function x({ a, b }: { a: string, b: number }) {}
1122
1123(): { a: number, b: string } => {}
1124
1125// Options: ["always"]
1126() : { a : number, b : string } => {}
1127
1128([ a, b ]: string[]) => {}
1129
1130type X = { foo: string }
1131
1132// Options: ["always"]
1133type X = { foo : string }
1134
1135type X = { foo?: string }
1136
1137type X = { foo ?: string }
1138
1139// Options: ["always"]
1140type X = { foo? : string }
1141
1142class Foo { bar }
1143
1144class Foo { bar = 3 }
1145
1146class Foo { bar: string }
1147
1148class Foo { bar: ?string }
1149
1150class Foo { bar:?string }
1151
1152// Options: ["always"]
1153class Foo { bar : string }
1154
1155type X = (foo:string) => number;
1156
1157type X = (foo: string) => number;
1158
1159type X = (foo: ?string) => number;
1160
1161type X = (foo?: string) => number;
1162
1163type X = (foo?: ?string) => number;
1164
1165type X = (foo ?: string) => number;
1166
1167// Options: ["always"]
1168type X = (foo? : string) => number
1169
1170// Options: ["always"]
1171type X = (foo? : ?string) => number
1172
1173class X { static foo:number }
1174
1175class X { static foo: number }
1176
1177// Options: ["always"]
1178class X { static foo :number }
1179
1180// Options: ["always"]
1181class X { static foo : number }
1182
1183declare class Foo { static bar:number; }
1184
1185// Options: ["always"]
1186declare class Foo { static bar :number; }
1187
1188declare class Foo { static bar: number; }
1189
1190// Options: ["always"]
1191declare class Foo { static bar : number; }
1192```
1193
1194
1195
1196<h3 id="eslint-plugin-flowtype-rules-type-id-match"><code>type-id-match</code></h3>
1197
1198Enforces a consistent naming pattern for type aliases.
1199
1200<h4 id="eslint-plugin-flowtype-rules-type-id-match-options">Options</h4>
1201
1202This rule needs a text RegExp to operate with Its signature is as follows:
1203
1204```js
1205{
1206 "rules": {
1207 "flowtype/type-id-match": [
1208 2,
1209 "^([A-Z][a-z0-9]+)+Type$"
1210 ]
1211 }
1212}
1213```
1214
1215`'^([A-Z][a-z0-9]+)+Type$'` is the default pattern.
1216
1217The following patterns are considered problems:
1218
1219```js
1220type foo = {};
1221// Message: Type identifier 'foo' does not match pattern '/^([A-Z][a-z0-9]+)+Type$/'.
1222
1223// Options: ["^foo$"]
1224type FooType = {};
1225// Message: Type identifier 'FooType' does not match pattern '/^foo$/'.
1226```
1227
1228The following patterns are not considered problems:
1229
1230```js
1231type FooType = {};
1232
1233// Options: ["^foo$"]
1234type foo = {};
1235```
1236
1237
1238
1239<h3 id="eslint-plugin-flowtype-rules-use-flow-type"><code>use-flow-type</code></h3>
1240
1241Marks Flow [type alias](https://flowtype.org/docs/type-aliases.html) declarations as used.
1242
1243Used to suppress [`no-unused-vars`](http://eslint.org/docs/rules/no-unused-vars) errors that are triggered by type aliases.
1244
1245The following patterns are not considered problems:
1246
1247```js
1248declare class A {}
1249// Additional rules: {"no-unused-vars":1}
1250
1251declare function A(): Y
1252// Additional rules: {"no-unused-vars":1}
1253
1254declare module A {}
1255// Additional rules: {"no-unused-vars":1}
1256
1257declare module A { declare var a: Y }
1258// Additional rules: {"no-unused-vars":1}
1259
1260declare var A: Y
1261// Additional rules: {"no-unused-vars":1}
1262
1263import type A from "a"; (function<T: A>(): T {})
1264// Additional rules: {"no-unused-vars":1}
1265
1266(function<T: A>(): T {}); import type A from "a"
1267// Additional rules: {"no-unused-vars":1}
1268
1269import type {A} from "a"; (function<T: A>(): T {})
1270// Additional rules: {"no-unused-vars":1}
1271
1272(function<T: A>(): T {}); import type {A} from "a"
1273// Additional rules: {"no-unused-vars":1}
1274
1275(function<T: A>(): T {}); import type {a as A} from "a"
1276// Additional rules: {"no-unused-vars":1}
1277
1278type A = {}; function x<Y: A>(i: Y) { i }; x()
1279// Additional rules: {"no-unused-vars":1}
1280
1281function x<Y: A>(i: Y) { i }; type A = {}; x()
1282// Additional rules: {"no-unused-vars":1}
1283
1284type A = {}; function x<Y: A.B.C>(i: Y) { i }; x()
1285// Additional rules: {"no-unused-vars":1}
1286
1287function x<Y: A.B.C>(i: Y) { i }; type A = {}; x()
1288// Additional rules: {"no-unused-vars":1}
1289```
1290
1291
1292
1293<h3 id="eslint-plugin-flowtype-rules-valid-syntax"><code>valid-syntax</code></h3>
1294
1295Checks for simple Flow syntax errors.
1296
1297The following patterns are considered problems:
1298
1299```js
1300function x(foo = "1": string) {}
1301// Message: "foo" parameter type annotation must be placed on left-hand side of assignment.
1302
1303function x(foo = bar(): Type, baz = []: []) {}
1304// Message: "foo" parameter type annotation must be placed on left-hand side of assignment.
1305// Message: "baz" parameter type annotation must be placed on left-hand side of assignment.
1306```
1307
1308The following patterns are not considered problems:
1309
1310```js
1311function x(foo: string = "1") {}
1312
1313function x(foo: Type = bar()) {}
1314```
1315
1316
1317