1 | <a name="eslint-plugin-flowtype"></a>
|
2 | # eslint-plugin-flowtype
|
3 |
|
4 | [![GitSpo Mentions](https://gitspo.com/badges/mentions/gajus/eslint-plugin-flowtype?style=flat-square)](https://gitspo.com/mentions/gajus/eslint-plugin-flowtype)
|
5 | [![NPM version](http://img.shields.io/npm/v/eslint-plugin-flowtype.svg?style=flat-square)](https://www.npmjs.org/package/eslint-plugin-flowtype)
|
6 | [![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)
|
7 | [![js-canonical-style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
|
8 |
|
9 | [Flow type](http://flowtype.org/) linting rules for ESLint.
|
10 |
|
11 | * [eslint-plugin-flowtype](#eslint-plugin-flowtype)
|
12 | * [Installation](#eslint-plugin-flowtype-installation)
|
13 | * [Configuration](#eslint-plugin-flowtype-configuration)
|
14 | * [Shareable configurations](#eslint-plugin-flowtype-configuration-shareable-configurations)
|
15 | * [Community maintained configurations](#eslint-plugin-flowtype-configuration-community-maintained-configurations)
|
16 | * [Settings](#eslint-plugin-flowtype-settings)
|
17 | * [`onlyFilesWithFlowAnnotation`](#eslint-plugin-flowtype-settings-onlyfileswithflowannotation)
|
18 | * [Rules](#eslint-plugin-flowtype-rules)
|
19 | * [`array-style-complex-type`](#eslint-plugin-flowtype-rules-array-style-complex-type)
|
20 | * [`array-style-simple-type`](#eslint-plugin-flowtype-rules-array-style-simple-type)
|
21 | * [`arrow-parens`](#eslint-plugin-flowtype-rules-arrow-parens)
|
22 | * [`boolean-style`](#eslint-plugin-flowtype-rules-boolean-style)
|
23 | * [`define-flow-type`](#eslint-plugin-flowtype-rules-define-flow-type)
|
24 | * [`delimiter-dangle`](#eslint-plugin-flowtype-rules-delimiter-dangle)
|
25 | * [`generic-spacing`](#eslint-plugin-flowtype-rules-generic-spacing)
|
26 | * [`newline-after-flow-annotation`](#eslint-plugin-flowtype-rules-newline-after-flow-annotation)
|
27 | * [`no-dupe-keys`](#eslint-plugin-flowtype-rules-no-dupe-keys)
|
28 | * [`no-existential-type`](#eslint-plugin-flowtype-rules-no-existential-type)
|
29 | * [`no-flow-fix-me-comments`](#eslint-plugin-flowtype-rules-no-flow-fix-me-comments)
|
30 | * [`no-internal-flow-type`](#eslint-plugin-flowtype-rules-no-internal-flow-type)
|
31 | * [`no-mixed`](#eslint-plugin-flowtype-rules-no-mixed)
|
32 | * [`no-mutable-array`](#eslint-plugin-flowtype-rules-no-mutable-array)
|
33 | * [`no-primitive-constructor-types`](#eslint-plugin-flowtype-rules-no-primitive-constructor-types)
|
34 | * [`no-types-missing-file-annotation`](#eslint-plugin-flowtype-rules-no-types-missing-file-annotation)
|
35 | * [`no-unused-expressions`](#eslint-plugin-flowtype-rules-no-unused-expressions)
|
36 | * [`no-weak-types`](#eslint-plugin-flowtype-rules-no-weak-types)
|
37 | * [`object-type-curly-spacing`](#eslint-plugin-flowtype-rules-object-type-curly-spacing)
|
38 | * [`object-type-delimiter`](#eslint-plugin-flowtype-rules-object-type-delimiter)
|
39 | * [`require-compound-type-alias`](#eslint-plugin-flowtype-rules-require-compound-type-alias)
|
40 | * [`require-exact-type`](#eslint-plugin-flowtype-rules-require-exact-type)
|
41 | * [`require-indexer-name`](#eslint-plugin-flowtype-rules-require-indexer-name)
|
42 | * [`require-inexact-type`](#eslint-plugin-flowtype-rules-require-inexact-type)
|
43 | * [`require-parameter-type`](#eslint-plugin-flowtype-rules-require-parameter-type)
|
44 | * [`require-readonly-react-props`](#eslint-plugin-flowtype-rules-require-readonly-react-props)
|
45 | * [`require-return-type`](#eslint-plugin-flowtype-rules-require-return-type)
|
46 | * [`require-types-at-top`](#eslint-plugin-flowtype-rules-require-types-at-top)
|
47 | * [`require-valid-file-annotation`](#eslint-plugin-flowtype-rules-require-valid-file-annotation)
|
48 | * [`require-variable-type`](#eslint-plugin-flowtype-rules-require-variable-type)
|
49 | * [`semi`](#eslint-plugin-flowtype-rules-semi)
|
50 | * [`sort-keys`](#eslint-plugin-flowtype-rules-sort-keys)
|
51 | * [`space-after-type-colon`](#eslint-plugin-flowtype-rules-space-after-type-colon)
|
52 | * [`space-before-generic-bracket`](#eslint-plugin-flowtype-rules-space-before-generic-bracket)
|
53 | * [`space-before-type-colon`](#eslint-plugin-flowtype-rules-space-before-type-colon)
|
54 | * [`spread-exact-type`](#eslint-plugin-flowtype-rules-spread-exact-type)
|
55 | * [`type-id-match`](#eslint-plugin-flowtype-rules-type-id-match)
|
56 | * [`type-import-style`](#eslint-plugin-flowtype-rules-type-import-style)
|
57 | * [`union-intersection-spacing`](#eslint-plugin-flowtype-rules-union-intersection-spacing)
|
58 | * [`use-flow-type`](#eslint-plugin-flowtype-rules-use-flow-type)
|
59 | * [`use-read-only-spread`](#eslint-plugin-flowtype-rules-use-read-only-spread)
|
60 | * [`valid-syntax`](#eslint-plugin-flowtype-rules-valid-syntax)
|
61 |
|
62 |
|
63 | <a name="eslint-plugin-flowtype-installation"></a>
|
64 | ## Installation
|
65 |
|
66 | 1. Install [ESLint](https://www.github.com/eslint/eslint).
|
67 | 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)).
|
68 | 1. Install [`eslint-plugin-flowtype`](https://github.com/gajus/eslint-plugin-flowtype) plugin.
|
69 |
|
70 |
|
71 |
|
72 | ```sh
|
73 | npm install eslint --save-dev
|
74 | npm install babel-eslint --save-dev
|
75 | npm install eslint-plugin-flowtype --save-dev
|
76 |
|
77 | # Or all at once:
|
78 | npm install eslint babel-eslint eslint-plugin-flowtype --save-dev
|
79 | ```
|
80 |
|
81 | <a name="eslint-plugin-flowtype-configuration"></a>
|
82 | ## Configuration
|
83 |
|
84 | 1. Set `parser` property to `babel-eslint`.
|
85 | 1. Add `plugins` section and specify `eslint-plugin-flowtype` as a plugin.
|
86 | 1. Enable rules.
|
87 |
|
88 |
|
89 |
|
90 | ```json
|
91 | {
|
92 | "parser": "babel-eslint",
|
93 | "plugins": [
|
94 | "flowtype"
|
95 | ],
|
96 | "rules": {
|
97 | "flowtype/boolean-style": [
|
98 | 2,
|
99 | "boolean"
|
100 | ],
|
101 | "flowtype/define-flow-type": 1,
|
102 | "flowtype/delimiter-dangle": [
|
103 | 2,
|
104 | "never"
|
105 | ],
|
106 | "flowtype/generic-spacing": [
|
107 | 2,
|
108 | "never"
|
109 | ],
|
110 | "flowtype/no-mixed": 2,
|
111 | "flowtype/no-primitive-constructor-types": 2,
|
112 | "flowtype/no-types-missing-file-annotation": 2,
|
113 | "flowtype/no-weak-types": 2,
|
114 | "flowtype/object-type-delimiter": [
|
115 | 2,
|
116 | "comma"
|
117 | ],
|
118 | "flowtype/require-parameter-type": 2,
|
119 | "flowtype/require-readonly-react-props": 0,
|
120 | "flowtype/require-return-type": [
|
121 | 2,
|
122 | "always",
|
123 | {
|
124 | "annotateUndefined": "never"
|
125 | }
|
126 | ],
|
127 | "flowtype/require-valid-file-annotation": 2,
|
128 | "flowtype/semi": [
|
129 | 2,
|
130 | "always"
|
131 | ],
|
132 | "flowtype/space-after-type-colon": [
|
133 | 2,
|
134 | "always"
|
135 | ],
|
136 | "flowtype/space-before-generic-bracket": [
|
137 | 2,
|
138 | "never"
|
139 | ],
|
140 | "flowtype/space-before-type-colon": [
|
141 | 2,
|
142 | "never"
|
143 | ],
|
144 | "flowtype/type-id-match": [
|
145 | 2,
|
146 | "^([A-Z][a-z0-9]+)+Type$"
|
147 | ],
|
148 | "flowtype/union-intersection-spacing": [
|
149 | 2,
|
150 | "always"
|
151 | ],
|
152 | "flowtype/use-flow-type": 1,
|
153 | "flowtype/valid-syntax": 1
|
154 | },
|
155 | "settings": {
|
156 | "flowtype": {
|
157 | "onlyFilesWithFlowAnnotation": false
|
158 | }
|
159 | }
|
160 | }
|
161 | ```
|
162 |
|
163 | <a name="eslint-plugin-flowtype-configuration-shareable-configurations"></a>
|
164 | ### Shareable configurations
|
165 |
|
166 | <a name="eslint-plugin-flowtype-configuration-shareable-configurations-recommended"></a>
|
167 | #### Recommended
|
168 |
|
169 | This plugin exports a [recommended configuration](./src/configs/recommended.json) that enforces Flow type good practices.
|
170 |
|
171 | To enable this configuration use the extends property in your `.eslintrc` config file:
|
172 |
|
173 | ```json
|
174 | {
|
175 | "extends": [
|
176 | "plugin:flowtype/recommended"
|
177 | ],
|
178 | "plugins": [
|
179 | "flowtype"
|
180 | ]
|
181 | }
|
182 | ```
|
183 |
|
184 | See [ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files) for more information about extending configuration files.
|
185 |
|
186 | <a name="eslint-plugin-flowtype-configuration-community-maintained-configurations"></a>
|
187 | ### Community maintained configurations
|
188 |
|
189 | The following are third-party submitted/ maintained configurations of `eslint-plugin-flowtype`:
|
190 |
|
191 | * https://github.com/wemake-services/eslint-config-flowtype-essential
|
192 |
|
193 | <a name="eslint-plugin-flowtype-settings"></a>
|
194 | ## Settings
|
195 |
|
196 | <a name="eslint-plugin-flowtype-settings-onlyfileswithflowannotation"></a>
|
197 | ### <code>onlyFilesWithFlowAnnotation</code>
|
198 |
|
199 | When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/docs/about-flow.html#gradual) in the first comment.
|
200 |
|
201 | ```js
|
202 | {
|
203 | "settings": {
|
204 | "flowtype": {
|
205 | "onlyFilesWithFlowAnnotation": true
|
206 | }
|
207 | }
|
208 | }
|
209 | ```
|
210 |
|
211 | <a name="eslint-plugin-flowtype-rules"></a>
|
212 | ## Rules
|
213 |
|
214 |
|
215 |
|
216 | <a name="eslint-plugin-flowtype-rules-array-style-complex-type"></a>
|
217 | ### <code>array-style-complex-type</code>
|
218 |
|
219 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
220 |
|
221 | Enforces a particular annotation style of complex types.
|
222 |
|
223 | Type is considered complex in these cases:
|
224 |
|
225 | * [Maybe type](https://flow.org/en/docs/types/maybe/)
|
226 | * [Function type](https://flow.org/en/docs/types/functions/)
|
227 | * [Object type](https://flow.org/en/docs/types/objects/)
|
228 | * [Tuple type](https://flow.org/en/docs/types/tuples/)
|
229 | * [Union type](https://flow.org/en/docs/types/unions/)
|
230 | * [Intersection type](https://flow.org/en/docs/types/intersections/)
|
231 |
|
232 | This rule takes one argument.
|
233 |
|
234 | If it is `'verbose'` then a problem is raised when using `Type[]` instead of `Array<Type>`.
|
235 |
|
236 | If it is `'shorthand'` then a problem is raised when using `Array<Type>` instead of `Type[]`.
|
237 |
|
238 | The default value is `'verbose'`.
|
239 |
|
240 | The following patterns are considered problems:
|
241 |
|
242 | ```js
|
243 | type X = (?string)[]
|
244 | // Message: Use "Array<?string>", not "(?string)[]"
|
245 |
|
246 | // Options: ["verbose"]
|
247 | type X = (?string)[]
|
248 | // Message: Use "Array<?string>", not "(?string)[]"
|
249 |
|
250 | // Options: ["shorthand"]
|
251 | type X = Array<?string>
|
252 | // Message: Use "(?string)[]", not "Array<?string>"
|
253 |
|
254 | // Options: ["shorthand"]
|
255 | type X = Array<{foo: string}>
|
256 | // Message: Use "{foo: string}[]", not "Array<{foo: string}>"
|
257 |
|
258 | type X = (string | number)[]
|
259 | // Message: Use "Array<string | number>", not "(string | number)[]"
|
260 |
|
261 | type X = (string & number)[]
|
262 | // Message: Use "Array<string & number>", not "(string & number)[]"
|
263 |
|
264 | type X = [string, number][]
|
265 | // Message: Use "Array<[string, number]>", not "[string, number][]"
|
266 |
|
267 | type X = {foo: string}[]
|
268 | // Message: Use "Array<{foo: string}>", not "{foo: string}[]"
|
269 |
|
270 | type X = (string => number)[]
|
271 | // Message: Use "Array<string => number>", not "(string => number)[]"
|
272 |
|
273 | type X = {
|
274 | foo: string,
|
275 | bar: number
|
276 | }[]
|
277 | // Message: Use "Array<{ foo: string, bar: number }>", not "{ foo: string, bar: number }[]"
|
278 |
|
279 | type X = {
|
280 | foo: string,
|
281 | bar: number,
|
282 | quo: boolean,
|
283 | hey: Date
|
284 | }[]
|
285 | // Message: Use "Array<Type>", not "Type[]"
|
286 | ```
|
287 |
|
288 | The following patterns are not considered problems:
|
289 |
|
290 | ```js
|
291 | type X = Array<?string>
|
292 |
|
293 | // Options: ["verbose"]
|
294 | type X = Array<?string>
|
295 |
|
296 | // Options: ["shorthand"]
|
297 | type X = (?string)[]
|
298 |
|
299 | // Options: ["shorthand"]
|
300 | type X = Array<string>
|
301 |
|
302 | // Options: ["shorthand"]
|
303 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
304 | type X = Array<?string>
|
305 | ```
|
306 |
|
307 |
|
308 |
|
309 | <a name="eslint-plugin-flowtype-rules-array-style-simple-type"></a>
|
310 | ### <code>array-style-simple-type</code>
|
311 |
|
312 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
313 |
|
314 | Enforces a particular array type annotation style of simple types.
|
315 |
|
316 | Type is considered simple in these cases:
|
317 |
|
318 | * [Primitive types](https://flow.org/en/docs/types/primitives/)
|
319 | * [Literal types](https://flow.org/en/docs/types/literals/)
|
320 | * [Mixed type](https://flow.org/en/docs/types/mixed/)
|
321 | * [Any type](https://flow.org/en/docs/types/any/)
|
322 | * [Class type](https://flow.org/en/docs/types/classes/)
|
323 | * [Generic type](https://flow.org/en/docs/types/generics/)
|
324 | * Array type [shorthand notation](https://flow.org/en/docs/types/arrays/#toc-array-type-shorthand-syntax)
|
325 |
|
326 | This rule takes one argument.
|
327 |
|
328 | If it is `'verbose'` then a problem is raised when using `Type[]` instead of `Array<Type>`.
|
329 |
|
330 | If it is `'shorthand'` then a problem is raised when using `Array<Type>` instead of `Type[]`.
|
331 |
|
332 | The default value is `'verbose'`.
|
333 |
|
334 | The following patterns are considered problems:
|
335 |
|
336 | ```js
|
337 | type X = string[]
|
338 | // Message: Use "Array<string>", not "string[]"
|
339 |
|
340 | // Options: ["verbose"]
|
341 | type X = string[]
|
342 | // Message: Use "Array<string>", not "string[]"
|
343 |
|
344 | // Options: ["shorthand"]
|
345 | type X = Array<string>
|
346 | // Message: Use "string[]", not "Array<string>"
|
347 |
|
348 | type X = Date[]
|
349 | // Message: Use "Array<Date>", not "Date[]"
|
350 |
|
351 | type X = Promise<string>[]
|
352 | // Message: Use "Array<Promise<string>>", not "Promise<string>[]"
|
353 |
|
354 | type X = $Keys<{foo: string}>[]
|
355 | // Message: Use "Array<$Keys<{foo: string}>>", not "$Keys<{foo: string}>[]"
|
356 |
|
357 | type X = any[]
|
358 | // Message: Use "Array<any>", not "any[]"
|
359 |
|
360 | type X = mixed[]
|
361 | // Message: Use "Array<mixed>", not "mixed[]"
|
362 |
|
363 | type X = void[]
|
364 | // Message: Use "Array<void>", not "void[]"
|
365 |
|
366 | type X = null[]
|
367 | // Message: Use "Array<null>", not "null[]"
|
368 |
|
369 | type X = Promise<{
|
370 | foo: string,
|
371 | bar: number
|
372 | }>[]
|
373 | // Message: Use "Array<Promise<{ foo: string, bar: number }>>", not "Promise<{ foo: string, bar: number }>[]"
|
374 |
|
375 | type X = Promise<{
|
376 | foo: string,
|
377 | bar: number,
|
378 | quo: boolean
|
379 | }>[]
|
380 | // Message: Use "Array<Type>", not "Type[]"
|
381 | ```
|
382 |
|
383 | The following patterns are not considered problems:
|
384 |
|
385 | ```js
|
386 | type X = Array<string>
|
387 |
|
388 | // Options: ["verbose"]
|
389 | type X = Array<string>
|
390 |
|
391 | // Options: ["shorthand"]
|
392 | type X = string[]
|
393 |
|
394 | type X = Array<Array<string>>
|
395 |
|
396 | // Options: ["verbose"]
|
397 | type X = (?string)[]
|
398 |
|
399 | // Options: ["verbose"]
|
400 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
401 | type X = string[]
|
402 |
|
403 | type X = Array
|
404 |
|
405 | type X = typeof Array
|
406 | ```
|
407 |
|
408 |
|
409 |
|
410 | <a name="eslint-plugin-flowtype-rules-arrow-parens"></a>
|
411 | ### <code>arrow-parens</code>
|
412 |
|
413 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
414 |
|
415 | Enforces the consistent use of parentheses in arrow functions.
|
416 |
|
417 | This rule has a string option and an object one.
|
418 |
|
419 | String options are:
|
420 |
|
421 | - `"always"` (default) requires parens around arguments in all cases.
|
422 | - `"as-needed"` enforces no braces where they can be omitted.
|
423 |
|
424 | Object properties for variants of the `"as-needed"` option:
|
425 |
|
426 | - `"requireForBlockBody": true` modifies the as-needed rule in order to require parens if the function body is in an instructions block (surrounded by braces).
|
427 |
|
428 | The following patterns are considered problems:
|
429 |
|
430 | ```js
|
431 | a => {}
|
432 | // Message: undefined
|
433 |
|
434 | a => a
|
435 | // Message: undefined
|
436 |
|
437 | a => {
|
438 | }
|
439 | // Message: undefined
|
440 |
|
441 | a.then(foo => {});
|
442 | // Message: undefined
|
443 |
|
444 | a.then(foo => a);
|
445 | // Message: undefined
|
446 |
|
447 | a(foo => { if (true) {}; });
|
448 | // Message: undefined
|
449 |
|
450 | a(async foo => { if (true) {}; });
|
451 | // Message: undefined
|
452 |
|
453 | // Options: ["as-needed"]
|
454 | (a) => a
|
455 | // Message: undefined
|
456 |
|
457 | // Options: ["as-needed"]
|
458 | (a,) => a
|
459 | // Message: undefined
|
460 |
|
461 | // Options: ["as-needed"]
|
462 | async (a) => a
|
463 | // Message: undefined
|
464 |
|
465 | // Options: ["as-needed"]
|
466 | async(a) => a
|
467 | // Message: undefined
|
468 |
|
469 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
470 | a => {}
|
471 | // Message: undefined
|
472 |
|
473 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
474 | (a) => a
|
475 | // Message: undefined
|
476 |
|
477 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
478 | async a => {}
|
479 | // Message: undefined
|
480 |
|
481 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
482 | async (a) => a
|
483 | // Message: undefined
|
484 |
|
485 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
486 | async(a) => a
|
487 | // Message: undefined
|
488 | ```
|
489 |
|
490 | The following patterns are not considered problems:
|
491 |
|
492 | ```js
|
493 | () => {}
|
494 |
|
495 | (a) => {}
|
496 |
|
497 | (a) => a
|
498 |
|
499 | (a) => {
|
500 | }
|
501 |
|
502 | a.then((foo) => {});
|
503 |
|
504 | a.then((foo) => { if (true) {}; });
|
505 |
|
506 | a.then(async (foo) => { if (true) {}; });
|
507 |
|
508 | // Options: ["always"]
|
509 | () => {}
|
510 |
|
511 | // Options: ["always"]
|
512 | (a) => {}
|
513 |
|
514 | // Options: ["always"]
|
515 | (a) => a
|
516 |
|
517 | // Options: ["always"]
|
518 | (a) => {
|
519 | }
|
520 |
|
521 | // Options: ["always"]
|
522 | a.then((foo) => {});
|
523 |
|
524 | // Options: ["always"]
|
525 | a.then((foo) => { if (true) {}; });
|
526 |
|
527 | // Options: ["always"]
|
528 | a.then(async (foo) => { if (true) {}; });
|
529 |
|
530 | // Options: ["as-needed"]
|
531 | () => {}
|
532 |
|
533 | // Options: ["as-needed"]
|
534 | a => {}
|
535 |
|
536 | // Options: ["as-needed"]
|
537 | a => a
|
538 |
|
539 | // Options: ["as-needed"]
|
540 | ([a, b]) => {}
|
541 |
|
542 | // Options: ["as-needed"]
|
543 | ({ a, b }) => {}
|
544 |
|
545 | // Options: ["as-needed"]
|
546 | (a = 10) => {}
|
547 |
|
548 | // Options: ["as-needed"]
|
549 | (...a) => a[0]
|
550 |
|
551 | // Options: ["as-needed"]
|
552 | (a, b) => {}
|
553 |
|
554 | // Options: ["as-needed"]
|
555 | async ([a, b]) => {}
|
556 |
|
557 | // Options: ["as-needed"]
|
558 | async (a, b) => {}
|
559 |
|
560 | // Options: ["as-needed"]
|
561 | (a: T) => a
|
562 |
|
563 | // Options: ["as-needed"]
|
564 | (a): T => a
|
565 |
|
566 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
567 | () => {}
|
568 |
|
569 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
570 | a => a
|
571 |
|
572 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
573 | ([a, b]) => {}
|
574 |
|
575 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
576 | ([a, b]) => a
|
577 |
|
578 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
579 | ({ a, b }) => {}
|
580 |
|
581 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
582 | ({ a, b }) => a + b
|
583 |
|
584 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
585 | (a = 10) => {}
|
586 |
|
587 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
588 | (...a) => a[0]
|
589 |
|
590 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
591 | (a, b) => {}
|
592 |
|
593 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
594 | a => ({})
|
595 |
|
596 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
597 | async a => ({})
|
598 |
|
599 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
600 | async a => a
|
601 |
|
602 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
603 | (a: T) => a
|
604 |
|
605 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
606 | (a): T => a
|
607 |
|
608 | // Options: ["always",{"requireForBlockBody":true}]
|
609 | <T>(a: T) => a
|
610 |
|
611 | // Options: ["as-needed",{"requireForBlockBody":false}]
|
612 | <T>(a: T) => { return a; }
|
613 |
|
614 | // Options: ["always",{"requireForBlockBody":true}]
|
615 | <T>(a: T) => { return a; }
|
616 |
|
617 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
618 | <T>(a: T) => { return a; }
|
619 |
|
620 | // Options: ["as-needed",{"requireForBlockBody":true}]
|
621 | (a): %checks => typeof a === "number"
|
622 | ```
|
623 |
|
624 |
|
625 |
|
626 | <a name="eslint-plugin-flowtype-rules-boolean-style"></a>
|
627 | ### <code>boolean-style</code>
|
628 |
|
629 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
630 |
|
631 | Enforces a particular style for boolean type annotations. This rule takes one argument.
|
632 |
|
633 | If it is `'boolean'` then a problem is raised when using `bool` instead of `boolean`.
|
634 |
|
635 | If it is `'bool'` then a problem is raised when using `boolean` instead of `bool`.
|
636 |
|
637 | The default value is `'boolean'`.
|
638 |
|
639 | The following patterns are considered problems:
|
640 |
|
641 | ```js
|
642 | type X = bool
|
643 | // Message: Use "boolean", not "bool"
|
644 |
|
645 | // Options: ["boolean"]
|
646 | type X = bool
|
647 | // Message: Use "boolean", not "bool"
|
648 |
|
649 | // Options: ["bool"]
|
650 | type X = boolean
|
651 | // Message: Use "bool", not "boolean"
|
652 | ```
|
653 |
|
654 | The following patterns are not considered problems:
|
655 |
|
656 | ```js
|
657 | type X = boolean
|
658 |
|
659 | // Options: ["boolean"]
|
660 | type X = boolean
|
661 |
|
662 | // Options: ["bool"]
|
663 | type X = bool
|
664 |
|
665 | // Options: ["boolean"]
|
666 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
667 | type X = bool
|
668 | ```
|
669 |
|
670 |
|
671 |
|
672 | <a name="eslint-plugin-flowtype-rules-define-flow-type"></a>
|
673 | ### <code>define-flow-type</code>
|
674 |
|
675 | Marks Flow type identifiers as defined.
|
676 |
|
677 | Used to suppress [`no-undef`](http://eslint.org/docs/rules/no-undef) reporting of type identifiers.
|
678 |
|
679 | The following patterns are not considered problems:
|
680 |
|
681 | ```js
|
682 | var a: AType
|
683 | // Additional rules: {"no-undef":2}
|
684 |
|
685 | var a: AType; var b: AType
|
686 | // Additional rules: {"no-undef":2}
|
687 |
|
688 | var a; (a: AType)
|
689 | // Additional rules: {"no-undef":2}
|
690 |
|
691 | var a: AType<BType>
|
692 | // Additional rules: {"no-undef":2}
|
693 |
|
694 | type A = AType
|
695 | // Additional rules: {"no-undef":2}
|
696 |
|
697 | declare type A = number
|
698 | // Additional rules: {"no-undef":2}
|
699 |
|
700 | opaque type A = AType
|
701 | // Additional rules: {"no-undef":2}
|
702 |
|
703 | function f(a: AType) {}
|
704 | // Additional rules: {"no-undef":2}
|
705 |
|
706 | function f(a: AType.a) {}
|
707 | // Additional rules: {"no-undef":2}
|
708 |
|
709 | function f(a: AType.a.b) {}
|
710 | // Additional rules: {"no-undef":2}
|
711 |
|
712 | function f(a): AType {}; var a: AType
|
713 | // Additional rules: {"no-undef":2}
|
714 |
|
715 | function f(a): AType {}
|
716 | // Additional rules: {"no-undef":2}
|
717 |
|
718 | class C { a: AType }
|
719 | // Additional rules: {"no-undef":2}
|
720 |
|
721 | class C { a: AType.a }
|
722 | // Additional rules: {"no-undef":2}
|
723 |
|
724 | class C { a: AType.a.b }
|
725 | // Additional rules: {"no-undef":2}
|
726 |
|
727 | class C implements AType {}
|
728 | // Additional rules: {"no-undef":2}
|
729 |
|
730 | declare interface A {}
|
731 | // Additional rules: {"no-undef":2}
|
732 |
|
733 | ({ a: ({b() {}}: AType) })
|
734 | // Additional rules: {"no-undef":2}
|
735 |
|
736 | type X = {Y<AType>(): BType}
|
737 | // Additional rules: {"no-undef":2}
|
738 |
|
739 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
740 |
|
741 | /**
|
742 | * Copyright 2019 no corp
|
743 | * @flow
|
744 | */
|
745 | type Foo = $ReadOnly<{}>
|
746 | // Additional rules: {"no-undef":2}
|
747 |
|
748 | var a: AType
|
749 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
750 |
|
751 | var a: AType; var b: AType
|
752 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
753 |
|
754 | var a; (a: AType)
|
755 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
756 |
|
757 | var a: AType<BType>
|
758 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
759 |
|
760 | type A = AType
|
761 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
762 |
|
763 | declare type A = number
|
764 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
765 |
|
766 | opaque type A = AType
|
767 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
768 |
|
769 | function f(a: AType) {}
|
770 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
771 |
|
772 | function f(a: AType.a) {}
|
773 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
774 |
|
775 | function f(a: AType.a.b) {}
|
776 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
777 |
|
778 | function f(a): AType {}; var a: AType
|
779 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
780 |
|
781 | function f(a): AType {}
|
782 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
783 |
|
784 | class C { a: AType }
|
785 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
786 |
|
787 | class C { a: AType.a }
|
788 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
789 |
|
790 | class C { a: AType.a.b }
|
791 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
792 |
|
793 | class C implements AType {}
|
794 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
795 |
|
796 | declare interface A {}
|
797 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
798 |
|
799 | ({ a: ({b() {}}: AType) })
|
800 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
801 |
|
802 | type X = {Y<AType>(): BType}
|
803 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
804 |
|
805 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
806 |
|
807 | /**
|
808 | * Copyright 2019 no corp
|
809 | * @flow
|
810 | */
|
811 | type Foo = $ReadOnly<{}>
|
812 | // Additional rules: {"no-undef":2,"no-use-before-define":[2,"nofunc"]}
|
813 | ```
|
814 |
|
815 |
|
816 |
|
817 | <a name="eslint-plugin-flowtype-rules-delimiter-dangle"></a>
|
818 | ### <code>delimiter-dangle</code>
|
819 |
|
820 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
821 |
|
822 | Enforces consistent use of trailing commas in Object and Tuple annotations.
|
823 |
|
824 | This rule takes three arguments where the possible values are the same as ESLint's default `comma-dangle` rule:
|
825 |
|
826 | 1. The first argument is for Object and Tuple annotations. The default value is `'never'`.
|
827 | 2. The second argument is used for Interface annotations. This defaults to the value of the first argument.
|
828 | 3. The third argument is used for inexact object notation (trailing `...`). The default value is `'never'`.
|
829 |
|
830 | If it is `'never'` then a problem is raised when there is a trailing comma.
|
831 |
|
832 | If it is `'always'` then a problem is raised when there is no trailing comma.
|
833 |
|
834 | 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.
|
835 |
|
836 | 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.
|
837 |
|
838 | The following patterns are considered problems:
|
839 |
|
840 | ```js
|
841 | type X = { foo: string, }
|
842 | // Message: Unexpected trailing delimiter
|
843 |
|
844 | // Options: ["never"]
|
845 | type X = { foo: string, }
|
846 | // Message: Unexpected trailing delimiter
|
847 |
|
848 | // Options: ["never"]
|
849 | type X = { foo: string; }
|
850 | // Message: Unexpected trailing delimiter
|
851 |
|
852 | // Options: ["never"]
|
853 | type X = {
|
854 | foo: string,
|
855 | }
|
856 | // Message: Unexpected trailing delimiter
|
857 |
|
858 | // Options: ["always"]
|
859 | type X = { foo: string }
|
860 | // Message: Missing trailing delimiter
|
861 |
|
862 | // Options: ["always"]
|
863 | type X = {
|
864 | foo: string
|
865 | }
|
866 | // Message: Missing trailing delimiter
|
867 |
|
868 | // Options: ["always-multiline"]
|
869 | type X = { foo: string, }
|
870 | // Message: Unexpected trailing delimiter
|
871 |
|
872 | // Options: ["always-multiline"]
|
873 | type X = {
|
874 | foo: string
|
875 | }
|
876 | // Message: Missing trailing delimiter
|
877 |
|
878 | // Options: ["only-multiline"]
|
879 | type X = { foo: string; }
|
880 | // Message: Unexpected trailing delimiter
|
881 |
|
882 | // Options: ["always","never"]
|
883 | interface X { foo: string; }
|
884 | // Message: Unexpected trailing delimiter
|
885 |
|
886 | // Options: ["never"]
|
887 | type X = { [key: string]: number, }
|
888 | // Message: Unexpected trailing delimiter
|
889 |
|
890 | // Options: ["always"]
|
891 | type X = { [key: string]: number }
|
892 | // Message: Missing trailing delimiter
|
893 |
|
894 | // Options: ["always-multiline"]
|
895 | type X = { [key: string]: number, }
|
896 | // Message: Unexpected trailing delimiter
|
897 |
|
898 | // Options: ["always-multiline"]
|
899 | type X = {
|
900 | [key: string]: number
|
901 | }
|
902 | // Message: Missing trailing delimiter
|
903 |
|
904 | // Options: ["only-multiline"]
|
905 | type X = { [key: string]: number; }
|
906 | // Message: Unexpected trailing delimiter
|
907 |
|
908 | // Options: ["never"]
|
909 | type X = { [key: string]: number, foo: string, }
|
910 | // Message: Unexpected trailing delimiter
|
911 |
|
912 | // Options: ["never"]
|
913 | type X = {
|
914 | [key: string]: number,
|
915 | foo: string,
|
916 | }
|
917 | // Message: Unexpected trailing delimiter
|
918 |
|
919 | // Options: ["never"]
|
920 | type X = {
|
921 | [key: string]: number,
|
922 | aReallyLongPropertyNameHere: string,
|
923 | }
|
924 | // Message: Unexpected trailing delimiter
|
925 |
|
926 | // Options: ["always"]
|
927 | type X = { [key: string]: number, foo: string }
|
928 | // Message: Missing trailing delimiter
|
929 |
|
930 | // Options: ["always"]
|
931 | type X = {
|
932 | [key: string]: number;
|
933 | foo: string
|
934 | }
|
935 | // Message: Missing trailing delimiter
|
936 |
|
937 | // Options: ["always-multiline"]
|
938 | type X = { [key: string]: number, foo: string, }
|
939 | // Message: Unexpected trailing delimiter
|
940 |
|
941 | // Options: ["always-multiline"]
|
942 | type X = {
|
943 | [key: string]: number,
|
944 | foo: string
|
945 | }
|
946 | // Message: Missing trailing delimiter
|
947 |
|
948 | // Options: ["only-multiline"]
|
949 | type X = { [key: string]: number, foo: string, }
|
950 | // Message: Unexpected trailing delimiter
|
951 |
|
952 | // Options: ["never"]
|
953 | type X = { foo: string, [key: string]: number, }
|
954 | // Message: Unexpected trailing delimiter
|
955 |
|
956 | // Options: ["never"]
|
957 | type X = {
|
958 | foo: string,
|
959 | [key: string]: number,
|
960 | }
|
961 | // Message: Unexpected trailing delimiter
|
962 |
|
963 | // Options: ["never"]
|
964 | type X = {
|
965 | aReallyLongPropertyNameHere: string,
|
966 | [key: string]: number,
|
967 | }
|
968 | // Message: Unexpected trailing delimiter
|
969 |
|
970 | // Options: ["always"]
|
971 | type X = { foo: string, [key: string]: number }
|
972 | // Message: Missing trailing delimiter
|
973 |
|
974 | // Options: ["always"]
|
975 | type X = { foo: string; [key: string]: number }
|
976 | // Message: Missing trailing delimiter
|
977 |
|
978 | // Options: ["always-multiline"]
|
979 | type X = { foo: string, [key: string]: number; }
|
980 | // Message: Unexpected trailing delimiter
|
981 |
|
982 | // Options: ["always-multiline"]
|
983 | type X = {
|
984 | foo: string,
|
985 | [key: string]: number
|
986 | }
|
987 | // Message: Missing trailing delimiter
|
988 |
|
989 | // Options: ["only-multiline"]
|
990 | type X = { foo: string, [key: string]: number; }
|
991 | // Message: Unexpected trailing delimiter
|
992 |
|
993 | type X = { ..., }
|
994 | // Message: Unexpected trailing delimiter
|
995 |
|
996 | type X = { ...; }
|
997 | // Message: Unexpected trailing delimiter
|
998 |
|
999 | // Options: ["never","never","never"]
|
1000 | type X = { ..., }
|
1001 | // Message: Unexpected trailing delimiter
|
1002 |
|
1003 | // Options: ["never","never","never"]
|
1004 | type X = { ...; }
|
1005 | // Message: Unexpected trailing delimiter
|
1006 |
|
1007 | // Options: ["never","never","always"]
|
1008 | type X = { ... }
|
1009 | // Message: Missing trailing delimiter
|
1010 |
|
1011 | // Options: ["never","never","always-multiline"]
|
1012 | type X = { ..., }
|
1013 | // Message: Unexpected trailing delimiter
|
1014 |
|
1015 | // Options: ["never","never","always-multiline"]
|
1016 | type X = { ...; }
|
1017 | // Message: Unexpected trailing delimiter
|
1018 |
|
1019 | // Options: ["never","never","only-multiline"]
|
1020 | type X = { ..., }
|
1021 | // Message: Unexpected trailing delimiter
|
1022 |
|
1023 | // Options: ["never","never","only-multiline"]
|
1024 | type X = { ...; }
|
1025 | // Message: Unexpected trailing delimiter
|
1026 |
|
1027 | type X = {
|
1028 | ...,
|
1029 | }
|
1030 | // Message: Unexpected trailing delimiter
|
1031 |
|
1032 | type X = {
|
1033 | ...;
|
1034 | }
|
1035 | // Message: Unexpected trailing delimiter
|
1036 |
|
1037 | // Options: ["never","never","never"]
|
1038 | type X = {
|
1039 | ...,
|
1040 | }
|
1041 | // Message: Unexpected trailing delimiter
|
1042 |
|
1043 | // Options: ["never","never","never"]
|
1044 | type X = {
|
1045 | ...;
|
1046 | }
|
1047 | // Message: Unexpected trailing delimiter
|
1048 |
|
1049 | // Options: ["never","never","always"]
|
1050 | type X = {
|
1051 | ...
|
1052 | }
|
1053 | // Message: Missing trailing delimiter
|
1054 |
|
1055 | // Options: ["never","never","always-multiline"]
|
1056 | type X = {
|
1057 | ...
|
1058 | }
|
1059 | // Message: Missing trailing delimiter
|
1060 |
|
1061 | type X = { foo: string, ..., }
|
1062 | // Message: Unexpected trailing delimiter
|
1063 |
|
1064 | type X = { foo: string; ...; }
|
1065 | // Message: Unexpected trailing delimiter
|
1066 |
|
1067 | // Options: ["never","never","never"]
|
1068 | type X = { foo: string, ..., }
|
1069 | // Message: Unexpected trailing delimiter
|
1070 |
|
1071 | // Options: ["never","never","never"]
|
1072 | type X = { foo: string; ...; }
|
1073 | // Message: Unexpected trailing delimiter
|
1074 |
|
1075 | // Options: ["never","never","always"]
|
1076 | type X = { foo: string, ... }
|
1077 | // Message: Missing trailing delimiter
|
1078 |
|
1079 | // Options: ["never","never","always-multiline"]
|
1080 | type X = { foo: string, ..., }
|
1081 | // Message: Unexpected trailing delimiter
|
1082 |
|
1083 | // Options: ["never","never","always-multiline"]
|
1084 | type X = { foo: string; ...; }
|
1085 | // Message: Unexpected trailing delimiter
|
1086 |
|
1087 | // Options: ["never","never","only-multiline"]
|
1088 | type X = { foo: string, ..., }
|
1089 | // Message: Unexpected trailing delimiter
|
1090 |
|
1091 | // Options: ["never","never","only-multiline"]
|
1092 | type X = { foo: string; ...; }
|
1093 | // Message: Unexpected trailing delimiter
|
1094 |
|
1095 | type X = {
|
1096 | foo: string,
|
1097 | ...,
|
1098 | }
|
1099 | // Message: Unexpected trailing delimiter
|
1100 |
|
1101 | type X = {
|
1102 | foo: string;
|
1103 | ...;
|
1104 | }
|
1105 | // Message: Unexpected trailing delimiter
|
1106 |
|
1107 | // Options: ["never","never","never"]
|
1108 | type X = {
|
1109 | foo: string,
|
1110 | ...,
|
1111 | }
|
1112 | // Message: Unexpected trailing delimiter
|
1113 |
|
1114 | // Options: ["never","never","never"]
|
1115 | type X = {
|
1116 | foo: string;
|
1117 | ...;
|
1118 | }
|
1119 | // Message: Unexpected trailing delimiter
|
1120 |
|
1121 | // Options: ["never","never","always"]
|
1122 | type X = {
|
1123 | foo: string,
|
1124 | ...
|
1125 | }
|
1126 | // Message: Missing trailing delimiter
|
1127 |
|
1128 | // Options: ["never","never","always-multiline"]
|
1129 | type X = {
|
1130 | foo: string,
|
1131 | ...
|
1132 | }
|
1133 | // Message: Missing trailing delimiter
|
1134 |
|
1135 | type X = { [key: string]: number, ..., }
|
1136 | // Message: Unexpected trailing delimiter
|
1137 |
|
1138 | type X = { [key: string]: number; ...; }
|
1139 | // Message: Unexpected trailing delimiter
|
1140 |
|
1141 | // Options: ["never","never","never"]
|
1142 | type X = { [key: string]: number, ..., }
|
1143 | // Message: Unexpected trailing delimiter
|
1144 |
|
1145 | // Options: ["never","never","never"]
|
1146 | type X = { [key: string]: number; ...; }
|
1147 | // Message: Unexpected trailing delimiter
|
1148 |
|
1149 | // Options: ["never","never","always"]
|
1150 | type X = { [key: string]: number, ... }
|
1151 | // Message: Missing trailing delimiter
|
1152 |
|
1153 | // Options: ["never","never","always-multiline"]
|
1154 | type X = { [key: string]: number, ..., }
|
1155 | // Message: Unexpected trailing delimiter
|
1156 |
|
1157 | // Options: ["never","never","always-multiline"]
|
1158 | type X = { [key: string]: number; ...; }
|
1159 | // Message: Unexpected trailing delimiter
|
1160 |
|
1161 | // Options: ["never","never","only-multiline"]
|
1162 | type X = { [key: string]: number, ..., }
|
1163 | // Message: Unexpected trailing delimiter
|
1164 |
|
1165 | // Options: ["never","never","only-multiline"]
|
1166 | type X = { [key: string]: number; ...; }
|
1167 | // Message: Unexpected trailing delimiter
|
1168 |
|
1169 | type X = {
|
1170 | [key: string]: number,
|
1171 | ...,
|
1172 | }
|
1173 | // Message: Unexpected trailing delimiter
|
1174 |
|
1175 | type X = {
|
1176 | [key: string]: number;
|
1177 | ...;
|
1178 | }
|
1179 | // Message: Unexpected trailing delimiter
|
1180 |
|
1181 | // Options: ["never","never","never"]
|
1182 | type X = {
|
1183 | [key: string]: number,
|
1184 | ...,
|
1185 | }
|
1186 | // Message: Unexpected trailing delimiter
|
1187 |
|
1188 | // Options: ["never","never","never"]
|
1189 | type X = {
|
1190 | [key: string]: number;
|
1191 | ...;
|
1192 | }
|
1193 | // Message: Unexpected trailing delimiter
|
1194 |
|
1195 | // Options: ["never","never","always"]
|
1196 | type X = {
|
1197 | [key: string]: number,
|
1198 | ...
|
1199 | }
|
1200 | // Message: Missing trailing delimiter
|
1201 |
|
1202 | // Options: ["never","never","always-multiline"]
|
1203 | type X = {
|
1204 | [key: string]: number,
|
1205 | ...
|
1206 | }
|
1207 | // Message: Missing trailing delimiter
|
1208 |
|
1209 | type X = [string, number,]
|
1210 | // Message: Unexpected trailing delimiter
|
1211 |
|
1212 | // Options: ["never"]
|
1213 | type X = [string, number,]
|
1214 | // Message: Unexpected trailing delimiter
|
1215 |
|
1216 | // Options: ["never"]
|
1217 | type X = [
|
1218 | string,
|
1219 | number,
|
1220 | ]
|
1221 | // Message: Unexpected trailing delimiter
|
1222 |
|
1223 | // Options: ["always"]
|
1224 | type X = [string, number]
|
1225 | // Message: Missing trailing delimiter
|
1226 |
|
1227 | // Options: ["always"]
|
1228 | type X = [
|
1229 | string,
|
1230 | number
|
1231 | ]
|
1232 | // Message: Missing trailing delimiter
|
1233 |
|
1234 | // Options: ["always-multiline"]
|
1235 | type X = [string, number,]
|
1236 | // Message: Unexpected trailing delimiter
|
1237 |
|
1238 | // Options: ["always-multiline"]
|
1239 | type X = [
|
1240 | foo, string
|
1241 | ]
|
1242 | // Message: Missing trailing delimiter
|
1243 |
|
1244 | // Options: ["only-multiline"]
|
1245 | type X = [ number, string, ]
|
1246 | // Message: Unexpected trailing delimiter
|
1247 | ```
|
1248 |
|
1249 | The following patterns are not considered problems:
|
1250 |
|
1251 | ```js
|
1252 | type X = { foo: string }
|
1253 |
|
1254 | // Options: ["never"]
|
1255 | type X = { foo: string }
|
1256 |
|
1257 | // Options: ["always"]
|
1258 | type X = { foo: string, }
|
1259 |
|
1260 | // Options: ["always"]
|
1261 | type X = { foo: string; }
|
1262 |
|
1263 | // Options: ["never"]
|
1264 | type X = {
|
1265 | foo: string
|
1266 | }
|
1267 |
|
1268 | // Options: ["always"]
|
1269 | type X = {
|
1270 | foo: string,
|
1271 | }
|
1272 |
|
1273 | // Options: ["always-multiline"]
|
1274 | type X = { foo: string }
|
1275 |
|
1276 | // Options: ["always-multiline"]
|
1277 | type X = {
|
1278 | foo: string,
|
1279 | }
|
1280 |
|
1281 | // Options: ["always-multiline"]
|
1282 | type X = {
|
1283 | foo: string;
|
1284 | }
|
1285 |
|
1286 | // Options: ["only-multiline"]
|
1287 | type X = { foo: string }
|
1288 |
|
1289 | // Options: ["only-multiline"]
|
1290 | type X = {
|
1291 | foo: string
|
1292 | }
|
1293 |
|
1294 | // Options: ["only-multiline"]
|
1295 | type X = {
|
1296 | foo: string,
|
1297 | }
|
1298 |
|
1299 | // Options: ["only-multiline"]
|
1300 | type X = {
|
1301 | foo: string;
|
1302 | }
|
1303 |
|
1304 | // Options: ["never","always"]
|
1305 | interface X { foo: string; }
|
1306 |
|
1307 | // Options: ["never"]
|
1308 | type X = {}
|
1309 |
|
1310 | // Options: ["always"]
|
1311 | type X = {}
|
1312 |
|
1313 | // Options: ["always-multiline"]
|
1314 | type X = {}
|
1315 |
|
1316 | // Options: ["only-multiline"]
|
1317 | type X = {}
|
1318 |
|
1319 | // Options: ["never"]
|
1320 | type X = { [key: string]: number }
|
1321 |
|
1322 | // Options: ["always"]
|
1323 | type X = { [key: string]: number, }
|
1324 |
|
1325 | // Options: ["always"]
|
1326 | type X = { [key: string]: number; }
|
1327 |
|
1328 | // Options: ["always-multiline"]
|
1329 | type X = { [key: string]: number }
|
1330 |
|
1331 | // Options: ["always-multiline"]
|
1332 | type X = {
|
1333 | [key: string]: number,
|
1334 | }
|
1335 |
|
1336 | // Options: ["only-multiline"]
|
1337 | type X = {
|
1338 | [key: string]: number,
|
1339 | }
|
1340 |
|
1341 | // Options: ["only-multiline"]
|
1342 | type X = {
|
1343 | [key: string]: number
|
1344 | }
|
1345 |
|
1346 | // Options: ["only-multiline"]
|
1347 | type X = { [key: string]: number }
|
1348 |
|
1349 | // Options: ["never"]
|
1350 | type X = { [key: string]: number, foo: string }
|
1351 |
|
1352 | // Options: ["always"]
|
1353 | type X = { [key: string]: number, foo: string, }
|
1354 |
|
1355 | // Options: ["always"]
|
1356 | type X = { [key: string]: number; foo: string; }
|
1357 |
|
1358 | // Options: ["always-multiline"]
|
1359 | type X = { [key: string]: number, foo: string }
|
1360 |
|
1361 | // Options: ["always-multiline"]
|
1362 | type X = {
|
1363 | [key: string]: number,
|
1364 | foo: string,
|
1365 | }
|
1366 |
|
1367 | // Options: ["only-multiline"]
|
1368 | type X = {
|
1369 | [key: string]: number,
|
1370 | foo: string,
|
1371 | }
|
1372 |
|
1373 | // Options: ["only-multiline"]
|
1374 | type X = {
|
1375 | [key: string]: number;
|
1376 | foo: string
|
1377 | }
|
1378 |
|
1379 | // Options: ["only-multiline"]
|
1380 | type X = { [key: string]: number, foo: string }
|
1381 |
|
1382 | // Options: ["never"]
|
1383 | type X = { foo: string, [key: string]: number }
|
1384 |
|
1385 | // Options: ["always"]
|
1386 | type X = { foo: string, [key: string]: number, }
|
1387 |
|
1388 | // Options: ["always"]
|
1389 | type X = { foo: string; [key: string]: number; }
|
1390 |
|
1391 | // Options: ["always-multiline"]
|
1392 | type X = { foo: string, [key: string]: number }
|
1393 |
|
1394 | // Options: ["always-multiline"]
|
1395 | type X = {
|
1396 | foo: string,
|
1397 | [key: string]: number,
|
1398 | }
|
1399 |
|
1400 | // Options: ["only-multiline"]
|
1401 | type X = {
|
1402 | foo: string,
|
1403 | [key: string]: number,
|
1404 | }
|
1405 |
|
1406 | // Options: ["only-multiline"]
|
1407 | type X = {
|
1408 | foo: string;
|
1409 | [key: string]: number
|
1410 | }
|
1411 |
|
1412 | // Options: ["only-multiline"]
|
1413 | type X = { foo: string, [key: string]: number }
|
1414 |
|
1415 | type X = { ... }
|
1416 |
|
1417 | // Options: ["never","never","never"]
|
1418 | type X = { ... }
|
1419 |
|
1420 | // Options: ["never","never","always"]
|
1421 | type X = { ..., }
|
1422 |
|
1423 | // Options: ["never","never","always-multiline"]
|
1424 | type X = { ... }
|
1425 |
|
1426 | // Options: ["never","never","only-multiline"]
|
1427 | type X = { ... }
|
1428 |
|
1429 | type X = {
|
1430 | ...
|
1431 | }
|
1432 |
|
1433 | // Options: ["never","never","never"]
|
1434 | type X = {
|
1435 | ...
|
1436 | }
|
1437 |
|
1438 | // Options: ["never","never","always"]
|
1439 | type X = {
|
1440 | ...,
|
1441 | }
|
1442 |
|
1443 | // Options: ["never","never","always"]
|
1444 | type X = {
|
1445 | ...;
|
1446 | }
|
1447 |
|
1448 | // Options: ["never","never","always-multiline"]
|
1449 | type X = {
|
1450 | ...,
|
1451 | }
|
1452 |
|
1453 | // Options: ["never","never","always-multiline"]
|
1454 | type X = {
|
1455 | ...;
|
1456 | }
|
1457 |
|
1458 | // Options: ["never","never","only-multiline"]
|
1459 | type X = {
|
1460 | ...
|
1461 | }
|
1462 |
|
1463 | // Options: ["never","never","only-multiline"]
|
1464 | type X = {
|
1465 | ...,
|
1466 | }
|
1467 |
|
1468 | // Options: ["never","never","only-multiline"]
|
1469 | type X = {
|
1470 | ...;
|
1471 | }
|
1472 |
|
1473 | type X = { foo: string, ... }
|
1474 |
|
1475 | // Options: ["never","never","never"]
|
1476 | type X = { foo: string, ... }
|
1477 |
|
1478 | // Options: ["never","never","always"]
|
1479 | type X = { foo: string, ..., }
|
1480 |
|
1481 | // Options: ["never","never","always"]
|
1482 | type X = { foo: string; ...; }
|
1483 |
|
1484 | // Options: ["never","never","always-multiline"]
|
1485 | type X = { foo: string, ... }
|
1486 |
|
1487 | // Options: ["never","never","only-multiline"]
|
1488 | type X = { foo: string, ... }
|
1489 |
|
1490 | type X = {
|
1491 | foo: string,
|
1492 | ...
|
1493 | }
|
1494 |
|
1495 | // Options: ["never","never","never"]
|
1496 | type X = {
|
1497 | foo: string,
|
1498 | ...
|
1499 | }
|
1500 |
|
1501 | // Options: ["never","never","always"]
|
1502 | type X = {
|
1503 | foo: string,
|
1504 | ...,
|
1505 | }
|
1506 |
|
1507 | // Options: ["never","never","always"]
|
1508 | type X = {
|
1509 | foo: string;
|
1510 | ...;
|
1511 | }
|
1512 |
|
1513 | // Options: ["never","never","always-multiline"]
|
1514 | type X = {
|
1515 | foo: string,
|
1516 | ...,
|
1517 | }
|
1518 |
|
1519 | // Options: ["never","never","always-multiline"]
|
1520 | type X = {
|
1521 | foo: string;
|
1522 | ...;
|
1523 | }
|
1524 |
|
1525 | // Options: ["never","never","only-multiline"]
|
1526 | type X = {
|
1527 | foo: string,
|
1528 | ...
|
1529 | }
|
1530 |
|
1531 | // Options: ["never","never","only-multiline"]
|
1532 | type X = {
|
1533 | foo: string,
|
1534 | ...,
|
1535 | }
|
1536 |
|
1537 | // Options: ["never","never","only-multiline"]
|
1538 | type X = {
|
1539 | foo: string,
|
1540 | ...;
|
1541 | }
|
1542 |
|
1543 | // Options: ["never","never","never"]
|
1544 | type X = { [key: string]: number, ... }
|
1545 |
|
1546 | // Options: ["never","never","always"]
|
1547 | type X = { [key: string]: number, ..., }
|
1548 |
|
1549 | // Options: ["never","never","always"]
|
1550 | type X = { [key: string]: number; ...; }
|
1551 |
|
1552 | // Options: ["never","never","always-multiline"]
|
1553 | type X = { [key: string]: number, ... }
|
1554 |
|
1555 | // Options: ["never","never","only-multiline"]
|
1556 | type X = { [key: string]: number, ... }
|
1557 |
|
1558 | // Options: ["never","never","never"]
|
1559 | type X = {
|
1560 | [key: string]: number,
|
1561 | ...
|
1562 | }
|
1563 |
|
1564 | // Options: ["never","never","always"]
|
1565 | type X = {
|
1566 | [key: string]: number,
|
1567 | ...,
|
1568 | }
|
1569 |
|
1570 | // Options: ["never","never","always"]
|
1571 | type X = {
|
1572 | [key: string]: number;
|
1573 | ...;
|
1574 | }
|
1575 |
|
1576 | // Options: ["never","never","always-multiline"]
|
1577 | type X = {
|
1578 | [key: string]: number,
|
1579 | ...,
|
1580 | }
|
1581 |
|
1582 | // Options: ["never","never","always-multiline"]
|
1583 | type X = {
|
1584 | [key: string]: number;
|
1585 | ...;
|
1586 | }
|
1587 |
|
1588 | // Options: ["never","never","only-multiline"]
|
1589 | type X = {
|
1590 | [key: string]: number,
|
1591 | ...
|
1592 | }
|
1593 |
|
1594 | // Options: ["never","never","only-multiline"]
|
1595 | type X = {
|
1596 | [key: string]: number,
|
1597 | ...,
|
1598 | }
|
1599 |
|
1600 | // Options: ["never","never","only-multiline"]
|
1601 | type X = {
|
1602 | [key: string]: number;
|
1603 | ...;
|
1604 | }
|
1605 |
|
1606 | type X = [string, number]
|
1607 |
|
1608 | // Options: ["never"]
|
1609 | type X = [string, number]
|
1610 |
|
1611 | // Options: ["never"]
|
1612 | type X = [
|
1613 | string,
|
1614 | number
|
1615 | ]
|
1616 |
|
1617 | // Options: ["always"]
|
1618 | type X = [string, number,]
|
1619 |
|
1620 | // Options: ["always"]
|
1621 | type X = [
|
1622 | string,
|
1623 | number,
|
1624 | ]
|
1625 |
|
1626 | // Options: ["always-multiline"]
|
1627 | type X = [ foo, string ]
|
1628 |
|
1629 | // Options: ["always-multiline"]
|
1630 | type X = [
|
1631 | foo, string,
|
1632 | ]
|
1633 |
|
1634 | // Options: ["only-multiline"]
|
1635 | type X = [ number, string ]
|
1636 |
|
1637 | // Options: ["only-multiline"]
|
1638 | type X = [
|
1639 | number,
|
1640 | string
|
1641 | ]
|
1642 |
|
1643 | // Options: ["only-multiline"]
|
1644 | type X = [
|
1645 | number,
|
1646 | string,
|
1647 | ]
|
1648 |
|
1649 | // Options: ["never"]
|
1650 | type X = []
|
1651 |
|
1652 | // Options: ["always"]
|
1653 | type X = []
|
1654 |
|
1655 | // Options: ["always-multiline"]
|
1656 | type X = []
|
1657 |
|
1658 | // Options: ["only-multiline"]
|
1659 | type X = []
|
1660 | ```
|
1661 |
|
1662 |
|
1663 |
|
1664 | <a name="eslint-plugin-flowtype-rules-generic-spacing"></a>
|
1665 | ### <code>generic-spacing</code>
|
1666 |
|
1667 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
1668 |
|
1669 | Enforces consistent spacing within generic type annotation parameters.
|
1670 |
|
1671 | 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.
|
1672 |
|
1673 | The default value is `'never'`.
|
1674 |
|
1675 | The following patterns are considered problems:
|
1676 |
|
1677 | ```js
|
1678 | type X = Promise< string>
|
1679 | // Message: There must be no space at start of "Promise" generic type annotation
|
1680 |
|
1681 | // Options: ["never"]
|
1682 | type X = Promise< string>
|
1683 | // Message: There must be no space at start of "Promise" generic type annotation
|
1684 |
|
1685 | type X = FooBar<string >
|
1686 | // Message: There must be no space at end of "FooBar" generic type annotation
|
1687 |
|
1688 | type X = Promise< string >
|
1689 | // Message: There must be no space at start of "Promise" generic type annotation
|
1690 | // Message: There must be no space at end of "Promise" generic type annotation
|
1691 |
|
1692 | type X = Promise< (foo), bar, (((baz))) >
|
1693 | // Message: There must be no space at start of "Promise" generic type annotation
|
1694 | // Message: There must be no space at end of "Promise" generic type annotation
|
1695 |
|
1696 | // Options: ["always"]
|
1697 | type X = Promise<string >
|
1698 | // Message: There must be a space at start of "Promise" generic type annotation
|
1699 |
|
1700 | // Options: ["always"]
|
1701 | type X = FooBar< string>
|
1702 | // Message: There must be a space at end of "FooBar" generic type annotation
|
1703 |
|
1704 | // Options: ["always"]
|
1705 | type X = Promise<string>
|
1706 | // Message: There must be a space at start of "Promise" generic type annotation
|
1707 | // Message: There must be a space at end of "Promise" generic type annotation
|
1708 |
|
1709 | // Options: ["always"]
|
1710 | type X = Promise<(foo), bar, (((baz)))>
|
1711 | // Message: There must be a space at start of "Promise" generic type annotation
|
1712 | // Message: There must be a space at end of "Promise" generic type annotation
|
1713 |
|
1714 | // Options: ["always"]
|
1715 | type X = FooBar< string >
|
1716 | // Message: There must be one space at start of "FooBar" generic type annotation
|
1717 |
|
1718 | // Options: ["always"]
|
1719 | type X = FooBar< string >
|
1720 | // Message: There must be one space at end of "FooBar" generic type annotation
|
1721 |
|
1722 | // Options: ["always"]
|
1723 | type X = Promise< (foo), bar, (((baz))) >
|
1724 | // Message: There must be one space at start of "Promise" generic type annotation
|
1725 | // Message: There must be one space at end of "Promise" generic type annotation
|
1726 | ```
|
1727 |
|
1728 | The following patterns are not considered problems:
|
1729 |
|
1730 | ```js
|
1731 | type X = Promise<string>
|
1732 |
|
1733 | type X = Promise<(string)>
|
1734 |
|
1735 | type X = Promise<(foo), bar, (((baz)))>
|
1736 |
|
1737 | type X = Promise<
|
1738 | (foo),
|
1739 | bar,
|
1740 | (((baz))),
|
1741 | >
|
1742 |
|
1743 | // Options: ["always"]
|
1744 | type X = Promise< string >
|
1745 |
|
1746 | // Options: ["always"]
|
1747 | type X = Promise< (string) >
|
1748 |
|
1749 | // Options: ["always"]
|
1750 | type X = Promise< (foo), bar, (((baz))) >
|
1751 | ```
|
1752 |
|
1753 |
|
1754 |
|
1755 | <a name="eslint-plugin-flowtype-rules-newline-after-flow-annotation"></a>
|
1756 | ### <code>newline-after-flow-annotation</code>
|
1757 |
|
1758 | This rule requires an empty line after the Flow annotation.
|
1759 |
|
1760 | <a name="eslint-plugin-flowtype-rules-newline-after-flow-annotation-options"></a>
|
1761 | #### Options
|
1762 |
|
1763 | The rule has a string option:
|
1764 |
|
1765 | * `"always"` (default): Enforces that `@flow` annotations be followed by an empty line, separated by newline (LF)
|
1766 | * `"always-windows"`: Identical to "always", but will use a CRLF when autofixing
|
1767 | * `"never"`: Enforces that `@flow` annotations are not followed by empty lines
|
1768 |
|
1769 | ```js
|
1770 | {
|
1771 | "rules": {
|
1772 | "flowtype/newline-after-flow-annotation": [
|
1773 | 2,
|
1774 | "always"
|
1775 | ]
|
1776 | }
|
1777 | }
|
1778 | ```
|
1779 |
|
1780 |
|
1781 | The following patterns are considered problems:
|
1782 |
|
1783 | ```js
|
1784 | // @flow
|
1785 | import Foo from './foo';
|
1786 | // Message: Expected newline after flow annotation
|
1787 |
|
1788 | // Options: ["always"]
|
1789 | // @flow
|
1790 | import Foo from './foo';
|
1791 | // Message: Expected newline after flow annotation
|
1792 |
|
1793 | // Options: ["always-windows"]
|
1794 | // @flow
|
1795 | import Foo from './foo';
|
1796 | // Message: Expected newline after flow annotation
|
1797 |
|
1798 | // Options: ["never"]
|
1799 | // @flow
|
1800 |
|
1801 |
|
1802 | // Message: Expected no newline after flow annotation
|
1803 | ```
|
1804 |
|
1805 | The following patterns are not considered problems:
|
1806 |
|
1807 | ```js
|
1808 | // Options: ["always"]
|
1809 | // @flow
|
1810 |
|
1811 | import Foo from './foo';
|
1812 |
|
1813 | // Options: ["always-windows"]
|
1814 | // @flow
|
1815 |
|
1816 | import Foo from './foo';
|
1817 |
|
1818 | // Options: ["never"]
|
1819 | // @flow
|
1820 | import Foo from './foo';
|
1821 | ```
|
1822 |
|
1823 |
|
1824 |
|
1825 | <a name="eslint-plugin-flowtype-rules-no-dupe-keys"></a>
|
1826 | ### <code>no-dupe-keys</code>
|
1827 |
|
1828 | Checks for duplicate properties in Object annotations.
|
1829 |
|
1830 | This rule mirrors ESLint's [no-dupe-keys](http://eslint.org/docs/rules/no-dupe-keys) rule.
|
1831 |
|
1832 | ```js
|
1833 | {
|
1834 | "rules": {
|
1835 | "flowtype/no-dupe-keys": 2
|
1836 | }
|
1837 | }
|
1838 | ```
|
1839 |
|
1840 | The following patterns are considered problems:
|
1841 |
|
1842 | ```js
|
1843 | type f = { a: number, b: string, a: number }
|
1844 | // Message: Duplicate property.
|
1845 |
|
1846 | type f = { a: number, b: string, a: string }
|
1847 | // Message: Duplicate property.
|
1848 |
|
1849 | type f = { get(key: "a"): string, get(key: "a"): string }
|
1850 | // Message: Duplicate property.
|
1851 |
|
1852 | type f = { get(key: 1): string, get(key: 1): string }
|
1853 | // Message: Duplicate property.
|
1854 |
|
1855 | type f = { get(key: 1.1): string, get(key: 1.1): string }
|
1856 | // Message: Duplicate property.
|
1857 |
|
1858 | type f = { get(key: true): string, get(key: true): string }
|
1859 | // Message: Duplicate property.
|
1860 |
|
1861 | type f = { get(key: {a: 1}): string, get(key: {a: 1}):string }
|
1862 | // Message: Duplicate property.
|
1863 |
|
1864 | var a = "a"; type f = { get(key: a): string, get(key: a): string }
|
1865 | // Message: Duplicate property.
|
1866 |
|
1867 | var b = 1; type f = { get(key: b): string, get(key: b): string }
|
1868 | // Message: Duplicate property.
|
1869 |
|
1870 | var c = true; type f = { get(key: c): string, get(key: c): string }
|
1871 | // Message: Duplicate property.
|
1872 |
|
1873 | var d = {}; type f = { get(key: d): string, get(key: d): string }
|
1874 | // Message: Duplicate property.
|
1875 |
|
1876 | var e = []; type f = { get(key: e): string, get(key: e): string }
|
1877 | // Message: Duplicate property.
|
1878 |
|
1879 | var e = [1, "a"]; type f = { get(key: e): string, get(key: e): string }
|
1880 | // Message: Duplicate property.
|
1881 |
|
1882 | function fn() {}; type f = { get(key: fn): string, get(key: fn): string }
|
1883 | // Message: Duplicate property.
|
1884 | ```
|
1885 |
|
1886 | The following patterns are not considered problems:
|
1887 |
|
1888 | ```js
|
1889 | type FooType = { a: number, b: string, c: number }
|
1890 |
|
1891 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
1892 | type FooType = { a: number, b: string, a: number }
|
1893 |
|
1894 | type f = { get(key: "a"): string, get(key: "b"): string }
|
1895 |
|
1896 | type f = { get(key: 1): string, get(key: 2): string }
|
1897 |
|
1898 | type f = { get(key: 1.1): string, get(key: 1.2): string }
|
1899 |
|
1900 | type f = { get(key: true): string, get(key: false): string }
|
1901 |
|
1902 | type f = { get(key: ["a", 1]): string, get(key: ["a", 2]): string }
|
1903 |
|
1904 | type f = { get(key: ["a", ["b", 1]]): string, get(key: ["a", ["b", 2]]): string }
|
1905 |
|
1906 | type f = { a: number, b: string, c: number }
|
1907 |
|
1908 | type f = { get(key: "a"): string, get(key: "b"): string }
|
1909 |
|
1910 | type f = { get(key: "a"): string, get(key: "a", key2: "b"): string }
|
1911 |
|
1912 | type f = { get(key: "a"): string, get(key: 1): string }
|
1913 |
|
1914 | type f = { get(key: { a: 1 }): string, get(key: { a: 2 }): string}
|
1915 |
|
1916 | var a = {}; var b = {}; type f = { get(key: a): string, get(key: b): string }
|
1917 |
|
1918 | var a = 1; var b = 1; type f = { get(key: a): string, get(key: b): string }
|
1919 |
|
1920 | type a = { b: <C>(config: { ...C, key: string}) => C }
|
1921 |
|
1922 | export interface Foo { get foo(): boolean; get bar(): string; }
|
1923 | ```
|
1924 |
|
1925 |
|
1926 |
|
1927 | <a name="eslint-plugin-flowtype-rules-no-existential-type"></a>
|
1928 | ### <code>no-existential-type</code>
|
1929 |
|
1930 | Disallows use of the existential type (*). [See more](https://flow.org/en/docs/types/utilities/#toc-existential-type)
|
1931 |
|
1932 | ```js
|
1933 | {
|
1934 | "rules": {
|
1935 | "flowtype/no-existential-type": 2
|
1936 | }
|
1937 | }
|
1938 | ```
|
1939 |
|
1940 |
|
1941 | The following patterns are considered problems:
|
1942 |
|
1943 | ```js
|
1944 | type T = *;
|
1945 | // Message: Unexpected use of existential type (*).
|
1946 |
|
1947 | type T = U<*, *>;
|
1948 | // Message: Unexpected use of existential type (*).
|
1949 | // Message: Unexpected use of existential type (*).
|
1950 |
|
1951 | const f: (*) => null = () => null;
|
1952 | // Message: Unexpected use of existential type (*).
|
1953 | ```
|
1954 |
|
1955 | The following patterns are not considered problems:
|
1956 |
|
1957 | ```js
|
1958 | type T = string | null
|
1959 | ```
|
1960 |
|
1961 |
|
1962 |
|
1963 | <a name="eslint-plugin-flowtype-rules-no-flow-fix-me-comments"></a>
|
1964 | ### <code>no-flow-fix-me-comments</code>
|
1965 |
|
1966 | Disallows `$FlowFixMe` comment suppressions.
|
1967 |
|
1968 | This is especially useful as a warning to ensure instances of `$FlowFixMe` in your codebase get fixed over time.
|
1969 |
|
1970 | <a name="eslint-plugin-flowtype-rules-no-flow-fix-me-comments-options-1"></a>
|
1971 | #### Options
|
1972 |
|
1973 | This rule takes an optional RegExp that comments a text RegExp that makes the supression valid.
|
1974 |
|
1975 | ```js
|
1976 | {
|
1977 | "rules": {
|
1978 | "flowtype/no-flow-fix-me-comments": [
|
1979 | 1,
|
1980 | "TODO\s+[0-9]+"
|
1981 | ]
|
1982 | }
|
1983 | }
|
1984 | ```
|
1985 |
|
1986 | The following patterns are considered problems:
|
1987 |
|
1988 | ```js
|
1989 | // $FlowFixMe I am doing something evil here
|
1990 | const text = 'HELLO';
|
1991 | // Message: $FlowFixMe is treated as `any` and should be fixed.
|
1992 |
|
1993 | // Options: ["TODO [0-9]+"]
|
1994 | // $FlowFixMe I am doing something evil here
|
1995 | const text = 'HELLO';
|
1996 | // Message: $FlowFixMe is treated as `any` and should be fixed. Fix it or match `/TODO [0-9]+/`.
|
1997 |
|
1998 | // Options: ["TODO [0-9]+"]
|
1999 | // $FlowFixMe TODO abc 47 I am doing something evil here
|
2000 | const text = 'HELLO';
|
2001 | // Message: $FlowFixMe is treated as `any` and should be fixed. Fix it or match `/TODO [0-9]+/`.
|
2002 |
|
2003 | // $$FlowFixMeProps I am doing something evil here
|
2004 | const text = 'HELLO';
|
2005 | // Message: $FlowFixMe is treated as `any` and should be fixed.
|
2006 |
|
2007 | // Options: ["TODO [0-9]+"]
|
2008 | // $FlowFixMeProps I am doing something evil here
|
2009 | const text = 'HELLO';
|
2010 | // Message: $FlowFixMe is treated as `any` and should be fixed. Fix it or match `/TODO [0-9]+/`.
|
2011 | ```
|
2012 |
|
2013 | The following patterns are not considered problems:
|
2014 |
|
2015 | ```js
|
2016 | const text = 'HELLO';
|
2017 |
|
2018 | // Options: ["TODO [0-9]+"]
|
2019 | // $FlowFixMe TODO 48
|
2020 | const text = 'HELLO';
|
2021 | ```
|
2022 |
|
2023 |
|
2024 |
|
2025 | <a name="eslint-plugin-flowtype-rules-no-internal-flow-type"></a>
|
2026 | ### <code>no-internal-flow-type</code>
|
2027 |
|
2028 | Warns against using internal Flow types such as `React$Node`, `React$Ref` and others and suggests using public alternatives instead (`React.Node`, `React.Ref`, …).
|
2029 |
|
2030 | The following patterns are considered problems:
|
2031 |
|
2032 | ```js
|
2033 | type X = React$AbstractComponent<Config, Instance>
|
2034 | // Message: Type identifier 'React$AbstractComponent' is not allowed. Use 'React.AbstractComponent' instead.
|
2035 |
|
2036 | type X = React$ChildrenArray<string>
|
2037 | // Message: Type identifier 'React$ChildrenArray' is not allowed. Use 'React.ChildrenArray' instead.
|
2038 |
|
2039 | type X = React$ComponentType<Props>
|
2040 | // Message: Type identifier 'React$ComponentType' is not allowed. Use 'React.ComponentType' instead.
|
2041 |
|
2042 | type X = React$Config<Prosp, DefaultProps>
|
2043 | // Message: Type identifier 'React$Config' is not allowed. Use 'React.Config' instead.
|
2044 |
|
2045 | type X = React$Element<typeof Component>
|
2046 | // Message: Type identifier 'React$Element' is not allowed. Use 'React.Element' instead.
|
2047 |
|
2048 | type X = React$ElementConfig<typeof Component>
|
2049 | // Message: Type identifier 'React$ElementConfig' is not allowed. Use 'React.ElementConfig' instead.
|
2050 |
|
2051 | type X = React$ElementProps<typeof Component>
|
2052 | // Message: Type identifier 'React$ElementProps' is not allowed. Use 'React.ElementProps' instead.
|
2053 |
|
2054 | type X = React$ElementRef<typeof Component>
|
2055 | // Message: Type identifier 'React$ElementRef' is not allowed. Use 'React.ElementRef' instead.
|
2056 |
|
2057 | type X = React$ElementType
|
2058 | // Message: Type identifier 'React$ElementType' is not allowed. Use 'React.ElementType' instead.
|
2059 |
|
2060 | type X = React$Key
|
2061 | // Message: Type identifier 'React$Key' is not allowed. Use 'React.Key' instead.
|
2062 |
|
2063 | type X = React$Node
|
2064 | // Message: Type identifier 'React$Node' is not allowed. Use 'React.Node' instead.
|
2065 |
|
2066 | type X = React$Ref<typeof Component>
|
2067 | // Message: Type identifier 'React$Ref' is not allowed. Use 'React.Ref' instead.
|
2068 |
|
2069 | type X = React$StatelessFunctionalComponent<Props>
|
2070 | // Message: Type identifier 'React$StatelessFunctionalComponent' is not allowed. Use 'React.StatelessFunctionalComponent' instead.
|
2071 | ```
|
2072 |
|
2073 | The following patterns are not considered problems:
|
2074 |
|
2075 | ```js
|
2076 | type X = React.AbstractComponent<Config, Instance>
|
2077 |
|
2078 | type X = React.ChildrenArray<string>
|
2079 |
|
2080 | type X = React.ComponentType<Props>
|
2081 |
|
2082 | type X = React.Config<Props, DefaultProps>
|
2083 |
|
2084 | type X = React.Element<typeof Component>
|
2085 |
|
2086 | type X = React.ElementConfig<typeof Component>
|
2087 |
|
2088 | type X = React.ElementProps<typeof Component>
|
2089 |
|
2090 | type X = React.ElementRef<typeof Component>
|
2091 |
|
2092 | type X = React.ElementType
|
2093 |
|
2094 | type X = React.Key
|
2095 |
|
2096 | type X = React.Node
|
2097 |
|
2098 | type X = React.Ref<typeof Component>
|
2099 |
|
2100 | type X = React.StatelessFunctionalComponent<Props>
|
2101 |
|
2102 | type X = React$Rocks
|
2103 | ```
|
2104 |
|
2105 |
|
2106 |
|
2107 | <a name="eslint-plugin-flowtype-rules-no-mixed"></a>
|
2108 | ### <code>no-mixed</code>
|
2109 |
|
2110 | Warns against "mixed" type annotations.
|
2111 | These types are not strict enough and could often be made more specific.
|
2112 |
|
2113 | The following patterns are considered problems:
|
2114 |
|
2115 | The following patterns are considered problems:
|
2116 |
|
2117 | ```js
|
2118 | function foo(thing): mixed {}
|
2119 | // Message: Unexpected use of mixed type
|
2120 |
|
2121 | function foo(thing): Promise<mixed> {}
|
2122 | // Message: Unexpected use of mixed type
|
2123 |
|
2124 | function foo(thing): Promise<Promise<mixed>> {}
|
2125 | // Message: Unexpected use of mixed type
|
2126 | ```
|
2127 |
|
2128 | The following patterns are not considered problems:
|
2129 |
|
2130 | ```js
|
2131 | function foo(thing): string {}
|
2132 |
|
2133 | function foo(thing): Promise<string> {}
|
2134 |
|
2135 | function foo(thing): Promise<Promise<string>> {}
|
2136 |
|
2137 | (foo?: string) => {}
|
2138 |
|
2139 | (foo: ?string) => {}
|
2140 |
|
2141 | (foo: { a: string }) => {}
|
2142 |
|
2143 | (foo: { a: ?string }) => {}
|
2144 |
|
2145 | (foo: string[]) => {}
|
2146 |
|
2147 | type Foo = string
|
2148 |
|
2149 | type Foo = { a: string }
|
2150 |
|
2151 | type Foo = { (a: string): string }
|
2152 |
|
2153 | function foo(thing: string) {}
|
2154 |
|
2155 | var foo: string
|
2156 |
|
2157 | class Foo { props: string }
|
2158 | ```
|
2159 |
|
2160 |
|
2161 |
|
2162 | <a name="eslint-plugin-flowtype-rules-no-mutable-array"></a>
|
2163 | ### <code>no-mutable-array</code>
|
2164 |
|
2165 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
2166 |
|
2167 | Requires use of [`$ReadOnlyArray`](https://github.com/facebook/flow/blob/v0.46.0/lib/core.js#L185) instead of just `Array` or array [shorthand notation](https://flow.org/en/docs/types/arrays/#toc-array-type-shorthand-syntax). `$ReadOnlyArray` is immutable array collection type and the superclass of Array and tuple types in Flow. Use of `$ReadOnlyArray` instead of `Array` can solve some "problems" in typing with Flow (e.g., [1](https://github.com/facebook/flow/issues/3425), [2](https://github.com/facebook/flow/issues/4251)).
|
2168 |
|
2169 | General reasons for using immutable data structures:
|
2170 |
|
2171 | * They are simpler to construct, test, and use
|
2172 | * They help to avoid temporal coupling
|
2173 | * Their usage is side-effect free (no defensive copies)
|
2174 | * Identity mutability problem is avoided
|
2175 | * They always have failure atomicity
|
2176 | * They are much easier to cache
|
2177 |
|
2178 | Note that initialization of a variable with an empty array is considered valid (e.g., `const values: Array<string> = [];`). This behavior resembles the behavior of Flow's [unsealed objects](https://flow.org/en/docs/types/objects/#toc-unsealed-objects), as it is assumed that empty array is intended to be mutated.
|
2179 |
|
2180 | The following patterns are considered problems:
|
2181 |
|
2182 | ```js
|
2183 | type X = Array<string>
|
2184 | // Message: Use "$ReadOnlyArray" instead of "Array"
|
2185 |
|
2186 | type X = string[]
|
2187 | // Message: Use "$ReadOnlyArray" instead of array shorthand notation
|
2188 |
|
2189 | const values: Array<Array<string>> = [];
|
2190 | // Message: Use "$ReadOnlyArray" instead of "Array"
|
2191 |
|
2192 | let values: Array<Array<string>>;
|
2193 | // Message: Use "$ReadOnlyArray" instead of "Array"
|
2194 | // Message: Use "$ReadOnlyArray" instead of "Array"
|
2195 | ```
|
2196 |
|
2197 | The following patterns are not considered problems:
|
2198 |
|
2199 | ```js
|
2200 | type X = $ReadOnlyArray<string>
|
2201 |
|
2202 | const values: Array<$ReadOnlyArray<string>> = [];
|
2203 |
|
2204 | const values: $ReadOnlyArray<string>[] = [];
|
2205 |
|
2206 | const values: Array<$ReadOnlyArray<string>> = new Array();
|
2207 |
|
2208 | const values: Array<$ReadOnlyArray<string>> = Array();
|
2209 | ```
|
2210 |
|
2211 |
|
2212 |
|
2213 | <a name="eslint-plugin-flowtype-rules-no-primitive-constructor-types"></a>
|
2214 | ### <code>no-primitive-constructor-types</code>
|
2215 |
|
2216 | Disallows use of primitive constructors as types, such as `Boolean`, `Number` and `String`. [See more](https://flowtype.org/docs/builtins.html).
|
2217 |
|
2218 | ```js
|
2219 | {
|
2220 | "rules": {
|
2221 | "flowtype/no-primitive-constructor-types": 2
|
2222 | }
|
2223 | }
|
2224 | ```
|
2225 |
|
2226 | The following patterns are considered problems:
|
2227 |
|
2228 | ```js
|
2229 | type x = Number
|
2230 | // Message: Unexpected use of Number constructor type.
|
2231 |
|
2232 | type x = String
|
2233 | // Message: Unexpected use of String constructor type.
|
2234 |
|
2235 | type x = Boolean
|
2236 | // Message: Unexpected use of Boolean constructor type.
|
2237 |
|
2238 | type x = { a: Number }
|
2239 | // Message: Unexpected use of Number constructor type.
|
2240 |
|
2241 | type x = { a: String }
|
2242 | // Message: Unexpected use of String constructor type.
|
2243 |
|
2244 | type x = { a: Boolean }
|
2245 | // Message: Unexpected use of Boolean constructor type.
|
2246 |
|
2247 | (x: Number) => {}
|
2248 | // Message: Unexpected use of Number constructor type.
|
2249 |
|
2250 | (x: String) => {}
|
2251 | // Message: Unexpected use of String constructor type.
|
2252 |
|
2253 | (x: Boolean) => {}
|
2254 | // Message: Unexpected use of Boolean constructor type.
|
2255 | ```
|
2256 |
|
2257 | The following patterns are not considered problems:
|
2258 |
|
2259 | ```js
|
2260 | type x = number
|
2261 |
|
2262 | type x = string
|
2263 |
|
2264 | type x = boolean
|
2265 |
|
2266 | type x = { a: number }
|
2267 |
|
2268 | type x = { a: string }
|
2269 |
|
2270 | type x = { a: boolean }
|
2271 |
|
2272 | (x: number) => {}
|
2273 |
|
2274 | (x: string) => {}
|
2275 |
|
2276 | (x: boolean) => {}
|
2277 |
|
2278 | type x = MyNumber
|
2279 |
|
2280 | type x = MyString
|
2281 |
|
2282 | type x = MyBoolean
|
2283 | ```
|
2284 |
|
2285 |
|
2286 |
|
2287 | <a name="eslint-plugin-flowtype-rules-no-types-missing-file-annotation"></a>
|
2288 | ### <code>no-types-missing-file-annotation</code>
|
2289 |
|
2290 | Disallows Flow type imports, aliases, and annotations in files missing a valid Flow file declaration (or a @noflow annotation).
|
2291 |
|
2292 | ```js
|
2293 | {
|
2294 | "rules": {
|
2295 | "flowtype/no-types-missing-file-annotation": 2
|
2296 | }
|
2297 | }
|
2298 | ```
|
2299 |
|
2300 | The following patterns are considered problems:
|
2301 |
|
2302 | ```js
|
2303 | const x: number = 42;
|
2304 | // Message: Type annotations require valid Flow declaration.
|
2305 |
|
2306 | type FooType = number;
|
2307 | // Message: Type aliases require valid Flow declaration.
|
2308 |
|
2309 | import type A from "a"
|
2310 | // Message: Type imports require valid Flow declaration.
|
2311 |
|
2312 | import type {A} from "a"
|
2313 | // Message: Type imports require valid Flow declaration.
|
2314 |
|
2315 | import {type A} from "a"
|
2316 | // Message: Type imports require valid Flow declaration.
|
2317 |
|
2318 | export type {A} from "a"
|
2319 | // Message: Type exports require valid Flow declaration.
|
2320 |
|
2321 | function t<T>(): T{}
|
2322 | // Message: Type annotations require valid Flow declaration.
|
2323 |
|
2324 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
2325 | const x: number = 42;
|
2326 | // Message: Type annotations require valid Flow declaration.
|
2327 | ```
|
2328 |
|
2329 | The following patterns are not considered problems:
|
2330 |
|
2331 | ```js
|
2332 | // @flow
|
2333 | const x: number = 42;
|
2334 |
|
2335 | /* @flow weak */
|
2336 | type FooType = number;
|
2337 |
|
2338 | /* @noflow */
|
2339 | type FooType = number;
|
2340 |
|
2341 | /* @noflow */
|
2342 | import type A from "a"
|
2343 |
|
2344 | /* @noflow */
|
2345 | import {type A} from "a"
|
2346 |
|
2347 | /* @noflow */
|
2348 | export type {A} from "a"
|
2349 |
|
2350 | // an unrelated comment
|
2351 | // @flow
|
2352 | export type {A} from "a"
|
2353 | ```
|
2354 |
|
2355 |
|
2356 |
|
2357 | <a name="eslint-plugin-flowtype-rules-no-unused-expressions"></a>
|
2358 | ### <code>no-unused-expressions</code>
|
2359 |
|
2360 | An extension of [ESLint's `no-unused-expressions`](https://eslint.org/docs/rules/no-unused-expressions).
|
2361 | This rule ignores type cast expressions and optional call expressions, but otherwise behaves the same as ESLint's
|
2362 | `no-unused-expressions`.
|
2363 |
|
2364 | Bare type casts are useful, for example to assert the exhaustiveness of a `switch`:
|
2365 |
|
2366 | ```js
|
2367 | type Action
|
2368 | = { type: 'FOO', doFoo: (_: number) => void }
|
2369 | | { type: 'BAR', doBar: (_: string) => void };
|
2370 |
|
2371 | type State = { foo: number, bar: string };
|
2372 |
|
2373 | function runFooBar(action: Action, state: State): void {
|
2374 | switch (action.type) {
|
2375 | case 'FOO':
|
2376 | doFoo(state.foo);
|
2377 | break;
|
2378 | case 'BAR':
|
2379 | doBar(state.bar);
|
2380 | break;
|
2381 | default:
|
2382 | (action: empty); // type error when `Action` is extended with new types
|
2383 | console.error(`Impossible action: ${action.toString()}`);
|
2384 | }
|
2385 | }
|
2386 | ```
|
2387 |
|
2388 | This rule takes the same arguments as ESLint's `no-unused-expressions`. See
|
2389 | [that rule's documentation](https://eslint.org/docs/rules/no-unused-expressions) for details.
|
2390 |
|
2391 | The following patterns are considered problems:
|
2392 |
|
2393 | ```js
|
2394 | foo + 1
|
2395 | // Message: Expected an assignment or function call and instead saw an expression.
|
2396 |
|
2397 | x?.y
|
2398 | // Message: Expected an assignment or function call and instead saw an expression.
|
2399 | ```
|
2400 |
|
2401 | The following patterns are not considered problems:
|
2402 |
|
2403 | ```js
|
2404 | (foo: number)
|
2405 |
|
2406 | x?.y()
|
2407 | ```
|
2408 |
|
2409 |
|
2410 |
|
2411 | <a name="eslint-plugin-flowtype-rules-no-weak-types"></a>
|
2412 | ### <code>no-weak-types</code>
|
2413 |
|
2414 | Warns against weak type annotations *any*, *Object* and *Function*.
|
2415 | These types can cause flow to silently skip over portions of your code,
|
2416 | which would have otherwise caused type errors.
|
2417 |
|
2418 | This rule optionally takes one argument, an object to configure which type warnings to enable. By default, all of the
|
2419 | warnings are enabled. e.g. to disable the `any` warning (allowing it to exist in your code), while continuing to warn
|
2420 | about `Object` and `Function`:
|
2421 |
|
2422 | ```js
|
2423 | {
|
2424 | "rules": {
|
2425 | "flowtype/no-weak-types": [2, {
|
2426 | "any": false,
|
2427 | "Object": true,
|
2428 | "Function": true
|
2429 | }]
|
2430 | }
|
2431 | }
|
2432 |
|
2433 | // or, the following is equivalent as default is true:
|
2434 |
|
2435 | {
|
2436 | "rules": {
|
2437 | "flowtype/no-weak-types": [2, {
|
2438 | "any": false
|
2439 | }]
|
2440 | }
|
2441 | }
|
2442 | ```
|
2443 |
|
2444 | The following patterns are considered problems:
|
2445 |
|
2446 | ```js
|
2447 | function foo(thing): any {}
|
2448 | // Message: Unexpected use of weak type "any"
|
2449 |
|
2450 | function foo(thing): Promise<any> {}
|
2451 | // Message: Unexpected use of weak type "any"
|
2452 |
|
2453 | function foo(thing): Promise<Promise<any>> {}
|
2454 | // Message: Unexpected use of weak type "any"
|
2455 |
|
2456 | function foo(thing): Object {}
|
2457 | // Message: Unexpected use of weak type "Object"
|
2458 |
|
2459 | function foo(thing): Promise<Object> {}
|
2460 | // Message: Unexpected use of weak type "Object"
|
2461 |
|
2462 | function foo(thing): Promise<Promise<Object>> {}
|
2463 | // Message: Unexpected use of weak type "Object"
|
2464 |
|
2465 | function foo(thing): Function {}
|
2466 | // Message: Unexpected use of weak type "Function"
|
2467 |
|
2468 | function foo(thing): Promise<Function> {}
|
2469 | // Message: Unexpected use of weak type "Function"
|
2470 |
|
2471 | function foo(thing): Promise<Promise<Function>> {}
|
2472 | // Message: Unexpected use of weak type "Function"
|
2473 |
|
2474 | (foo: any) => {}
|
2475 | // Message: Unexpected use of weak type "any"
|
2476 |
|
2477 | (foo: Function) => {}
|
2478 | // Message: Unexpected use of weak type "Function"
|
2479 |
|
2480 | (foo?: any) => {}
|
2481 | // Message: Unexpected use of weak type "any"
|
2482 |
|
2483 | (foo?: Function) => {}
|
2484 | // Message: Unexpected use of weak type "Function"
|
2485 |
|
2486 | (foo: { a: any }) => {}
|
2487 | // Message: Unexpected use of weak type "any"
|
2488 |
|
2489 | (foo: { a: Object }) => {}
|
2490 | // Message: Unexpected use of weak type "Object"
|
2491 |
|
2492 | (foo: any[]) => {}
|
2493 | // Message: Unexpected use of weak type "any"
|
2494 |
|
2495 | type Foo = any
|
2496 | // Message: Unexpected use of weak type "any"
|
2497 |
|
2498 | type Foo = Function
|
2499 | // Message: Unexpected use of weak type "Function"
|
2500 |
|
2501 | type Foo = { a: any }
|
2502 | // Message: Unexpected use of weak type "any"
|
2503 |
|
2504 | type Foo = { a: Object }
|
2505 | // Message: Unexpected use of weak type "Object"
|
2506 |
|
2507 | type Foo = { (a: Object): string }
|
2508 | // Message: Unexpected use of weak type "Object"
|
2509 |
|
2510 | type Foo = { (a: string): Function }
|
2511 | // Message: Unexpected use of weak type "Function"
|
2512 |
|
2513 | function foo(thing: any) {}
|
2514 | // Message: Unexpected use of weak type "any"
|
2515 |
|
2516 | function foo(thing: Object) {}
|
2517 | // Message: Unexpected use of weak type "Object"
|
2518 |
|
2519 | var foo: Function
|
2520 | // Message: Unexpected use of weak type "Function"
|
2521 |
|
2522 | var foo: Object
|
2523 | // Message: Unexpected use of weak type "Object"
|
2524 |
|
2525 | class Foo { props: any }
|
2526 | // Message: Unexpected use of weak type "any"
|
2527 |
|
2528 | class Foo { props: Object }
|
2529 | // Message: Unexpected use of weak type "Object"
|
2530 |
|
2531 | var foo: any
|
2532 | // Message: Unexpected use of weak type "any"
|
2533 |
|
2534 | // Options: [{"Function":false}]
|
2535 | type X = any; type Y = Function; type Z = Object
|
2536 | // Message: Unexpected use of weak type "any"
|
2537 | // Message: Unexpected use of weak type "Object"
|
2538 |
|
2539 | // Options: [{"any":false,"Object":false}]
|
2540 | type X = any; type Y = Function; type Z = Object
|
2541 | // Message: Unexpected use of weak type "Function"
|
2542 | ```
|
2543 |
|
2544 | The following patterns are not considered problems:
|
2545 |
|
2546 | ```js
|
2547 | function foo(thing): string {}
|
2548 |
|
2549 | function foo(thing): Promise<string> {}
|
2550 |
|
2551 | function foo(thing): Promise<Promise<string>> {}
|
2552 |
|
2553 | (foo?: string) => {}
|
2554 |
|
2555 | (foo: ?string) => {}
|
2556 |
|
2557 | (foo: { a: string }) => {}
|
2558 |
|
2559 | (foo: { a: ?string }) => {}
|
2560 |
|
2561 | (foo: string[]) => {}
|
2562 |
|
2563 | type Foo = string
|
2564 |
|
2565 | type Foo = { a: string }
|
2566 |
|
2567 | type Foo = { (a: string): string }
|
2568 |
|
2569 | function foo(thing: string) {}
|
2570 |
|
2571 | var foo: string
|
2572 |
|
2573 | class Foo { props: string }
|
2574 |
|
2575 | // Options: [{"any":false,"Object":false}]
|
2576 | type X = any; type Y = Object
|
2577 |
|
2578 | // Options: [{"Function":false}]
|
2579 | type X = Function
|
2580 |
|
2581 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
2582 | function foo(thing): Function {}
|
2583 | ```
|
2584 |
|
2585 |
|
2586 |
|
2587 | <a name="eslint-plugin-flowtype-rules-object-type-curly-spacing"></a>
|
2588 | ### <code>object-type-curly-spacing</code>
|
2589 |
|
2590 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
2591 |
|
2592 | This rule enforces consistent spacing inside braces of object types.
|
2593 |
|
2594 | <a name="eslint-plugin-flowtype-rules-object-type-curly-spacing-options-2"></a>
|
2595 | #### Options
|
2596 |
|
2597 | The rule has a string option:
|
2598 |
|
2599 | * `"never"` (default): disallows spacing inside of braces.
|
2600 | * `"always"`: requires spacing inside of braces.
|
2601 |
|
2602 |
|
2603 | The following patterns are considered problems:
|
2604 |
|
2605 | ```js
|
2606 | type obj = { "foo": "bar" }
|
2607 | // Message: There should be no space after "{".
|
2608 | // Message: There should be no space before "}".
|
2609 |
|
2610 | type obj = {"foo": "bar" }
|
2611 | // Message: There should be no space before "}".
|
2612 |
|
2613 | type obj = {"foo": "bar", ... }
|
2614 | // Message: There should be no space before "}".
|
2615 |
|
2616 | type obj = {|"foo": "bar" |}
|
2617 | // Message: There should be no space before "|}".
|
2618 |
|
2619 | type obj = {"foo": "bar", [key: string]: string }
|
2620 | // Message: There should be no space before "}".
|
2621 |
|
2622 | type obj = { baz: {"foo": "qux"}, bar: 4}
|
2623 | // Message: There should be no space after "{".
|
2624 |
|
2625 | // Options: ["always"]
|
2626 | type obj = {"foo": "bar"}
|
2627 | // Message: A space is required after "{".
|
2628 | // Message: A space is required before "}".
|
2629 |
|
2630 | // Options: ["always"]
|
2631 | type obj = {"foo": "bar" }
|
2632 | // Message: A space is required after "{".
|
2633 |
|
2634 | // Options: ["always"]
|
2635 | type obj = { baz: {"foo": "qux"}, bar: 4}
|
2636 | // Message: A space is required before "}".
|
2637 | // Message: A space is required after "{".
|
2638 | // Message: A space is required before "}".
|
2639 |
|
2640 | // Options: ["always"]
|
2641 | type obj = { baz: { "foo": "qux" }, bar: 4}
|
2642 | // Message: A space is required before "}".
|
2643 |
|
2644 | // Options: ["always"]
|
2645 | type obj = { "foo": "bar", ...}
|
2646 | // Message: A space is required before "}".
|
2647 |
|
2648 | // Options: ["always"]
|
2649 | type obj = {|"foo": "bar" |}
|
2650 | // Message: A space is required after "{|".
|
2651 |
|
2652 | // Options: ["always"]
|
2653 | type obj = {"foo": "bar", [key: string]: string }
|
2654 | // Message: A space is required after "{".
|
2655 | ```
|
2656 |
|
2657 | The following patterns are not considered problems:
|
2658 |
|
2659 | ```js
|
2660 | type obj = {baz: {"foo": "qux"}, bar: 4}
|
2661 |
|
2662 | type obj = {foo: {"foo": "qux"}}
|
2663 |
|
2664 | type obj = {foo: "bar"}
|
2665 |
|
2666 | type obj = {foo: "bar"
|
2667 | }
|
2668 |
|
2669 | type obj = {
|
2670 | foo: "bar"}
|
2671 |
|
2672 | type obj = {
|
2673 | foo: "bar"}
|
2674 |
|
2675 | type obj = {|"foo": "bar"|}
|
2676 |
|
2677 | type obj = {"foo": "bar", [key: string]: string}
|
2678 |
|
2679 | // Options: ["always"]
|
2680 | type obj = { baz: { "foo": "qux" }, bar: 4 }
|
2681 |
|
2682 | // Options: ["always"]
|
2683 | type obj = {}
|
2684 |
|
2685 | // Options: ["always"]
|
2686 | type obj = {
|
2687 | foo: "bar"
|
2688 | }
|
2689 |
|
2690 | // Options: ["always"]
|
2691 | type obj = { baz: 4 }
|
2692 |
|
2693 | // Options: ["always"]
|
2694 | type obj = {| "foo": "bar" |}
|
2695 |
|
2696 | // Options: ["always"]
|
2697 | type obj = { "foo": "bar", [key: string]: string }
|
2698 |
|
2699 | // Options: ["always"]
|
2700 | type obj = { baz: { "foo": "qux" }, bar: 4 }
|
2701 |
|
2702 | // Options: ["always"]
|
2703 | type obj = {
|
2704 | baz: { "foo": "qux" }, bar: 4
|
2705 | }
|
2706 | ```
|
2707 |
|
2708 |
|
2709 |
|
2710 | <a name="eslint-plugin-flowtype-rules-object-type-delimiter"></a>
|
2711 | ### <code>object-type-delimiter</code>
|
2712 |
|
2713 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
2714 |
|
2715 | Enforces consistent separators between properties in Flow object types.
|
2716 |
|
2717 | This rule takes one argument.
|
2718 |
|
2719 | If it is `'comma'` then a problem is raised when using `;` as a separator.
|
2720 |
|
2721 | If it is `'semicolon'` then a problem is raised when using `,` as a separator.
|
2722 |
|
2723 | The default value is `'comma'`.
|
2724 |
|
2725 | _This rule is ported from `babel/flow-object-type`, however the default option was changed._
|
2726 |
|
2727 | The following patterns are considered problems:
|
2728 |
|
2729 | ```js
|
2730 | // Options: ["semicolon"]
|
2731 | type Foo = { a: Foo, b: Bar }
|
2732 | // Message: Prefer semicolons to commas in object and class types
|
2733 |
|
2734 | // Options: ["comma"]
|
2735 | type Foo = { a: Foo; b: Bar }
|
2736 | // Message: Prefer commas to semicolons in object and class types
|
2737 |
|
2738 | // Options: ["semicolon"]
|
2739 | type Foo = { [a: string]: Foo, [b: string]: Bar }
|
2740 | // Message: Prefer semicolons to commas in object and class types
|
2741 |
|
2742 | // Options: ["comma"]
|
2743 | type Foo = { [a: string]: Foo; [b: string]: Bar }
|
2744 | // Message: Prefer commas to semicolons in object and class types
|
2745 |
|
2746 | // Options: ["semicolon"]
|
2747 | type Foo = { (): Foo, (): Bar }
|
2748 | // Message: Prefer semicolons to commas in object and class types
|
2749 |
|
2750 | // Options: ["comma"]
|
2751 | type Foo = { (): Foo; (): Bar }
|
2752 | // Message: Prefer commas to semicolons in object and class types
|
2753 |
|
2754 | // Options: ["semicolon"]
|
2755 | declare class Foo { a: Foo, }
|
2756 | // Message: Prefer semicolons to commas in object and class types
|
2757 |
|
2758 | // Options: ["comma"]
|
2759 | declare class Foo { a: Foo; }
|
2760 | // Message: Prefer commas to semicolons in object and class types
|
2761 |
|
2762 | // Options: ["semicolon"]
|
2763 | declare class Foo { [a: string]: Foo, }
|
2764 | // Message: Prefer semicolons to commas in object and class types
|
2765 |
|
2766 | // Options: ["comma"]
|
2767 | declare class Foo { a: Foo; }
|
2768 | // Message: Prefer commas to semicolons in object and class types
|
2769 |
|
2770 | // Options: ["semicolon"]
|
2771 | declare class Foo { (): Foo, }
|
2772 | // Message: Prefer semicolons to commas in object and class types
|
2773 |
|
2774 | // Options: ["comma"]
|
2775 | declare class Foo { (): Foo; }
|
2776 | // Message: Prefer commas to semicolons in object and class types
|
2777 |
|
2778 | // Options: ["semicolon"]
|
2779 | declare class Foo { static (): Foo, }
|
2780 | // Message: Prefer semicolons to commas in object and class types
|
2781 |
|
2782 | // Options: ["comma"]
|
2783 | declare class Foo { static (): Foo; }
|
2784 | // Message: Prefer commas to semicolons in object and class types
|
2785 | ```
|
2786 |
|
2787 | The following patterns are not considered problems:
|
2788 |
|
2789 | ```js
|
2790 | // Options: ["semicolon"]
|
2791 | type Foo = { a: Foo; b: Bar }
|
2792 |
|
2793 | // Options: ["comma"]
|
2794 | type Foo = { a: Foo, b: Bar }
|
2795 |
|
2796 | // Options: ["semicolon"]
|
2797 | type Foo = { [a: string]: Foo; [b: string]: Bar }
|
2798 |
|
2799 | // Options: ["comma"]
|
2800 | type Foo = { [a: string]: Foo, [b: string]: Bar }
|
2801 |
|
2802 | // Options: ["semicolon"]
|
2803 | type Foo = { (): Foo; (): Bar }
|
2804 |
|
2805 | // Options: ["comma"]
|
2806 | type Foo = { (): Foo, (): Bar }
|
2807 |
|
2808 | type Foo = { a: Foo, b: Bar }
|
2809 |
|
2810 | type Foo = { [a: string]: Foo, [b: string]: Bar }
|
2811 |
|
2812 | type Foo = { (): Foo, (): Bar }
|
2813 |
|
2814 | // Options: ["semicolon"]
|
2815 | declare class Foo { a: Foo; }
|
2816 |
|
2817 | // Options: ["comma"]
|
2818 | declare class Foo { a: Foo, }
|
2819 |
|
2820 | // Options: ["semicolon"]
|
2821 | declare class Foo { [a: string]: Foo; }
|
2822 |
|
2823 | // Options: ["comma"]
|
2824 | declare class Foo { [a: string]: Foo, }
|
2825 |
|
2826 | // Options: ["semicolon"]
|
2827 | declare class Foo { (): Foo; }
|
2828 |
|
2829 | // Options: ["comma"]
|
2830 | declare class Foo { (): Foo, }
|
2831 |
|
2832 | // Options: ["semicolon"]
|
2833 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
2834 | type Foo = { a: Foo, b: Bar }
|
2835 | ```
|
2836 |
|
2837 |
|
2838 |
|
2839 | <a name="eslint-plugin-flowtype-rules-require-compound-type-alias"></a>
|
2840 | ### <code>require-compound-type-alias</code>
|
2841 |
|
2842 | Requires to make a type alias for all [union](https://flow.org/en/docs/types/unions/) and [intersection](https://flow.org/en/docs/types/intersections/) types. If these are used in "raw" forms it might be tempting to just copy & paste them around the code. However, this brings sort of a source code pollution and unnecessary changes on several parts when these compound types need to be changed.
|
2843 |
|
2844 | <a name="eslint-plugin-flowtype-rules-require-compound-type-alias-options-3"></a>
|
2845 | #### Options
|
2846 |
|
2847 | The rule has two options:
|
2848 |
|
2849 | 1. a string option
|
2850 |
|
2851 | * `"always"` (default)
|
2852 | * `"never"`
|
2853 |
|
2854 | 2. an object
|
2855 |
|
2856 | ```js
|
2857 | {
|
2858 | "rules": {
|
2859 | "flowtype/require-compound-type-alias": [
|
2860 | 2,
|
2861 | "always",
|
2862 | {
|
2863 | "allowNull": true
|
2864 | }
|
2865 | ]
|
2866 | }
|
2867 | }
|
2868 | ```
|
2869 |
|
2870 | * `allowNull` – allows compound types where one of the members is a `null`, e.g. `string | null`.
|
2871 |
|
2872 | The following patterns are considered problems:
|
2873 |
|
2874 | ```js
|
2875 | // Options: ["always",{"allowNull":false}]
|
2876 | const foo: string | null = null;
|
2877 | // Message: All union types must be declared with named type alias.
|
2878 |
|
2879 | function foo(bar: "A" | "B") {}
|
2880 | // Message: All union types must be declared with named type alias.
|
2881 |
|
2882 | const foo: "A" | "B" = "A";
|
2883 | // Message: All union types must be declared with named type alias.
|
2884 |
|
2885 | type Foo = { bar: "A" | "B" };
|
2886 | // Message: All union types must be declared with named type alias.
|
2887 |
|
2888 | function foo(bar: { n: number } | { s: string }) {}
|
2889 | // Message: All union types must be declared with named type alias.
|
2890 |
|
2891 | function foo(bar: { n: number } & { s: string }) {}
|
2892 | // Message: All intersection types must be declared with named type alias.
|
2893 |
|
2894 | const foo: { n: number } & { s: string } = { n: 0, s: "" };
|
2895 | // Message: All intersection types must be declared with named type alias.
|
2896 |
|
2897 | type Foo = { bar: { n: number } & { s: string } };
|
2898 | // Message: All intersection types must be declared with named type alias.
|
2899 |
|
2900 | function foo(bar: { n: number } & { s: string }) {}
|
2901 | // Message: All intersection types must be declared with named type alias.
|
2902 | ```
|
2903 |
|
2904 | The following patterns are not considered problems:
|
2905 |
|
2906 | ```js
|
2907 | const foo: string | null = null;
|
2908 |
|
2909 | // Options: ["always",{"allowNull":true}]
|
2910 | const foo: string | null = null;
|
2911 |
|
2912 | type Foo = "A" | "B";
|
2913 |
|
2914 | type Bar = "A" | "B"; function foo(bar: Bar) {}
|
2915 |
|
2916 | type Foo = { disjoint: "A", n: number } | { disjoint: "B", s: string };
|
2917 |
|
2918 | type Foo = { n: number } & { s: string };
|
2919 |
|
2920 | type Bar = { n: number } & { s: string }; function foo(bar: Bar) {}
|
2921 |
|
2922 | // Options: ["never"]
|
2923 | function foo(bar: "A" | "B") {}
|
2924 |
|
2925 | // Options: ["never"]
|
2926 | function foo(bar: { n: number } & { s: string }) {}
|
2927 | ```
|
2928 |
|
2929 |
|
2930 |
|
2931 | <a name="eslint-plugin-flowtype-rules-require-exact-type"></a>
|
2932 | ### <code>require-exact-type</code>
|
2933 |
|
2934 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
2935 |
|
2936 | This rule enforces [exact object types](https://flow.org/en/docs/types/objects/#toc-exact-object-types).
|
2937 |
|
2938 | <a name="eslint-plugin-flowtype-rules-require-exact-type-options-4"></a>
|
2939 | #### Options
|
2940 |
|
2941 | The rule has one string option:
|
2942 |
|
2943 | * `"always"` (default): Report all object type definitions that aren't exact.
|
2944 | * `"never"`: Report all object type definitions that are exact.
|
2945 |
|
2946 | ```js
|
2947 | {
|
2948 | "rules": {
|
2949 | "flowtype/require-exact-type": [
|
2950 | 2,
|
2951 | "always"
|
2952 | ]
|
2953 | }
|
2954 | }
|
2955 |
|
2956 | {
|
2957 | "rules": {
|
2958 | "flowtype/require-exact-type": [
|
2959 | 2,
|
2960 | "never"
|
2961 | ]
|
2962 | }
|
2963 | }
|
2964 | ```
|
2965 |
|
2966 | The following patterns are considered problems:
|
2967 |
|
2968 | ```js
|
2969 | type foo = {};
|
2970 | // Message: Object type must be exact.
|
2971 |
|
2972 | type foo = { bar: string };
|
2973 | // Message: Object type must be exact.
|
2974 |
|
2975 | // Options: ["always"]
|
2976 | type foo = Array<{bar: string}>;
|
2977 | // Message: Object type must be exact.
|
2978 |
|
2979 | // Options: ["always"]
|
2980 | (foo: Array<{bar: string}>) => {};
|
2981 | // Message: Object type must be exact.
|
2982 |
|
2983 | // Options: ["always"]
|
2984 | interface StackFrame {
|
2985 | colno?: number;
|
2986 | lineno?: number;
|
2987 | filename?: string;
|
2988 | function?: { name: string };
|
2989 | }
|
2990 | // Message: Object type must be exact.
|
2991 |
|
2992 | // Options: ["never"]
|
2993 | type foo = {| |};
|
2994 | // Message: Object type must not be exact.
|
2995 |
|
2996 | // Options: ["never"]
|
2997 | type foo = {| bar: string |};
|
2998 | // Message: Object type must not be exact.
|
2999 |
|
3000 | // Options: ["never"]
|
3001 | type foo = { bar: {| baz: string |} };
|
3002 | // Message: Object type must not be exact.
|
3003 |
|
3004 | // Options: ["never"]
|
3005 | type foo = Array<{| bar: string |}>;
|
3006 | // Message: Object type must not be exact.
|
3007 |
|
3008 | // Options: ["never"]
|
3009 | (foo: Array<{| bar: string |}>) => {};
|
3010 | // Message: Object type must not be exact.
|
3011 |
|
3012 | // Options: ["never"]
|
3013 | interface StackFrame {
|
3014 | colno?: number;
|
3015 | lineno?: number;
|
3016 | filename?: string;
|
3017 | function?: {| name: string |};
|
3018 | }
|
3019 | // Message: Object type must not be exact.
|
3020 | ```
|
3021 |
|
3022 | The following patterns are not considered problems:
|
3023 |
|
3024 | ```js
|
3025 | type foo = {| |};
|
3026 |
|
3027 | type foo = {| bar: string |};
|
3028 |
|
3029 | type foo = { [key: string]: string };
|
3030 |
|
3031 | type foo = number;
|
3032 |
|
3033 | // Options: ["always"]
|
3034 | type foo = {| |};
|
3035 |
|
3036 | // Options: ["always"]
|
3037 | type foo = {| bar: string |};
|
3038 |
|
3039 | // Options: ["always"]
|
3040 | type foo = {| bar: {| baz: string |} |};
|
3041 |
|
3042 | // Options: ["always"]
|
3043 | type foo = Array<{| bar: string |}>;
|
3044 |
|
3045 | // Options: ["always"]
|
3046 | type foo = number;
|
3047 |
|
3048 | // Options: ["always"]
|
3049 | interface StackFrame {
|
3050 | colno?: number;
|
3051 | lineno?: number;
|
3052 | filename?: string;
|
3053 | function?: {| name: string |};
|
3054 | }
|
3055 |
|
3056 | // Options: ["never"]
|
3057 | type foo = { };
|
3058 |
|
3059 | // Options: ["never"]
|
3060 | type foo = { bar: string };
|
3061 |
|
3062 | // Options: ["never"]
|
3063 | type foo = { bar: { baz: string } };
|
3064 |
|
3065 | // Options: ["never"]
|
3066 | type foo = Array<{bar: string}>;
|
3067 |
|
3068 | // Options: ["never"]
|
3069 | type foo = number;
|
3070 |
|
3071 | // Options: ["always"]
|
3072 | interface StackFrame {
|
3073 | colno?: number;
|
3074 | lineno?: number;
|
3075 | filename?: string;
|
3076 | function?: {| name: string |};
|
3077 | }
|
3078 |
|
3079 | type A = { a: string, ... }
|
3080 | ```
|
3081 |
|
3082 |
|
3083 |
|
3084 | <a name="eslint-plugin-flowtype-rules-require-indexer-name"></a>
|
3085 | ### <code>require-indexer-name</code>
|
3086 |
|
3087 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
3088 |
|
3089 | This rule validates Flow object indexer name.
|
3090 |
|
3091 | <a name="eslint-plugin-flowtype-rules-require-indexer-name-options-5"></a>
|
3092 | #### Options
|
3093 |
|
3094 | The rule has a string option:
|
3095 |
|
3096 | * `"never"` (default): Never report files that are missing an indexer key name.
|
3097 | * `"always"`: Always report files that are missing an indexer key name.
|
3098 |
|
3099 | ```js
|
3100 | {
|
3101 | "rules": {
|
3102 | "flowtype/require-indexer-name": [
|
3103 | 2,
|
3104 | "always"
|
3105 | ]
|
3106 | }
|
3107 | }
|
3108 | ```
|
3109 |
|
3110 | The following patterns are considered problems:
|
3111 |
|
3112 | ```js
|
3113 | type foo = { [string]: number };
|
3114 | // Message: All indexers must be declared with key name.
|
3115 | ```
|
3116 |
|
3117 | The following patterns are not considered problems:
|
3118 |
|
3119 | ```js
|
3120 | type foo = { [key: string]: number };
|
3121 |
|
3122 | // Options: ["never"]
|
3123 | type foo = { [key: string]: number };
|
3124 |
|
3125 | // Options: ["never"]
|
3126 | type foo = { [string]: number };
|
3127 | ```
|
3128 |
|
3129 |
|
3130 |
|
3131 | <a name="eslint-plugin-flowtype-rules-require-inexact-type"></a>
|
3132 | ### <code>require-inexact-type</code>
|
3133 |
|
3134 | This rule enforces explicit inexact object types.
|
3135 |
|
3136 | <a name="eslint-plugin-flowtype-rules-require-inexact-type-options-6"></a>
|
3137 | #### Options
|
3138 |
|
3139 | The rule has one string option:
|
3140 |
|
3141 | - `"always"` (default): Report all object type definitions that aren't explicit inexact, but ignore exact objects.
|
3142 | - `"never"`: Report all object type definitions that are explicit inexact.
|
3143 |
|
3144 | ```js
|
3145 | {
|
3146 | "rules": {
|
3147 | "flowtype/require-inexact-type": [
|
3148 | 2,
|
3149 | "always"
|
3150 | ]
|
3151 | }
|
3152 | }
|
3153 |
|
3154 | {
|
3155 | "rules": {
|
3156 | "flowtype/require-inexact-type": [
|
3157 | 2,
|
3158 | "never"
|
3159 | ]
|
3160 | }
|
3161 | }
|
3162 | ```
|
3163 |
|
3164 | The following patterns are considered problems:
|
3165 |
|
3166 | ```js
|
3167 | type foo = {};
|
3168 | // Message: Type must be explicit inexact.
|
3169 |
|
3170 | type foo = { bar: string };
|
3171 | // Message: Type must be explicit inexact.
|
3172 |
|
3173 | // Options: ["always"]
|
3174 | type foo = {};
|
3175 | // Message: Type must be explicit inexact.
|
3176 |
|
3177 | // Options: ["always"]
|
3178 | type foo = { bar: string };
|
3179 | // Message: Type must be explicit inexact.
|
3180 |
|
3181 | // Options: ["never"]
|
3182 | type foo = {...};
|
3183 | // Message: Type must not be explicit inexact.
|
3184 |
|
3185 | // Options: ["never"]
|
3186 | type foo = { bar: string, ... };
|
3187 | // Message: Type must not be explicit inexact.
|
3188 | ```
|
3189 |
|
3190 | The following patterns are not considered problems:
|
3191 |
|
3192 | ```js
|
3193 | type foo = { foo: string, ... };
|
3194 |
|
3195 | interface Foo { foo: string }
|
3196 |
|
3197 | declare class Foo { foo: string }
|
3198 |
|
3199 | type foo = {| |};
|
3200 |
|
3201 | type foo = {| bar: string |};
|
3202 |
|
3203 | type foo = { [key: string]: string, ... };
|
3204 |
|
3205 | type foo = number;
|
3206 |
|
3207 | // Options: ["always"]
|
3208 | type foo = {| |};
|
3209 |
|
3210 | // Options: ["always"]
|
3211 | type foo = {...};
|
3212 |
|
3213 | // Options: ["always"]
|
3214 | type foo = { bar: string, ... };
|
3215 |
|
3216 | // Options: ["always"]
|
3217 | type foo = {| bar: string |};
|
3218 |
|
3219 | // Options: ["always"]
|
3220 | type foo = number;
|
3221 |
|
3222 | // Options: ["never"]
|
3223 | type foo = { };
|
3224 |
|
3225 | // Options: ["never"]
|
3226 | type foo = {| |};
|
3227 |
|
3228 | // Options: ["never"]
|
3229 | type foo = { bar: string };
|
3230 |
|
3231 | // Options: ["never"]
|
3232 | type foo = {| bar: string |};
|
3233 |
|
3234 | // Options: ["never"]
|
3235 | type foo = number;
|
3236 | ```
|
3237 |
|
3238 |
|
3239 |
|
3240 | <a name="eslint-plugin-flowtype-rules-require-parameter-type"></a>
|
3241 | ### <code>require-parameter-type</code>
|
3242 |
|
3243 | Requires that all function parameters have type annotations.
|
3244 |
|
3245 | <a name="eslint-plugin-flowtype-rules-require-parameter-type-options-7"></a>
|
3246 | #### Options
|
3247 |
|
3248 | You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
|
3249 |
|
3250 | Alternatively, you can want to exclude only concise arrow functions (e.g. `x => x * 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
|
3251 |
|
3252 | ```js
|
3253 | {
|
3254 | "rules": {
|
3255 | "flowtype/require-parameter-type": [
|
3256 | 2,
|
3257 | {
|
3258 | "excludeArrowFunctions": true
|
3259 | }
|
3260 | ]
|
3261 | }
|
3262 | }
|
3263 |
|
3264 | {
|
3265 | "rules": {
|
3266 | "flowtype/require-parameter-type": [
|
3267 | 2,
|
3268 | {
|
3269 | "excludeArrowFunctions": "expressionsOnly"
|
3270 | }
|
3271 | ]
|
3272 | }
|
3273 | }
|
3274 | ```
|
3275 |
|
3276 | You can exclude parameters that match a certain regex by using `excludeParameterMatch`.
|
3277 |
|
3278 | ```js
|
3279 | {
|
3280 | "rules": {
|
3281 | "flowtype/require-parameter-type": [
|
3282 | 2,
|
3283 | {
|
3284 | "excludeParameterMatch": "^_"
|
3285 | }
|
3286 | ]
|
3287 | }
|
3288 | }
|
3289 | ```
|
3290 |
|
3291 | This excludes all parameters that start with an underscore (`_`).
|
3292 | The default pattern is `a^`, which doesn't match anything, i.e., all parameters are checked.
|
3293 |
|
3294 | The following patterns are considered problems:
|
3295 |
|
3296 | ```js
|
3297 | (foo) => {}
|
3298 | // Message: Missing "foo" parameter type annotation.
|
3299 |
|
3300 | function x(foo) {}
|
3301 | // Message: Missing "foo" parameter type annotation.
|
3302 |
|
3303 | // Options: [{"excludeArrowFunctions":true}]
|
3304 | function x(foo) {}
|
3305 | // Message: Missing "foo" parameter type annotation.
|
3306 |
|
3307 | (foo = 'FOO') => {}
|
3308 | // Message: Missing "foo" parameter type annotation.
|
3309 |
|
3310 | (...foo) => {}
|
3311 | // Message: Missing "foo" parameter type annotation.
|
3312 |
|
3313 | ({foo}) => {}
|
3314 | // Message: Missing "{foo}" parameter type annotation.
|
3315 |
|
3316 | ([foo]) => {}
|
3317 | // Message: Missing "[foo]" parameter type annotation.
|
3318 |
|
3319 | ({foo = 1} = {}) => {}
|
3320 | // Message: Missing "{foo = 1}" parameter type annotation.
|
3321 |
|
3322 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
3323 | // @flow
|
3324 | (foo) => {}
|
3325 | // Message: Missing "foo" parameter type annotation.
|
3326 |
|
3327 | // Options: [{"excludeArrowFunctions":"expressionsOnly"}]
|
3328 | (foo) => {}
|
3329 | // Message: Missing "foo" parameter type annotation.
|
3330 |
|
3331 | // Options: [{"excludeArrowFunctions":"expressionsOnly"}]
|
3332 | function x(foo) {}
|
3333 | // Message: Missing "foo" parameter type annotation.
|
3334 |
|
3335 | // Options: [{"excludeParameterMatch":"^_"}]
|
3336 | (_foo: number, bar) => {}
|
3337 | // Message: Missing "bar" parameter type annotation.
|
3338 |
|
3339 | // Options: [{"excludeParameterMatch":"^_"}]
|
3340 | (_foo, bar) => {}
|
3341 | // Message: Missing "bar" parameter type annotation.
|
3342 | ```
|
3343 |
|
3344 | The following patterns are not considered problems:
|
3345 |
|
3346 | ```js
|
3347 | (foo: string) => {}
|
3348 |
|
3349 | (foo: string = 'FOO') => {}
|
3350 |
|
3351 | (...foo: string) => {}
|
3352 |
|
3353 | const f: Foo = (a, b) => 42;
|
3354 |
|
3355 | ({foo}: {foo: string}) => {}
|
3356 |
|
3357 | ([foo]: Array) => {}
|
3358 |
|
3359 | type fn = (a: string, b: number) => number;
|
3360 | const f: fn = (a, b) => {}
|
3361 |
|
3362 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
3363 | (foo) => {}
|
3364 |
|
3365 | // Options: [{"excludeArrowFunctions":true}]
|
3366 | (foo) => {}
|
3367 |
|
3368 | // Options: [{"excludeArrowFunctions":"expressionsOnly"}]
|
3369 | (foo) => 3
|
3370 |
|
3371 | // Options: [{"excludeParameterMatch":"^_"}]
|
3372 | (_foo, bar: string) => {}
|
3373 |
|
3374 | // Options: [{"excludeParameterMatch":"^_"}]
|
3375 | (_foo: number, bar: string) => {}
|
3376 |
|
3377 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
3378 | (foo) => {}
|
3379 | ```
|
3380 |
|
3381 |
|
3382 |
|
3383 | <a name="eslint-plugin-flowtype-rules-require-readonly-react-props"></a>
|
3384 | ### <code>require-readonly-react-props</code>
|
3385 |
|
3386 | This rule validates that React props are marked as $ReadOnly. React props are immutable and modifying them could lead to unexpected results. Marking prop shapes as $ReadOnly avoids these issues.
|
3387 |
|
3388 | The rule tries its best to work with both class and functional components. For class components, it does a fuzzy check for one of "Component", "PureComponent", "React.Component" and "React.PureComponent". It doesn't actually infer that those identifiers resolve to a proper `React.Component` object.
|
3389 |
|
3390 | For example, this will NOT be checked:
|
3391 |
|
3392 | ```js
|
3393 | import MyReact from 'react';
|
3394 | class Foo extends MyReact.Component { }
|
3395 | ```
|
3396 |
|
3397 | As a result, you can safely use other classes without getting warnings from this rule:
|
3398 |
|
3399 | ```js
|
3400 | class MyClass extends MySuperClass { }
|
3401 | ```
|
3402 |
|
3403 | React's functional components are hard to detect statically. The way it's done here is by searching for JSX within a function. When present, a function is considered a React component:
|
3404 |
|
3405 | ```js
|
3406 | // this gets checked
|
3407 | type Props = { };
|
3408 | function MyComponent(props: Props) {
|
3409 | return <p />;
|
3410 | }
|
3411 |
|
3412 | // this doesn't get checked since no JSX is present in a function
|
3413 | type Options = { };
|
3414 | function SomeHelper(options: Options) {
|
3415 | // ...
|
3416 | }
|
3417 |
|
3418 | // this doesn't get checked since no JSX is present directly in a function
|
3419 | function helper() { return <p /> }
|
3420 | function MyComponent(props: Props) {
|
3421 | return helper();
|
3422 | }
|
3423 | ```
|
3424 |
|
3425 | The rule only works for locally defined props that are marked with a `$ReadOnly` or using covariant notation. It doesn't work with imported props:
|
3426 |
|
3427 | ```js
|
3428 | // the rule has no way of knowing whether ImportedProps are read-only
|
3429 | import { type ImportedProps } from './somewhere';
|
3430 | class Foo extends React.Component<ImportedProps> { }
|
3431 |
|
3432 |
|
3433 | // the rule also checks for covariant properties
|
3434 | type Props = {|
|
3435 | +foo: string
|
3436 | |};
|
3437 | class Bar extends React.Component<Props> { }
|
3438 |
|
3439 | // this fails because the object is not fully read-only
|
3440 | type Props = {|
|
3441 | +foo: string,
|
3442 | bar: number,
|
3443 | |};
|
3444 | class Bar extends React.Component<Props> { }
|
3445 |
|
3446 | // this fails because spreading makes object mutable (as of Flow 0.98)
|
3447 | // https://github.com/gajus/eslint-plugin-flowtype/pull/400#issuecomment-489813899
|
3448 | type Props = {|
|
3449 | +foo: string,
|
3450 | ...bar,
|
3451 | |};
|
3452 | class Bar extends React.Component<Props> { }
|
3453 | ```
|
3454 |
|
3455 |
|
3456 | ```js
|
3457 | {
|
3458 | "rules": {
|
3459 | "flowtype/require-readonly-react-props": 2
|
3460 | }
|
3461 | }
|
3462 | ```
|
3463 |
|
3464 |
|
3465 | Optionally, you can enable support for [implicit exact Flow types](https://medium.com/flow-type/on-the-roadmap-exact-objects-by-default-16b72933c5cf) (useful when using `exact_by_default=true` Flow option):
|
3466 |
|
3467 |
|
3468 | ```js
|
3469 | {
|
3470 | "rules": {
|
3471 | "flowtype/require-readonly-react-props": [
|
3472 | 2,
|
3473 | {
|
3474 | "useImplicitExactTypes": true
|
3475 | }
|
3476 | ]
|
3477 | }
|
3478 | }
|
3479 | ```
|
3480 |
|
3481 |
|
3482 | The following patterns are considered problems:
|
3483 |
|
3484 | ```js
|
3485 | type Props = { }; class Foo extends React.Component<Props> { }
|
3486 | // Message: Props must be $ReadOnly
|
3487 |
|
3488 | type OtherProps = { foo: string }; class Foo extends React.Component<OtherProps> { }
|
3489 | // Message: OtherProps must be $ReadOnly
|
3490 |
|
3491 | class Foo extends React.Component<{}> { }
|
3492 | // Message: Foo class props must be $ReadOnly
|
3493 |
|
3494 | type Props = { bar: {} }; class Foo extends React.Component<Props, State> { }
|
3495 | // Message: Props must be $ReadOnly
|
3496 |
|
3497 | type Props = { }; class Foo extends Component<Props> { }
|
3498 | // Message: Props must be $ReadOnly
|
3499 |
|
3500 | type Props = { }; class Foo extends PureComponent<Props> { }
|
3501 | // Message: Props must be $ReadOnly
|
3502 |
|
3503 | export type Props = {}; class Foo extends Component<Props> { }
|
3504 | // Message: Props must be $ReadOnly
|
3505 |
|
3506 | type Props = {| foo: string |}; class Foo extends Component<Props> { }
|
3507 | // Message: Props must be $ReadOnly
|
3508 |
|
3509 | type Props = {| +foo: string, ...bar |}; class Foo extends Component<Props> { }
|
3510 | // Message: Props must be $ReadOnly
|
3511 |
|
3512 | type Props = {| +foo: string, -bar: number |}; class Foo extends Component<Props> { }
|
3513 | // Message: Props must be $ReadOnly
|
3514 |
|
3515 | type Props = { }; function Foo(props: Props) { return <p /> }
|
3516 | // Message: Props must be $ReadOnly
|
3517 |
|
3518 | type Props = { }; function Foo(props: Props) { return foo ? <p /> : <span /> }
|
3519 | // Message: Props must be $ReadOnly
|
3520 |
|
3521 | function Foo(props: {}) { return <p /> }
|
3522 | // Message: Foo component props must be $ReadOnly
|
3523 |
|
3524 | export type Props = {}; function Foo(props: Props) { return <p /> }
|
3525 | // Message: Props must be $ReadOnly
|
3526 | ```
|
3527 |
|
3528 | The following patterns are not considered problems:
|
3529 |
|
3530 | ```js
|
3531 | class Foo extends React.Component<$ReadOnly<{}>> { }
|
3532 |
|
3533 | type Props = $ReadOnly<{}>; class Foo extends React.Component<Props> { }
|
3534 |
|
3535 | type Props = $ReadOnly<{}>; class Foo extends React.PureComponent<Props> { }
|
3536 |
|
3537 | class Foo extends React.Component<$ReadOnly<{}, State>> { }
|
3538 |
|
3539 | type Props = $ReadOnly<{}>; class Foo extends React.Component<Props, State> { }
|
3540 |
|
3541 | type Props = $ReadOnly<{}>; class Foo extends Component<Props> { }
|
3542 |
|
3543 | type Props = $ReadOnly<{}>; class Foo extends PureComponent<Props> { }
|
3544 |
|
3545 | type FooType = {}; class Foo extends Bar<FooType> { }
|
3546 |
|
3547 | class Foo { }
|
3548 |
|
3549 | export type Props = $ReadOnly<{}>; class Foo extends Component<Props, State> { }
|
3550 |
|
3551 | export type Props = $ReadOnly<{}>; export class Foo extends Component<Props> { }
|
3552 |
|
3553 | type Props = {| +foo: string |}; class Foo extends Component<Props> { }
|
3554 |
|
3555 | type Props = {| +foo: string, +bar: number |}; class Foo extends Component<Props> { }
|
3556 |
|
3557 | type Props = $FlowFixMe; class Foo extends Component<Props> { }
|
3558 |
|
3559 | type Props = {||}; class Foo extends Component<Props> { }
|
3560 |
|
3561 | // Options: [{"useImplicitExactTypes":true}]
|
3562 | type Props = {||}; class Foo extends Component<Props> { }
|
3563 |
|
3564 | // Options: [{"useImplicitExactTypes":true}]
|
3565 | type Props = {}; class Foo extends Component<Props> { }
|
3566 |
|
3567 | class Foo extends Component<{||}> { }
|
3568 |
|
3569 | // Options: [{"useImplicitExactTypes":true}]
|
3570 | class Foo extends Component<{||}> { }
|
3571 |
|
3572 | // Options: [{"useImplicitExactTypes":true}]
|
3573 | class Foo extends Component<{}> { }
|
3574 |
|
3575 | class Foo extends React.Component<UnknownProps> { }
|
3576 |
|
3577 | import { type Props } from "file"; class Foo extends React.Component<Props> { }
|
3578 |
|
3579 | type Props = {}; function Foo() { }
|
3580 |
|
3581 | type Props = $ReadOnly<{}>; function Foo(props: Props) { }
|
3582 |
|
3583 | type Props = {}; function Foo(props: OtherProps) { }
|
3584 |
|
3585 | function Foo() { return <p /> }
|
3586 |
|
3587 | function Foo(props: $FlowFixMe) { return <p /> }
|
3588 |
|
3589 | function Foo(props: {||}) { return <p /> }
|
3590 |
|
3591 | // Options: [{"useImplicitExactTypes":true}]
|
3592 | function Foo(props: {||}) { return <p /> }
|
3593 |
|
3594 | // Options: [{"useImplicitExactTypes":true}]
|
3595 | function Foo(props: {}) { return <p /> }
|
3596 | ```
|
3597 |
|
3598 |
|
3599 |
|
3600 | <a name="eslint-plugin-flowtype-rules-require-return-type"></a>
|
3601 | ### <code>require-return-type</code>
|
3602 |
|
3603 | Requires that functions have return type annotation.
|
3604 |
|
3605 | <a name="eslint-plugin-flowtype-rules-require-return-type-options-8"></a>
|
3606 | #### Options
|
3607 |
|
3608 | You can skip all arrow functions by providing the `excludeArrowFunctions` option with `true`.
|
3609 |
|
3610 | Alternatively, you can exclude a concise arrow function (e.g. `() => 2`). Provide `excludeArrowFunctions` with `expressionsOnly` for this.
|
3611 |
|
3612 | ```js
|
3613 | {
|
3614 | "rules": {
|
3615 | "flowtype/require-return-type": [
|
3616 | 2,
|
3617 | "always",
|
3618 | {
|
3619 | "excludeArrowFunctions": true
|
3620 | }
|
3621 | ]
|
3622 | }
|
3623 | }
|
3624 |
|
3625 | {
|
3626 | "rules": {
|
3627 | "flowtype/require-return-type": [
|
3628 | 2,
|
3629 | "always",
|
3630 | {
|
3631 | "excludeArrowFunctions": "expressionsOnly"
|
3632 | }
|
3633 | ]
|
3634 | }
|
3635 | }
|
3636 | ```
|
3637 |
|
3638 | You can exclude or include specific tests with the `includeOnlyMatching` and `excludeMatching` rules.
|
3639 |
|
3640 | ```js
|
3641 | {
|
3642 | "rules": {
|
3643 | "flowtype/require-return-type": [
|
3644 | 2,
|
3645 | "always",
|
3646 | {
|
3647 | "includeOnlyMatching": [
|
3648 | "^F.*",
|
3649 | "Ba(r|z)"
|
3650 | ]
|
3651 | }
|
3652 | ]
|
3653 | }
|
3654 | }
|
3655 |
|
3656 | {
|
3657 | "rules": {
|
3658 | "flowtype/require-return-type": [
|
3659 | 2,
|
3660 | "always",
|
3661 | {
|
3662 | "excludeMatching": [
|
3663 | "^F.*",
|
3664 | "Ba(r|z)"
|
3665 | ]
|
3666 | }
|
3667 | ]
|
3668 | }
|
3669 | }
|
3670 |
|
3671 | ```
|
3672 |
|
3673 | Both rules take an array that can contain either strings or valid RegExp statements.
|
3674 |
|
3675 | The following patterns are considered problems:
|
3676 |
|
3677 | ```js
|
3678 | (foo) => { return "foo"; }
|
3679 | // Message: Missing return type annotation.
|
3680 |
|
3681 | // Options: ["always"]
|
3682 | (foo) => { return "foo"; }
|
3683 | // Message: Missing return type annotation.
|
3684 |
|
3685 | // Options: ["always"]
|
3686 | (foo) => "foo"
|
3687 | // Message: Missing return type annotation.
|
3688 |
|
3689 | (foo) => ({})
|
3690 | // Message: Missing return type annotation.
|
3691 |
|
3692 | /* @flow */
|
3693 | (foo) => { return 1; }
|
3694 | // Message: Missing return type annotation.
|
3695 |
|
3696 | (foo): undefined => { return; }
|
3697 | // Message: Must not annotate undefined return type.
|
3698 |
|
3699 | (foo): void => { return; }
|
3700 | // Message: Must not annotate undefined return type.
|
3701 |
|
3702 | (foo): undefined => { return undefined; }
|
3703 | // Message: Must not annotate undefined return type.
|
3704 |
|
3705 | (foo): void => { return void 0; }
|
3706 | // Message: Must not annotate undefined return type.
|
3707 |
|
3708 | // Options: ["always",{"annotateUndefined":"never"}]
|
3709 | (foo): undefined => { return; }
|
3710 | // Message: Must not annotate undefined return type.
|
3711 |
|
3712 | // Options: ["always",{"annotateUndefined":"never"}]
|
3713 | (foo): void => { return; }
|
3714 | // Message: Must not annotate undefined return type.
|
3715 |
|
3716 | // Options: ["always",{"annotateUndefined":"always"}]
|
3717 | (foo) => { return; }
|
3718 | // Message: Must annotate undefined return type.
|
3719 |
|
3720 | // Options: ["always",{"annotateUndefined":"never"}]
|
3721 | (foo): undefined => { return undefined; }
|
3722 | // Message: Must not annotate undefined return type.
|
3723 |
|
3724 | // Options: ["always",{"annotateUndefined":"always"}]
|
3725 | (foo) => { return undefined; }
|
3726 | // Message: Must annotate undefined return type.
|
3727 |
|
3728 | // Options: ["always",{"annotateUndefined":"always"}]
|
3729 | (foo) => { return void 0; }
|
3730 | // Message: Must annotate undefined return type.
|
3731 |
|
3732 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
3733 | // @flow
|
3734 | (foo) => { return 1; }
|
3735 | // Message: Missing return type annotation.
|
3736 |
|
3737 | // Options: ["always",{"annotateUndefined":"always"}]
|
3738 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
3739 | // @flow
|
3740 | (foo) => { return undefined; }
|
3741 | // Message: Must annotate undefined return type.
|
3742 |
|
3743 | // Options: ["always"]
|
3744 | async () => { return 2; }
|
3745 | // Message: Missing return type annotation.
|
3746 |
|
3747 | // Options: ["always",{"annotateUndefined":"always"}]
|
3748 | async () => {}
|
3749 | // Message: Must annotate undefined return type.
|
3750 |
|
3751 | // Options: ["always",{"annotateUndefined":"always"}]
|
3752 | async function x() {}
|
3753 | // Message: Must annotate undefined return type.
|
3754 |
|
3755 | // Options: ["always",{"annotateUndefined":"never"}]
|
3756 | async (): Promise<void> => { return; }
|
3757 | // Message: Must not annotate undefined return type.
|
3758 |
|
3759 | // Options: ["always",{"annotateUndefined":"never"}]
|
3760 | async (): Promise<undefined> => { return; }
|
3761 | // Message: Must not annotate undefined return type.
|
3762 |
|
3763 | // Options: ["always",{"annotateUndefined":"always"}]
|
3764 | class Test { constructor() { } }
|
3765 | // Message: Must annotate undefined return type.
|
3766 |
|
3767 | class Test { foo() { return 42; } }
|
3768 | // Message: Missing return type annotation.
|
3769 |
|
3770 | class Test { foo = () => { return 42; } }
|
3771 | // Message: Missing return type annotation.
|
3772 |
|
3773 | class Test { foo = () => 42; }
|
3774 | // Message: Missing return type annotation.
|
3775 |
|
3776 | // Options: ["always"]
|
3777 | function* x() {}
|
3778 | // Message: Missing return type annotation.
|
3779 |
|
3780 | // Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
|
3781 | () => { return 3; }
|
3782 | // Message: Missing return type annotation.
|
3783 |
|
3784 | // Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
|
3785 | async () => { return 4; }
|
3786 | // Message: Missing return type annotation.
|
3787 |
|
3788 | // Options: ["always",{"includeOnlyMatching":["bar"]}]
|
3789 | function foo() { return 42; }
|
3790 | function bar() { return 42; }
|
3791 | // Message: Missing return type annotation.
|
3792 |
|
3793 | // Options: ["always",{"includeOnlyMatching":["bar"]}]
|
3794 | const foo = () => { return 42; };
|
3795 | const bar = () => { return 42; }
|
3796 | // Message: Missing return type annotation.
|
3797 |
|
3798 | // Options: ["always",{"includeOnlyMatching":["bar"]}]
|
3799 | const foo = { bar() { return 42; }, foobar: function() { return 42; } }
|
3800 | // Message: Missing return type annotation.
|
3801 | // Message: Missing return type annotation.
|
3802 |
|
3803 | // Options: ["always",{"excludeMatching":["bar"]}]
|
3804 | const foo = { bar() { return 42; }, baz() { return 42; } }
|
3805 | // Message: Missing return type annotation.
|
3806 |
|
3807 | // Options: ["always",{"annotateUndefined":"always"}]
|
3808 | function * foo() { yield 2; }
|
3809 | // Message: Missing return type annotation.
|
3810 |
|
3811 | // Options: ["always",{"annotateUndefined":"always"}]
|
3812 | async function * foo() { yield 2; }
|
3813 | // Message: Missing return type annotation.
|
3814 | ```
|
3815 |
|
3816 | The following patterns are not considered problems:
|
3817 |
|
3818 | ```js
|
3819 | return;
|
3820 |
|
3821 | (foo): string => {}
|
3822 |
|
3823 | const f: Foo = (a, b) => 42;
|
3824 |
|
3825 | // Options: ["always"]
|
3826 | (foo): string => {}
|
3827 |
|
3828 | type fn = (a: string, b: number) => number;
|
3829 | const f: fn = (a, b) => { return 42; }
|
3830 |
|
3831 | (foo) => { return; }
|
3832 |
|
3833 | (foo): Object => ( {} )
|
3834 |
|
3835 | (foo) => { return undefined; }
|
3836 |
|
3837 | (foo) => { return void 0; }
|
3838 |
|
3839 | // Options: ["always",{"annotateUndefined":"always"}]
|
3840 | (foo): undefined => { return; }
|
3841 |
|
3842 | // Options: ["always",{"annotateUndefined":"always"}]
|
3843 | (foo): void => { return; }
|
3844 |
|
3845 | // Options: ["always",{"annotateUndefined":"never"}]
|
3846 | (foo) => { return; }
|
3847 |
|
3848 | // Options: ["always",{"annotateUndefined":"never"}]
|
3849 | (foo) => { return undefined; }
|
3850 |
|
3851 | // Options: ["always",{"annotateUndefined":"never"}]
|
3852 | (foo) => { return void 0; }
|
3853 |
|
3854 | // Options: ["always",{"annotateUndefined":"always"}]
|
3855 | (foo): undefined => { return undefined; }
|
3856 |
|
3857 | // Options: ["always",{"annotateUndefined":"always"}]
|
3858 | (foo): void => { return void 0; }
|
3859 |
|
3860 | // Options: ["always"]
|
3861 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
3862 | (foo) => { return 1; }
|
3863 |
|
3864 | // Options: ["always"]
|
3865 | /* @noflow */
|
3866 | (foo) => { return 1; }
|
3867 |
|
3868 | // Options: ["always",{"annotateUndefined":"always"}]
|
3869 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
3870 | (foo) => { return undefined; }
|
3871 |
|
3872 | // Options: ["always",{"annotateUndefined":"always"}]
|
3873 | async function doThing(): Promise<void> {}
|
3874 |
|
3875 | // Options: ["always",{"annotateUndefined":"ignore"}]
|
3876 | async function doThing(): Promise<void> {}
|
3877 |
|
3878 | // Options: ["always",{"annotateUndefined":"ignore"}]
|
3879 | async function doThing() {}
|
3880 |
|
3881 | // Options: ["always",{"annotateUndefined":"always"}]
|
3882 | function* doThing(): Generator<number, void, void> { yield 2; }
|
3883 |
|
3884 | // Options: ["always",{"annotateUndefined":"always","excludeMatching":["constructor"]}]
|
3885 | class Test { constructor() { } }
|
3886 |
|
3887 | class Test { constructor() { } }
|
3888 |
|
3889 | // Options: ["always",{"excludeMatching":["foo"]}]
|
3890 | class Test { foo() { return 42; } }
|
3891 |
|
3892 | // Options: ["always",{"excludeMatching":["foo"]}]
|
3893 | class Test { foo = () => { return 42; } }
|
3894 |
|
3895 | // Options: ["always",{"excludeMatching":["foo"]}]
|
3896 | class Test { foo = () => 42; }
|
3897 |
|
3898 | class Test { foo = (): number => { return 42; } }
|
3899 |
|
3900 | class Test { foo = (): number => 42; }
|
3901 |
|
3902 | async (foo): Promise<number> => { return 3; }
|
3903 |
|
3904 | // Options: ["always",{"excludeArrowFunctions":true}]
|
3905 | () => 3
|
3906 |
|
3907 | // Options: ["always",{"excludeArrowFunctions":true}]
|
3908 | () => { return 4; }
|
3909 |
|
3910 | // Options: ["always",{"excludeArrowFunctions":true}]
|
3911 | () => undefined
|
3912 |
|
3913 | // Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
|
3914 | () => undefined
|
3915 |
|
3916 | // Options: ["always",{"annotateUndefined":"always","excludeArrowFunctions":true}]
|
3917 | () => { return undefined; }
|
3918 |
|
3919 | // Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
|
3920 | () => 3
|
3921 |
|
3922 | // Options: ["always",{"excludeArrowFunctions":"expressionsOnly"}]
|
3923 | async () => 3
|
3924 |
|
3925 | // Options: ["always",{"excludeMatching":["foo"]}]
|
3926 | function foo() { return 42; }
|
3927 |
|
3928 | // Options: ["always",{"includeOnlyMatching":["bar"]}]
|
3929 | function foo() { return 42; }
|
3930 |
|
3931 | // Options: ["always",{"excludeMatching":["bar"]}]
|
3932 | function foo(): number { return 42; }
|
3933 | function bar() { return 42; }
|
3934 |
|
3935 | // Options: ["always",{"includeOnlyMatching":["foo","baz"]}]
|
3936 | function foo(): number { return 42; }
|
3937 | function bar() { return 42; }
|
3938 |
|
3939 | // Options: ["always",{"excludeMatching":["^b.*","qux"]}]
|
3940 | function foo(): number { return 42; }
|
3941 | function bar() { return 42; }
|
3942 |
|
3943 | // Options: ["always",{"includeOnlyMatching":["^f.*"]}]
|
3944 | function foo(): number { return 42; }
|
3945 | function bar() { return 42; }
|
3946 |
|
3947 | // Options: ["always",{"includeOnlyMatching":["bar"]}]
|
3948 | const foo = { baz() { return 42; } }
|
3949 |
|
3950 | // Options: ["always",{"excludeMatching":["bar"]}]
|
3951 | const foo = { bar() { return 42; } }
|
3952 |
|
3953 | // Options: ["always",{"annotateUndefined":"always"}]
|
3954 | function * foo(): Iterable<number> { yield 2; }
|
3955 |
|
3956 | // Options: ["always",{"annotateUndefined":"always"}]
|
3957 | async function * foo(): AsyncIterable<number> { yield 2; }
|
3958 | ```
|
3959 |
|
3960 |
|
3961 |
|
3962 | <a name="eslint-plugin-flowtype-rules-require-types-at-top"></a>
|
3963 | ### <code>require-types-at-top</code>
|
3964 |
|
3965 | Requires all type declarations to be at the top of the file, after any import declarations.
|
3966 |
|
3967 | <a name="eslint-plugin-flowtype-rules-require-types-at-top-options-9"></a>
|
3968 | #### Options
|
3969 |
|
3970 | The rule has a string option:
|
3971 |
|
3972 | * `"never"`
|
3973 | * `"always"`
|
3974 |
|
3975 | The default value is `"always"`.
|
3976 |
|
3977 | The following patterns are considered problems:
|
3978 |
|
3979 | ```js
|
3980 | const foo = 3;
|
3981 | type Foo = number;
|
3982 | // Message: All type declaration should be at the top of the file, after any import declarations.
|
3983 |
|
3984 | const foo = 3;
|
3985 | opaque type Foo = number;
|
3986 | // Message: All type declaration should be at the top of the file, after any import declarations.
|
3987 |
|
3988 | const foo = 3;
|
3989 | export type Foo = number;
|
3990 | // Message: All type declaration should be at the top of the file, after any import declarations.
|
3991 |
|
3992 | const foo = 3;
|
3993 | export opaque type Foo = number;
|
3994 | // Message: All type declaration should be at the top of the file, after any import declarations.
|
3995 |
|
3996 | const foo = 3;
|
3997 | type Foo = number | string;
|
3998 | // Message: All type declaration should be at the top of the file, after any import declarations.
|
3999 |
|
4000 | import bar from "./bar";
|
4001 | const foo = 3;
|
4002 | type Foo = number;
|
4003 | // Message: All type declaration should be at the top of the file, after any import declarations.
|
4004 | ```
|
4005 |
|
4006 | The following patterns are not considered problems:
|
4007 |
|
4008 | ```js
|
4009 | type Foo = number;
|
4010 | const foo = 3;
|
4011 |
|
4012 | opaque type Foo = number;
|
4013 | const foo = 3;
|
4014 |
|
4015 | export type Foo = number;
|
4016 | const foo = 3;
|
4017 |
|
4018 | export opaque type Foo = number;
|
4019 | const foo = 3;
|
4020 |
|
4021 | type Foo = number;
|
4022 | const foo = 3;
|
4023 |
|
4024 | import bar from "./bar";
|
4025 | type Foo = number;
|
4026 |
|
4027 | type Foo = number;
|
4028 | import bar from "./bar";
|
4029 |
|
4030 | // Options: ["never"]
|
4031 | const foo = 3;
|
4032 | type Foo = number;
|
4033 | ```
|
4034 |
|
4035 |
|
4036 |
|
4037 | <a name="eslint-plugin-flowtype-rules-require-valid-file-annotation"></a>
|
4038 | ### <code>require-valid-file-annotation</code>
|
4039 |
|
4040 | This rule validates Flow file annotations.
|
4041 |
|
4042 | This rule can optionally report missing or missed placed annotations, common typos (e.g. `// @floww`), and enforce a consistent annotation style.
|
4043 |
|
4044 | <a name="eslint-plugin-flowtype-rules-require-valid-file-annotation-options-10"></a>
|
4045 | #### Options
|
4046 |
|
4047 | The rule has a string option:
|
4048 |
|
4049 | * `"never"` (default): Never report files that are missing an `@flow` annotation.
|
4050 | * `"always"`: Always report files that are missing an `@flow` annotation
|
4051 |
|
4052 | This rule has an object option:
|
4053 |
|
4054 | * `"annotationStyle"` - Enforce a consistent file annotation style.
|
4055 | * `"none"` (default): Either annotation style is accepted.
|
4056 | * `"line"`: Require single line annotations (i.e. `// @flow`).
|
4057 | * `"block"`: Require block annotations (i.e. `/* @flow */`).
|
4058 |
|
4059 | * `"strict"` - Enforce a strict flow file annotation.
|
4060 | * `false` (default): strict flow annotation is not required.
|
4061 | * `true`: Require strict flow annotation (i.e. `// @flow strict`).
|
4062 |
|
4063 | ```js
|
4064 | {
|
4065 | "rules": {
|
4066 | "flowtype/require-valid-file-annotation": [
|
4067 | 2,
|
4068 | "always"
|
4069 | ]
|
4070 | }
|
4071 | }
|
4072 |
|
4073 | {
|
4074 | "rules": {
|
4075 | "flowtype/require-valid-file-annotation": [
|
4076 | 2,
|
4077 | "always", {
|
4078 | "annotationStyle": "block",
|
4079 | "strict": true,
|
4080 | }
|
4081 | ]
|
4082 | }
|
4083 | }
|
4084 | ```
|
4085 |
|
4086 | The following patterns are considered problems:
|
4087 |
|
4088 | ```js
|
4089 | // Options: ["always"]
|
4090 | #!/usr/bin/env node
|
4091 | // Message: Flow file annotation is missing.
|
4092 |
|
4093 | // Options: ["always"]
|
4094 | #!/usr/bin/env node
|
4095 | a;
|
4096 | // Message: Flow file annotation is missing.
|
4097 |
|
4098 | ;// @flow
|
4099 | // Message: Flow file annotation not at the top of the file.
|
4100 |
|
4101 | ;
|
4102 | // @flow
|
4103 | // Message: Flow file annotation not at the top of the file.
|
4104 |
|
4105 | // @Flow
|
4106 | // Message: Malformed Flow file annotation.
|
4107 |
|
4108 | // @NoFlow
|
4109 | // Message: Malformed Flow file annotation.
|
4110 |
|
4111 | // @Noflow
|
4112 | // Message: Malformed Flow file annotation.
|
4113 |
|
4114 | // @floweeeeeee
|
4115 | // Message: Misspelled or malformed Flow file annotation.
|
4116 |
|
4117 | // @nofloweeeeeee
|
4118 | // Message: Misspelled or malformed Flow file annotation.
|
4119 |
|
4120 | // Options: ["always"]
|
4121 | a;
|
4122 | // Message: Flow file annotation is missing.
|
4123 |
|
4124 | // Options: ["always",{"annotationStyle":"line"}]
|
4125 | /* @flow */
|
4126 | // Message: Flow file annotation style must be `// @flow`
|
4127 |
|
4128 | // Options: ["always",{"annotationStyle":"block"}]
|
4129 | // @flow
|
4130 | // Message: Flow file annotation style must be `/* @flow */`
|
4131 |
|
4132 | // Options: ["always",{"annotationStyle":"block"}]
|
4133 | // @flow
|
4134 | // Message: Flow file annotation style must be `/* @flow */`
|
4135 |
|
4136 | // Options: ["always",{"annotationStyle":"line","strict":true}]
|
4137 | // @flow
|
4138 | // Message: Strict Flow file annotation is required, should be `// @flow strict`
|
4139 |
|
4140 | // Options: ["always",{"annotationStyle":"line"}]
|
4141 | /* @noflow */
|
4142 | // Message: Flow file annotation style must be `// @noflow`
|
4143 |
|
4144 | // Options: ["always",{"annotationStyle":"block"}]
|
4145 | // @noflow
|
4146 | // Message: Flow file annotation style must be `/* @noflow */`
|
4147 |
|
4148 | // Options: ["always"]
|
4149 | a;
|
4150 | // Message: Flow file annotation is missing.
|
4151 |
|
4152 | // Options: ["always",{"annotationStyle":"block"}]
|
4153 | a;
|
4154 | // Message: Flow file annotation is missing.
|
4155 |
|
4156 | // Options: ["always",{"annotationStyle":"line","strict":true}]
|
4157 | a;
|
4158 | // Message: Flow file annotation is missing.
|
4159 |
|
4160 | // Options: ["always",{"annotationStyle":"line","strict":true}]
|
4161 | // @flow
|
4162 | a;
|
4163 | b;
|
4164 | // Message: Strict Flow file annotation is required, should be `// @flow strict`
|
4165 |
|
4166 | // Options: ["never",{"annotationStyle":"line"}]
|
4167 | /* @flow */
|
4168 | a;
|
4169 | b;
|
4170 | // Message: Flow file annotation style must be `// @flow`
|
4171 |
|
4172 | // Options: ["never",{"annotationStyle":"line"}]
|
4173 | /* @flow strict */
|
4174 | a;
|
4175 | b;
|
4176 | // Message: Flow file annotation style must be `// @flow strict`
|
4177 | ```
|
4178 |
|
4179 | The following patterns are not considered problems:
|
4180 |
|
4181 | ```js
|
4182 | a;
|
4183 |
|
4184 | // @flow
|
4185 | a;
|
4186 |
|
4187 | //@flow
|
4188 | a;
|
4189 |
|
4190 | //**@flow
|
4191 | a;
|
4192 |
|
4193 | /* foo @flow bar */
|
4194 | a;
|
4195 |
|
4196 |
|
4197 |
|
4198 | // @flow
|
4199 | a;
|
4200 |
|
4201 | // @flow
|
4202 | // @FLow
|
4203 |
|
4204 | // @noflow
|
4205 | a;
|
4206 |
|
4207 | // Options: ["always"]
|
4208 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
4209 | a;
|
4210 |
|
4211 | // Options: ["always",{"annotationStyle":"line"}]
|
4212 | // @flow
|
4213 |
|
4214 | // Options: ["always",{"annotationStyle":"line","strict":true}]
|
4215 | // @noflow
|
4216 |
|
4217 | // Options: ["always",{"annotationStyle":"line","strict":true}]
|
4218 | // @flow strict
|
4219 |
|
4220 | // Options: ["never",{"annotationStyle":"none"}]
|
4221 | // @function
|
4222 |
|
4223 | // Options: ["never"]
|
4224 | // @fixable
|
4225 |
|
4226 | // Options: ["always",{"annotationStyle":"block"}]
|
4227 | /* @flow */
|
4228 | ```
|
4229 |
|
4230 |
|
4231 |
|
4232 | <a name="eslint-plugin-flowtype-rules-require-variable-type"></a>
|
4233 | ### <code>require-variable-type</code>
|
4234 |
|
4235 | Requires that all variable declarators have type annotations.
|
4236 |
|
4237 | <a name="eslint-plugin-flowtype-rules-require-variable-type-options-11"></a>
|
4238 | #### Options
|
4239 |
|
4240 | You can exclude variables that match a certain regex by using `excludeVariableMatch`.
|
4241 |
|
4242 | This excludes all parameters that start with an underscore (`_`).
|
4243 | The default pattern is `a^`, which doesn't match anything, i.e., all parameters are checked.
|
4244 |
|
4245 | ```js
|
4246 | {
|
4247 | "rules": {
|
4248 | "flowtype/require-variable-type": [
|
4249 | 2,
|
4250 | {
|
4251 | "excludeVariableMatch": "^_"
|
4252 | }
|
4253 | ]
|
4254 | }
|
4255 | }
|
4256 | ```
|
4257 |
|
4258 |
|
4259 | You can choose specific variable types (`var`, `let`, and `const`) to ignore using `excludeVariableTypes`.
|
4260 |
|
4261 | This excludes `var` and `let` declarations from needing type annotations, but forces `const` declarations to have it.
|
4262 | By default, all declarations are checked.
|
4263 |
|
4264 | ```js
|
4265 | {
|
4266 | "rules": {
|
4267 | "flowtype/require-variable-type": [
|
4268 | 2,
|
4269 | {
|
4270 | "excludeVariableTypes": {
|
4271 | "var": true,
|
4272 | "let": true,
|
4273 | "const": false,
|
4274 | }
|
4275 | }
|
4276 | ]
|
4277 | }
|
4278 | }
|
4279 | ```
|
4280 |
|
4281 |
|
4282 |
|
4283 | The following patterns are considered problems:
|
4284 |
|
4285 | ```js
|
4286 | var foo = "bar"
|
4287 | // Message: Missing "foo" variable type annotation.
|
4288 |
|
4289 | var foo : string = "bar", bar = 1
|
4290 | // Message: Missing "bar" variable type annotation.
|
4291 |
|
4292 | // Options: [{"excludeVariableMatch":"^_"}]
|
4293 | var _foo = "bar", bar = 1
|
4294 | // Message: Missing "bar" variable type annotation.
|
4295 |
|
4296 | // Options: [{"excludeVariableTypes":{"let":false,"var":true}}]
|
4297 | var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"
|
4298 | // Message: Missing "hey" variable type annotation.
|
4299 | ```
|
4300 |
|
4301 | The following patterns are not considered problems:
|
4302 |
|
4303 | ```js
|
4304 | var foo : string = "bar"
|
4305 |
|
4306 | var foo : string = "bar", bar : number = 1
|
4307 |
|
4308 | // Options: [{"excludeVariableMatch":"^_"}]
|
4309 | var _foo = "bar", bar : number = 1
|
4310 |
|
4311 | // Options: [{"excludeVariableTypes":{"var":true}}]
|
4312 | var foo = "bar", bar = 1
|
4313 |
|
4314 | // Options: [{"excludeVariableTypes":{"let":true,"var":true}}]
|
4315 | var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"
|
4316 | ```
|
4317 |
|
4318 |
|
4319 |
|
4320 | <a name="eslint-plugin-flowtype-rules-semi"></a>
|
4321 | ### <code>semi</code>
|
4322 |
|
4323 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
4324 |
|
4325 | Enforces consistent use of semicolons after type aliases.
|
4326 |
|
4327 | 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.
|
4328 |
|
4329 | The default value is `'always'`.
|
4330 |
|
4331 | The following patterns are considered problems:
|
4332 |
|
4333 | ```js
|
4334 | // Options: ["always"]
|
4335 | class Foo { foo: string }
|
4336 | // Message: Missing semicolon.
|
4337 |
|
4338 | // Options: ["never"]
|
4339 | class Foo { foo: string; }
|
4340 | // Message: Extra semicolon.
|
4341 |
|
4342 | // Options: []
|
4343 | type FooType = {}
|
4344 | // Message: Missing semicolon.
|
4345 |
|
4346 | // Options: ["always"]
|
4347 | type FooType = {}
|
4348 | // Message: Missing semicolon.
|
4349 |
|
4350 | // Options: ["never"]
|
4351 | type FooType = {};
|
4352 | // Message: Extra semicolon.
|
4353 |
|
4354 | // Options: []
|
4355 | opaque type FooType = {}
|
4356 | // Message: Missing semicolon.
|
4357 | ```
|
4358 |
|
4359 | The following patterns are not considered problems:
|
4360 |
|
4361 | ```js
|
4362 | type FooType = {};
|
4363 |
|
4364 | // Options: ["always"]
|
4365 | type FooType = {};
|
4366 |
|
4367 | // Options: ["always"]
|
4368 | (foo: string) => {}
|
4369 |
|
4370 | // Options: ["always"]
|
4371 | class Foo { foo: string; }
|
4372 |
|
4373 | // Options: ["never"]
|
4374 | class Foo { foo: string }
|
4375 |
|
4376 | // Options: ["always"]
|
4377 | type FooType = { a: number;
|
4378 | b: string;
|
4379 | };
|
4380 |
|
4381 | // Options: ["never"]
|
4382 | type FooType = { a: number;
|
4383 | b: string;
|
4384 | }
|
4385 |
|
4386 | // Options: ["never"]
|
4387 | type FooType = {}
|
4388 |
|
4389 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
4390 | type FooType = {}
|
4391 |
|
4392 | opaque type FooType = {};
|
4393 | ```
|
4394 |
|
4395 |
|
4396 |
|
4397 | <a name="eslint-plugin-flowtype-rules-sort-keys"></a>
|
4398 | ### <code>sort-keys</code>
|
4399 |
|
4400 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
4401 |
|
4402 | Enforces natural, case-insensitive sorting of Object annotations.
|
4403 |
|
4404 | <a name="eslint-plugin-flowtype-rules-sort-keys-options-12"></a>
|
4405 | #### Options
|
4406 |
|
4407 | The first option specifies sort order.
|
4408 |
|
4409 | * `"asc"` (default) - enforce ascending sort order.
|
4410 | * `"desc"` - enforce descending sort order.
|
4411 |
|
4412 | ```js
|
4413 | {
|
4414 | "rules": {
|
4415 | "flowtype/sort-keys": [
|
4416 | 2,
|
4417 | "asc"
|
4418 | ]
|
4419 | }
|
4420 | }
|
4421 | ```
|
4422 |
|
4423 | The following patterns are considered problems:
|
4424 |
|
4425 | ```js
|
4426 | type FooType = { a: number, c: number, b: string }
|
4427 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4428 |
|
4429 | // Options: ["desc"]
|
4430 | type FooType = { a: number, b: number }
|
4431 | // Message: Expected type annotations to be in descending order. "b" should be before "a".
|
4432 |
|
4433 | // Options: ["desc"]
|
4434 | type FooType = { b: number, C: number, a: string }
|
4435 | // Message: Expected type annotations to be in descending order. "C" should be before "b".
|
4436 |
|
4437 | // Options: ["asc"]
|
4438 | type FooType = { a: number, c: number, C: number, b: string }
|
4439 | // Message: Expected type annotations to be in ascending order. "b" should be before "C".
|
4440 |
|
4441 | // Options: ["asc"]
|
4442 | type FooType = { a: number, C: number, c: number, b: string }
|
4443 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4444 |
|
4445 | // Options: ["asc"]
|
4446 | type FooType = { 1: number, 10: number, 2: boolean }
|
4447 | // Message: Expected type annotations to be in ascending order. "2" should be before "10".
|
4448 |
|
4449 | type FooType = { a: number, c: number, b: string }
|
4450 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4451 |
|
4452 |
|
4453 | type FooType = {
|
4454 | a: number,
|
4455 | c: number,
|
4456 | b: string,
|
4457 | }
|
4458 |
|
4459 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4460 |
|
4461 |
|
4462 | type FooType = {
|
4463 | a: $ReadOnlyArray<number>,
|
4464 | c: $ReadOnlyMap<string, number>,
|
4465 | b: Map<string, Array<Map<string, number>>>,
|
4466 | }
|
4467 |
|
4468 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4469 |
|
4470 |
|
4471 | type FooType = {
|
4472 | ...ErrorsInRecursiveGenericTypeArgsButDoesNotFix<{
|
4473 | y: boolean,
|
4474 | x: string,
|
4475 | z: {
|
4476 | j: string,
|
4477 | l: number,
|
4478 | k: boolean,
|
4479 | },
|
4480 | }>,
|
4481 | a: number,
|
4482 | c: string,
|
4483 | b: Map<string, Array<ErrorsInRecursiveGenericTypeArgsButDoesNotFix<{
|
4484 | y: boolean,
|
4485 | x: string,
|
4486 | z: {
|
4487 | j: string,
|
4488 | l: number,
|
4489 | k: boolean,
|
4490 | },
|
4491 | }>>>,
|
4492 | }
|
4493 |
|
4494 | // Message: Expected type annotations to be in ascending order. "x" should be before "y".
|
4495 | // Message: Expected type annotations to be in ascending order. "k" should be before "l".
|
4496 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4497 | // Message: Expected type annotations to be in ascending order. "x" should be before "y".
|
4498 | // Message: Expected type annotations to be in ascending order. "k" should be before "l".
|
4499 |
|
4500 |
|
4501 | type FooType = {
|
4502 | ...BPreservesSpreadOrder,
|
4503 | ...APreservesSpreadOrder,
|
4504 | c: string,
|
4505 | b: number,
|
4506 | }
|
4507 |
|
4508 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4509 |
|
4510 |
|
4511 | type FooType = {
|
4512 | ...BPreservesSpreadSpans,
|
4513 | ...APreservesSpreadSpans,
|
4514 | c: string,
|
4515 | b: number,
|
4516 | ...CPreservesSpreadSpans,
|
4517 | e: string,
|
4518 | d: number,
|
4519 | }
|
4520 |
|
4521 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4522 | // Message: Expected type annotations to be in ascending order. "d" should be before "e".
|
4523 |
|
4524 |
|
4525 | type FooType = {
|
4526 | ...BPreservesSpreadOrderAndTypeArgs<string, number>,
|
4527 | ...APreservesSpreadOrderAndTypeArgs<number>,
|
4528 | c: string,
|
4529 | b: number,
|
4530 | }
|
4531 |
|
4532 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4533 |
|
4534 |
|
4535 | type FooType = {
|
4536 | /* preserves block comment before spread BType */
|
4537 | // preserves line comment before spread BType
|
4538 | ... /* preserves comment in spread BType */ BType<Generic> /* preserves trailing comment in spread AType */,
|
4539 | /* preserves block comment before spread AType */
|
4540 | // preserves line comment before spread AType
|
4541 | ... /* preserves comment in spread AType */ AType /* preserves trailing comment in spread AType */,
|
4542 | /* preserves block comment before reordered key "c" */
|
4543 | // preserves line comment before reordered key "c"
|
4544 | c:/* preserves comment and white space or lack of it */string/* preserves trailing comment for key "c" */,
|
4545 | b: number,
|
4546 | dWithoutComma: boolean
|
4547 | }
|
4548 |
|
4549 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4550 |
|
4551 |
|
4552 | type FooType = {
|
4553 | +a: number,
|
4554 | c: number,
|
4555 | b: string,
|
4556 | }
|
4557 |
|
4558 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4559 |
|
4560 |
|
4561 | type FooType = {
|
4562 | -a: number,
|
4563 | c: number,
|
4564 | b: string,
|
4565 | }
|
4566 |
|
4567 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4568 |
|
4569 |
|
4570 | type FooType = {
|
4571 | a?: number,
|
4572 | c: ?number,
|
4573 | b: string,
|
4574 | }
|
4575 |
|
4576 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4577 |
|
4578 |
|
4579 | type FooType = {
|
4580 | a: (number) => void,
|
4581 | c: number,
|
4582 | b: (param: string) => number,
|
4583 | }
|
4584 |
|
4585 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4586 |
|
4587 |
|
4588 | type FooType = {
|
4589 | a: number | string | boolean,
|
4590 | c: number,
|
4591 | b: (param: string) => number,
|
4592 | }
|
4593 |
|
4594 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4595 |
|
4596 |
|
4597 | type FooType = {
|
4598 | c: number,
|
4599 | a: number | string | boolean,
|
4600 | b: (param: string) => number,
|
4601 | }
|
4602 |
|
4603 | // Message: Expected type annotations to be in ascending order. "a" should be before "c".
|
4604 |
|
4605 |
|
4606 | type FooType = {
|
4607 | c: {
|
4608 | z: number,
|
4609 | x: string,
|
4610 | y: boolean,
|
4611 | },
|
4612 | a: number | string | boolean,
|
4613 | b: (param: string) => number,
|
4614 | }
|
4615 |
|
4616 | // Message: Expected type annotations to be in ascending order. "x" should be before "z".
|
4617 | // Message: Expected type annotations to be in ascending order. "a" should be before "c".
|
4618 |
|
4619 |
|
4620 | type FooType = {
|
4621 | c: {
|
4622 | z: {
|
4623 | j: string,
|
4624 | l: number,
|
4625 | k: boolean,
|
4626 | },
|
4627 | x: string,
|
4628 | y: boolean,
|
4629 | },
|
4630 | a: number | string | boolean,
|
4631 | b: (param: string) => number,
|
4632 | }
|
4633 |
|
4634 | // Message: Expected type annotations to be in ascending order. "k" should be before "l".
|
4635 | // Message: Expected type annotations to be in ascending order. "x" should be before "z".
|
4636 | // Message: Expected type annotations to be in ascending order. "a" should be before "c".
|
4637 |
|
4638 |
|
4639 | type FooType = {
|
4640 | +c: number,
|
4641 | -b: number,
|
4642 | a: number,
|
4643 | }
|
4644 |
|
4645 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4646 | // Message: Expected type annotations to be in ascending order. "a" should be before "b".
|
4647 |
|
4648 |
|
4649 | type FooType = {|
|
4650 | +c: number,
|
4651 | -b: number,
|
4652 | a: number,
|
4653 | |}
|
4654 |
|
4655 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4656 | // Message: Expected type annotations to be in ascending order. "a" should be before "b".
|
4657 |
|
4658 |
|
4659 | type FooType = {
|
4660 | a(number): void,
|
4661 | c: number,
|
4662 | b(param: string): number,
|
4663 | }
|
4664 |
|
4665 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4666 |
|
4667 |
|
4668 | type FooType = {
|
4669 | a: number | string | boolean,
|
4670 | c: number,
|
4671 | b(param: string): number,
|
4672 | }
|
4673 |
|
4674 | // Message: Expected type annotations to be in ascending order. "b" should be before "c".
|
4675 |
|
4676 |
|
4677 | type FooType = {
|
4678 | c: number,
|
4679 | a: number | string | boolean,
|
4680 | b(param: string): number,
|
4681 | }
|
4682 |
|
4683 | // Message: Expected type annotations to be in ascending order. "a" should be before "c".
|
4684 |
|
4685 |
|
4686 | type FooType = {
|
4687 | c: {
|
4688 | z: number,
|
4689 | x: string,
|
4690 | y: boolean,
|
4691 | },
|
4692 | a: number | string | boolean,
|
4693 | b(param: string): number,
|
4694 | }
|
4695 |
|
4696 | // Message: Expected type annotations to be in ascending order. "x" should be before "z".
|
4697 | // Message: Expected type annotations to be in ascending order. "a" should be before "c".
|
4698 |
|
4699 |
|
4700 | type FooType = {
|
4701 | c: {
|
4702 | z: {
|
4703 | j: string,
|
4704 | l: number,
|
4705 | k: boolean,
|
4706 | },
|
4707 | x: string,
|
4708 | y: boolean,
|
4709 | },
|
4710 | a: number | string | boolean,
|
4711 | b(param: string): number,
|
4712 | }
|
4713 |
|
4714 | // Message: Expected type annotations to be in ascending order. "k" should be before "l".
|
4715 | // Message: Expected type annotations to be in ascending order. "x" should be before "z".
|
4716 | // Message: Expected type annotations to be in ascending order. "a" should be before "c".
|
4717 | ```
|
4718 |
|
4719 | The following patterns are not considered problems:
|
4720 |
|
4721 | ```js
|
4722 | type FooType = { a: number }
|
4723 |
|
4724 | type FooType = { a: number, b: number, c: (boolean | number) }
|
4725 |
|
4726 | type FooType = { a: string, b: foo, C: number }
|
4727 |
|
4728 | type FooType = { 1: number, 2: boolean, 10: number }
|
4729 |
|
4730 | // Options: ["desc"]
|
4731 | type FooType = { c: number, b: number, a: number }
|
4732 |
|
4733 | // Options: ["desc"]
|
4734 | type FooType = { C: number, b: string, a: {} }
|
4735 |
|
4736 | // Options: ["desc"]
|
4737 | type FooType = { 10: number, 2: number, 1: boolean }
|
4738 |
|
4739 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
4740 | type FooType = { b: number, a: number }
|
4741 |
|
4742 | type FooType = { a: string, b(): number, c: boolean }
|
4743 |
|
4744 | type FooType = { a(): string, b: number, c: boolean }
|
4745 | ```
|
4746 |
|
4747 |
|
4748 |
|
4749 | <a name="eslint-plugin-flowtype-rules-space-after-type-colon"></a>
|
4750 | ### <code>space-after-type-colon</code>
|
4751 |
|
4752 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
4753 |
|
4754 | Enforces consistent spacing after the type annotation colon.
|
4755 |
|
4756 | <a name="eslint-plugin-flowtype-rules-space-after-type-colon-options-13"></a>
|
4757 | #### Options
|
4758 |
|
4759 | This rule has a string argument.
|
4760 |
|
4761 | * `"always"` (default): Require a space after the type annotation colon (e.g. foo: BarType).
|
4762 | * `"never"`: Require no spaces after the type annotation colon (e.g. foo:BarType).
|
4763 |
|
4764 | This rule has an option object.
|
4765 |
|
4766 | * `"allowLineBreak"` - Allow a line break to count as a space following the annotation colon.
|
4767 | * `"true"`: Enable
|
4768 | * `"false"`: Disable
|
4769 |
|
4770 | {
|
4771 | "rules": {
|
4772 | "flowtype/space-after-type-colon": [
|
4773 | 2,
|
4774 | "always", {
|
4775 | "allowLineBreak": false
|
4776 | }
|
4777 | ]
|
4778 | }
|
4779 | }
|
4780 |
|
4781 | The following patterns are considered problems:
|
4782 |
|
4783 | ```js
|
4784 | // Options: ["never"]
|
4785 | (foo: string) => {}
|
4786 | // Message: There must be no space after "foo" parameter type annotation colon.
|
4787 |
|
4788 | // Options: ["always"]
|
4789 | (foo: string) => {}
|
4790 | // Message: There must be 1 space after "foo" parameter type annotation colon.
|
4791 |
|
4792 | // Options: ["always"]
|
4793 | (foo:(() => void)) => {}
|
4794 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4795 |
|
4796 | // Options: ["never"]
|
4797 | (foo: (() => void)) => {}
|
4798 | // Message: There must be no space after "foo" parameter type annotation colon.
|
4799 |
|
4800 | // Options: ["always"]
|
4801 | (foo: (() => void)) => {}
|
4802 | // Message: There must be 1 space after "foo" parameter type annotation colon.
|
4803 |
|
4804 | ({ lorem, ipsum, dolor } : SomeType) => {}
|
4805 | // Message: There must be 1 space after "{ lorem, ipsum, dolor }" parameter type annotation colon.
|
4806 |
|
4807 | (foo:{ a: string, b: number }) => {}
|
4808 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4809 |
|
4810 | ({ a, b } :{ a: string, b: number }) => {}
|
4811 | // Message: There must be a space after "{ a, b }" parameter type annotation colon.
|
4812 |
|
4813 | ([ a, b ] :string[]) => {}
|
4814 | // Message: There must be a space after "[ a, b ]" parameter type annotation colon.
|
4815 |
|
4816 | (i?:number) => {}
|
4817 | // Message: There must be a space after "i" parameter type annotation colon.
|
4818 |
|
4819 | (i?: number) => {}
|
4820 | // Message: There must be 1 space after "i" parameter type annotation colon.
|
4821 |
|
4822 | // Options: ["never"]
|
4823 | (i?: number) => {}
|
4824 | // Message: There must be no space after "i" parameter type annotation colon.
|
4825 |
|
4826 | (foo:
|
4827 | { a: string, b: number }) => {}
|
4828 | // Message: There must not be a line break after "foo" parameter type annotation colon.
|
4829 |
|
4830 | (foo:
|
4831 | { a: string, b: number }) => {}
|
4832 | // Message: There must not be a line break after "foo" parameter type annotation colon.
|
4833 |
|
4834 | (foo:
|
4835 | { a: string, b: number }) => {}
|
4836 | // Message: There must not be a line break after "foo" parameter type annotation colon.
|
4837 |
|
4838 | // Options: ["always"]
|
4839 | ():Object => {}
|
4840 | // Message: There must be a space after return type colon.
|
4841 |
|
4842 | // Options: ["never"]
|
4843 | (): Object => {}
|
4844 | // Message: There must be no space after return type colon.
|
4845 |
|
4846 | // Options: ["always"]
|
4847 | (): Object => {}
|
4848 | // Message: There must be 1 space after return type colon.
|
4849 |
|
4850 | // Options: ["always"]
|
4851 | ():(() => void) => {}
|
4852 | // Message: There must be a space after return type colon.
|
4853 |
|
4854 | // Options: ["never"]
|
4855 | (): (() => void) => {}
|
4856 | // Message: There must be no space after return type colon.
|
4857 |
|
4858 | // Options: ["always"]
|
4859 | (): (() => void) => {}
|
4860 | // Message: There must be 1 space after return type colon.
|
4861 |
|
4862 | // Options: ["never"]
|
4863 | export default function (foo: string) {}
|
4864 | // Message: There must be no space after "foo" parameter type annotation colon.
|
4865 |
|
4866 | // Options: ["never"]
|
4867 | function foo (foo: string) {}
|
4868 | // Message: There must be no space after "foo" parameter type annotation colon.
|
4869 |
|
4870 | // Options: ["always"]
|
4871 | (foo:string) => {}
|
4872 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4873 |
|
4874 | function foo (foo:string) {}
|
4875 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4876 |
|
4877 | async function foo({ lorem, ipsum, dolor }:SomeType) {}
|
4878 | // Message: There must be a space after "{ lorem, ipsum, dolor }" parameter type annotation colon.
|
4879 |
|
4880 | function x(i?:number) {}
|
4881 | // Message: There must be a space after "i" parameter type annotation colon.
|
4882 |
|
4883 | function x(i?: number) {}
|
4884 | // Message: There must be 1 space after "i" parameter type annotation colon.
|
4885 |
|
4886 | // Options: ["never"]
|
4887 | function x(i?: number) {}
|
4888 | // Message: There must be no space after "i" parameter type annotation colon.
|
4889 |
|
4890 | function a():x {}
|
4891 | // Message: There must be a space after return type colon.
|
4892 |
|
4893 | // Options: ["always"]
|
4894 | function a(): x {}
|
4895 | // Message: There must be 1 space after return type colon.
|
4896 |
|
4897 | // Options: ["never"]
|
4898 | function a(): x {}
|
4899 | // Message: There must be no space after return type colon.
|
4900 |
|
4901 | type X = (foo:number) => string
|
4902 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4903 |
|
4904 | // Options: ["never"]
|
4905 | type X = (foo: number) => string
|
4906 | // Message: There must be no space after "foo" parameter type annotation colon.
|
4907 |
|
4908 | type X = (foo: number) => string
|
4909 | // Message: There must be 1 space after "foo" parameter type annotation colon.
|
4910 |
|
4911 | type X = (foo:?number) => string
|
4912 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4913 |
|
4914 | type X = (foo:(number)) => string
|
4915 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4916 |
|
4917 | type X = (foo:((number))) => string
|
4918 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4919 |
|
4920 | type X = (foo: ((number))) => string
|
4921 | // Message: There must be 1 space after "foo" parameter type annotation colon.
|
4922 |
|
4923 | // Options: ["never"]
|
4924 | type X = (foo: ((number))) => string
|
4925 | // Message: There must be no space after "foo" parameter type annotation colon.
|
4926 |
|
4927 | type X = (foo:?(number)) => string
|
4928 | // Message: There must be a space after "foo" parameter type annotation colon.
|
4929 |
|
4930 | type TArrayPredicate = (el: T, i?:number) => boolean
|
4931 | // Message: There must be a space after "i" parameter type annotation colon.
|
4932 |
|
4933 | type TArrayPredicate = (el: T, i?: number) => boolean
|
4934 | // Message: There must be 1 space after "i" parameter type annotation colon.
|
4935 |
|
4936 | // Options: ["never"]
|
4937 | type TArrayPredicate = (el:T, i?: number) => boolean
|
4938 | // Message: There must be no space after "i" parameter type annotation colon.
|
4939 |
|
4940 | class X { foo:string }
|
4941 | // Message: There must be a space after "foo" class property type annotation colon.
|
4942 |
|
4943 | // Options: ["never"]
|
4944 | class X { foo: string }
|
4945 | // Message: There must be no space after "foo" class property type annotation colon.
|
4946 |
|
4947 | class X { foo:?string }
|
4948 | // Message: There must be a space after "foo" class property type annotation colon.
|
4949 |
|
4950 | // Options: ["never"]
|
4951 | class X { foo: ?string }
|
4952 | // Message: There must be no space after "foo" class property type annotation colon.
|
4953 |
|
4954 | class X { static foo:number }
|
4955 | // Message: There must be a space after "foo" class property type annotation colon.
|
4956 |
|
4957 | // Options: ["never"]
|
4958 | class X { static foo: number }
|
4959 | // Message: There must be no space after "foo" class property type annotation colon.
|
4960 |
|
4961 | class X { static foo :number }
|
4962 | // Message: There must be a space after "foo" class property type annotation colon.
|
4963 |
|
4964 | // Options: ["never"]
|
4965 | class X { static foo : number }
|
4966 | // Message: There must be no space after "foo" class property type annotation colon.
|
4967 |
|
4968 | declare class X { static foo:number }
|
4969 | // Message: There must be a space after "foo" type annotation colon.
|
4970 |
|
4971 | // Options: ["never"]
|
4972 | declare class X { static foo: number }
|
4973 | // Message: There must be no space after "foo" type annotation colon.
|
4974 |
|
4975 | declare class X { static foo :number }
|
4976 | // Message: There must be a space after "foo" type annotation colon.
|
4977 |
|
4978 | // Options: ["never"]
|
4979 | declare class X { static foo : number }
|
4980 | // Message: There must be no space after "foo" type annotation colon.
|
4981 |
|
4982 | class X { +foo:string }
|
4983 | // Message: There must be a space after "foo" class property type annotation colon.
|
4984 |
|
4985 | class X { +foo: string }
|
4986 | // Message: There must be 1 space after "foo" class property type annotation colon.
|
4987 |
|
4988 | // Options: ["never"]
|
4989 | class X { +foo: string }
|
4990 | // Message: There must be no space after "foo" class property type annotation colon.
|
4991 |
|
4992 | class X { static +foo:string }
|
4993 | // Message: There must be a space after "foo" class property type annotation colon.
|
4994 |
|
4995 | class X { static +foo: string }
|
4996 | // Message: There must be 1 space after "foo" class property type annotation colon.
|
4997 |
|
4998 | // Options: ["never"]
|
4999 | class X { static +foo: string }
|
5000 | // Message: There must be no space after "foo" class property type annotation colon.
|
5001 |
|
5002 | type X = { foo:string }
|
5003 | // Message: There must be a space after "foo" type annotation colon.
|
5004 |
|
5005 | // Options: ["always"]
|
5006 | type X = { foo:string }
|
5007 | // Message: There must be a space after "foo" type annotation colon.
|
5008 |
|
5009 | // Options: ["never"]
|
5010 | type X = { foo: string }
|
5011 | // Message: There must be no space after "foo" type annotation colon.
|
5012 |
|
5013 | type X = { foo: string }
|
5014 | // Message: There must be 1 space after "foo" type annotation colon.
|
5015 |
|
5016 | type X = { foo?:string }
|
5017 | // Message: There must be a space after "foo" type annotation colon.
|
5018 |
|
5019 | // Options: ["never"]
|
5020 | type X = { foo?: string }
|
5021 | // Message: There must be no space after "foo" type annotation colon.
|
5022 |
|
5023 | type X = { foo?:?string }
|
5024 | // Message: There must be a space after "foo" type annotation colon.
|
5025 |
|
5026 | type X = { foo?: ?string }
|
5027 | // Message: There must be 1 space after "foo" type annotation colon.
|
5028 |
|
5029 | type Foo = { barType:(string | () => void) }
|
5030 | // Message: There must be a space after "barType" type annotation colon.
|
5031 |
|
5032 | type Foo = { barType:(((string | () => void))) }
|
5033 | // Message: There must be a space after "barType" type annotation colon.
|
5034 |
|
5035 | // Options: ["never"]
|
5036 | type Foo = { barType: (string | () => void) }
|
5037 | // Message: There must be no space after "barType" type annotation colon.
|
5038 |
|
5039 | type Foo = { barType: (string | () => void) }
|
5040 | // Message: There must be 1 space after "barType" type annotation colon.
|
5041 |
|
5042 | type Foo = { barType: ((string | () => void)) }
|
5043 | // Message: There must be 1 space after "barType" type annotation colon.
|
5044 |
|
5045 | type X = { get:() => A; }
|
5046 | // Message: There must be a space after "get" type annotation colon.
|
5047 |
|
5048 | type X = { get:<X>() => A; }
|
5049 | // Message: There must be a space after "get" type annotation colon.
|
5050 |
|
5051 | // Options: ["never"]
|
5052 | type X = { get: () => A; }
|
5053 | // Message: There must be no space after "get" type annotation colon.
|
5054 |
|
5055 | // Options: ["never"]
|
5056 | type X = { get: <X>() => A; }
|
5057 | // Message: There must be no space after "get" type annotation colon.
|
5058 |
|
5059 | type X = { get: () => A; }
|
5060 | // Message: There must be 1 space after "get" type annotation colon.
|
5061 |
|
5062 | type X = { get: <X>() => A; }
|
5063 | // Message: There must be 1 space after "get" type annotation colon.
|
5064 |
|
5065 | type X = { +foo:string }
|
5066 | // Message: There must be a space after "foo" type annotation colon.
|
5067 |
|
5068 | type X = { +foo: string }
|
5069 | // Message: There must be 1 space after "foo" type annotation colon.
|
5070 |
|
5071 | // Options: ["never"]
|
5072 | type X = { +foo: string }
|
5073 | // Message: There must be no space after "foo" type annotation colon.
|
5074 |
|
5075 | type X = { +foo?:string }
|
5076 | // Message: There must be a space after "foo" type annotation colon.
|
5077 |
|
5078 | type X = { +foo?: string }
|
5079 | // Message: There must be 1 space after "foo" type annotation colon.
|
5080 |
|
5081 | // Options: ["never"]
|
5082 | type X = { +foo?: string }
|
5083 | // Message: There must be no space after "foo" type annotation colon.
|
5084 |
|
5085 | // Options: ["always"]
|
5086 | type X = { [a:b]: c }
|
5087 | // Message: There must be a space after type annotation colon.
|
5088 |
|
5089 | // Options: ["never"]
|
5090 | type X = { [a: b]:c }
|
5091 | // Message: There must be no space after type annotation colon.
|
5092 |
|
5093 | // Options: ["always"]
|
5094 | type X = { [a: b]: c }
|
5095 | // Message: There must be 1 space after type annotation colon.
|
5096 |
|
5097 | // Options: ["always"]
|
5098 | type X = { +[a:b]: c }
|
5099 | // Message: There must be a space after type annotation colon.
|
5100 |
|
5101 | // Options: ["never"]
|
5102 | type X = { +[a: b]:c }
|
5103 | // Message: There must be no space after type annotation colon.
|
5104 |
|
5105 | // Options: ["always"]
|
5106 | type X = { +[a: b]: c }
|
5107 | // Message: There must be 1 space after type annotation colon.
|
5108 |
|
5109 | // Options: ["always"]
|
5110 | type X = { [a: b]:c }
|
5111 | // Message: There must be a space after type annotation colon.
|
5112 |
|
5113 | // Options: ["never"]
|
5114 | type X = { [a:b]: c }
|
5115 | // Message: There must be no space after type annotation colon.
|
5116 |
|
5117 | // Options: ["always"]
|
5118 | type X = { [a: b]: c }
|
5119 | // Message: There must be 1 space after type annotation colon.
|
5120 |
|
5121 | // Options: ["always"]
|
5122 | type X = { [a:b]:c }
|
5123 | // Message: There must be a space after type annotation colon.
|
5124 | // Message: There must be a space after type annotation colon.
|
5125 |
|
5126 | // Options: ["never"]
|
5127 | type X = { [a: b]: c }
|
5128 | // Message: There must be no space after type annotation colon.
|
5129 | // Message: There must be no space after type annotation colon.
|
5130 |
|
5131 | // Options: ["always"]
|
5132 | type X = { [a: b]: c }
|
5133 | // Message: There must be 1 space after type annotation colon.
|
5134 | // Message: There must be 1 space after type annotation colon.
|
5135 |
|
5136 | // Options: ["always"]
|
5137 | type X = { [a:(b)]:(c) }
|
5138 | // Message: There must be a space after type annotation colon.
|
5139 | // Message: There must be a space after type annotation colon.
|
5140 |
|
5141 | // Options: ["never"]
|
5142 | type X = { [a: (b)]: (c) }
|
5143 | // Message: There must be no space after type annotation colon.
|
5144 | // Message: There must be no space after type annotation colon.
|
5145 |
|
5146 | // Options: ["never"]
|
5147 | const x = ({}: {})
|
5148 | // Message: There must be no space after type cast colon.
|
5149 |
|
5150 | // Options: ["always"]
|
5151 | const x = ({}:{})
|
5152 | // Message: There must be a space after type cast colon.
|
5153 |
|
5154 | // Options: ["always"]
|
5155 | const x = ({}: {})
|
5156 | // Message: There must be 1 space after type cast colon.
|
5157 |
|
5158 | // Options: ["never"]
|
5159 | ((x): (string))
|
5160 | // Message: There must be no space after type cast colon.
|
5161 |
|
5162 | // Options: ["always"]
|
5163 | ((x):(string))
|
5164 | // Message: There must be a space after type cast colon.
|
5165 |
|
5166 | // Options: ["always"]
|
5167 | ((x): (string))
|
5168 | // Message: There must be 1 space after type cast colon.
|
5169 |
|
5170 | // Options: ["always"]
|
5171 | const x:number = 7;
|
5172 | // Message: There must be a space after const type annotation colon.
|
5173 |
|
5174 | // Options: ["always"]
|
5175 | let x:number = 42;
|
5176 | // Message: There must be a space after let type annotation colon.
|
5177 |
|
5178 | // Options: ["always"]
|
5179 | var x:number = 42;
|
5180 | // Message: There must be a space after var type annotation colon.
|
5181 | ```
|
5182 |
|
5183 | The following patterns are not considered problems:
|
5184 |
|
5185 | ```js
|
5186 | (foo) => {}
|
5187 |
|
5188 | (foo: string) => {}
|
5189 |
|
5190 | (foo: (string|number)) => {}
|
5191 |
|
5192 | // Options: ["never"]
|
5193 | (foo:string) => {}
|
5194 |
|
5195 | // Options: ["always"]
|
5196 | (foo: string) => {}
|
5197 |
|
5198 | // Options: ["never"]
|
5199 | (foo:(() => void)) => {}
|
5200 |
|
5201 | // Options: ["always"]
|
5202 | (foo: (() => void)) => {}
|
5203 |
|
5204 | ({ lorem, ipsum, dolor }: SomeType) => {}
|
5205 |
|
5206 | (foo: { a: string, b: number }) => {}
|
5207 |
|
5208 | ({ a, b }: ?{ a: string, b: number }) => {}
|
5209 |
|
5210 | ([ a, b ]: string[]) => {}
|
5211 |
|
5212 | (i?: number) => {}
|
5213 |
|
5214 | // Options: ["never"]
|
5215 | (i?:number) => {}
|
5216 |
|
5217 | // Options: ["always",{"allowLineBreak":true}]
|
5218 | (foo:
|
5219 | { a: string, b: number }) => {}
|
5220 |
|
5221 | // Options: ["always",{"allowLineBreak":true}]
|
5222 | (foo:
|
5223 | { a: string, b: number }) => {}
|
5224 |
|
5225 | // Options: ["never"]
|
5226 | ():Object => {}
|
5227 |
|
5228 | // Options: ["always"]
|
5229 | (): Object => {}
|
5230 |
|
5231 | // Options: ["never"]
|
5232 | ():(number | string) => {}
|
5233 |
|
5234 | // Options: ["always"]
|
5235 | (): (number | string) => {}
|
5236 |
|
5237 | // Options: ["never"]
|
5238 | ():number|string => {}
|
5239 |
|
5240 | // Options: ["always"]
|
5241 | (): number|string => {}
|
5242 |
|
5243 | // Options: ["never"]
|
5244 | ():(() => void) => {}
|
5245 |
|
5246 | // Options: ["always"]
|
5247 | (): (() => void) => {}
|
5248 |
|
5249 | // Options: ["never"]
|
5250 | ():( () => void ) => {}
|
5251 |
|
5252 | // Options: ["always"]
|
5253 | (): ( () => void ) => {}
|
5254 |
|
5255 | (): { a: number, b: string } => {}
|
5256 |
|
5257 | // Options: ["never"]
|
5258 | () :{ a:number, b:string } => {}
|
5259 |
|
5260 | function x(foo: string) {}
|
5261 |
|
5262 | class Foo { constructor(foo: string) {} }
|
5263 |
|
5264 | // Options: ["never"]
|
5265 | function x(foo:string) {}
|
5266 |
|
5267 | // Options: ["never"]
|
5268 | class Foo { constructor(foo:string) {} }
|
5269 |
|
5270 | async function foo({ lorem, ipsum, dolor }: SomeType) {}
|
5271 |
|
5272 | function x({ a, b }: { a: string, b: number }) {}
|
5273 |
|
5274 | function x(i?: number) {}
|
5275 |
|
5276 | // Options: ["never"]
|
5277 | function x(i?:number) {}
|
5278 |
|
5279 | function a(): x {}
|
5280 |
|
5281 | // Options: ["never"]
|
5282 | function a():x {}
|
5283 |
|
5284 | function a(): (number | string) {}
|
5285 |
|
5286 | // Options: ["never"]
|
5287 | function a() :(number | string) {}
|
5288 |
|
5289 | type X = (foo: number) => string;
|
5290 |
|
5291 | type X = (foo : number) => string;
|
5292 |
|
5293 | type X = (foo: ?number) => string;
|
5294 |
|
5295 | type X = (foo? : ?number) => string;
|
5296 |
|
5297 | type X = (foo: ?{ x: number }) => string;
|
5298 |
|
5299 | // Options: ["never"]
|
5300 | type X = (foo:number) => string;
|
5301 |
|
5302 | // Options: ["never"]
|
5303 | type X = (foo:?{ x:number }) => string;
|
5304 |
|
5305 | type X = (foo: (number)) => string
|
5306 |
|
5307 | type X = (foo: ((number))) => string
|
5308 |
|
5309 | // Options: ["never"]
|
5310 | type X = (foo:((number))) => string
|
5311 |
|
5312 | type X = ?(foo: ((number))) => string
|
5313 |
|
5314 | // Options: ["never"]
|
5315 | type X = ?(foo:((number))) => string
|
5316 |
|
5317 | type TArrayPredicate = (el: T, i?: number) => boolean
|
5318 |
|
5319 | // Options: ["never"]
|
5320 | type TArrayPredicate = (el:T, i?:number) => boolean
|
5321 |
|
5322 | type X = (number) => string;
|
5323 |
|
5324 | type X = (?number) => string;
|
5325 |
|
5326 | type X = number => string;
|
5327 |
|
5328 | type X = ?number => string;
|
5329 |
|
5330 | type X = ({ foo: bar }) => string;
|
5331 |
|
5332 | // Options: ["always"]
|
5333 | type X = (number) => string;
|
5334 |
|
5335 | // Options: ["always"]
|
5336 | type X = (?number) => string;
|
5337 |
|
5338 | // Options: ["always"]
|
5339 | type X = number => string;
|
5340 |
|
5341 | // Options: ["always"]
|
5342 | type X = ?number => string;
|
5343 |
|
5344 | // Options: ["always"]
|
5345 | type X = ({ foo: bar }) => string;
|
5346 |
|
5347 | class Foo { bar }
|
5348 |
|
5349 | class Foo { bar = 3 }
|
5350 |
|
5351 | class Foo { bar: string }
|
5352 |
|
5353 | class Foo { bar: ?string }
|
5354 |
|
5355 | // Options: ["never"]
|
5356 | class Foo { bar:string }
|
5357 |
|
5358 | // Options: ["never"]
|
5359 | class Foo { bar:?string }
|
5360 |
|
5361 | class X { static foo : number }
|
5362 |
|
5363 | // Options: ["never"]
|
5364 | class X { static foo :number }
|
5365 |
|
5366 | declare class X { static foo : number }
|
5367 |
|
5368 | // Options: ["never"]
|
5369 | declare class X { static foo :number }
|
5370 |
|
5371 | class X { +foo: string }
|
5372 |
|
5373 | class X { static +foo: string }
|
5374 |
|
5375 | // Options: ["never"]
|
5376 | class X { +foo:string }
|
5377 |
|
5378 | // Options: ["never"]
|
5379 | class X { static +foo:string }
|
5380 |
|
5381 | type X = { foo: string }
|
5382 |
|
5383 | // Options: ["never"]
|
5384 | type X = { foo:string }
|
5385 |
|
5386 | type X = { foo?: string }
|
5387 |
|
5388 | type X = { foo?: ?string }
|
5389 |
|
5390 | // Options: ["never"]
|
5391 | type X = { foo?:?string }
|
5392 |
|
5393 | type Foo = { barType: (string | () => void) }
|
5394 |
|
5395 | type Foo = { barType: ((string | () => void)) }
|
5396 |
|
5397 | // Options: ["never"]
|
5398 | type Foo = { barType:(string | () => void) }
|
5399 |
|
5400 | // Options: ["never"]
|
5401 | type Foo = { barType:((string | () => void)) }
|
5402 |
|
5403 | type X = { get(): A; }
|
5404 |
|
5405 | type X = { get<X>(): A; }
|
5406 |
|
5407 | // Options: ["never"]
|
5408 | type X = { get(): A; }
|
5409 |
|
5410 | // Options: ["never"]
|
5411 | type X = { get<X>(): A; }
|
5412 |
|
5413 | type X = { get: () => A; }
|
5414 |
|
5415 | type X = { get: <X>() => A; }
|
5416 |
|
5417 | // Options: ["never"]
|
5418 | type X = { get:() => A; }
|
5419 |
|
5420 | // Options: ["never"]
|
5421 | type X = { get:<X>() => A; }
|
5422 |
|
5423 | type X = { +foo: string }
|
5424 |
|
5425 | type X = { +foo?: string }
|
5426 |
|
5427 | // Options: ["never"]
|
5428 | type X = { +foo:string }
|
5429 |
|
5430 | // Options: ["never"]
|
5431 | type X = { +foo?:string }
|
5432 |
|
5433 | // Options: ["always"]
|
5434 | type X = { [a: b]: c }
|
5435 |
|
5436 | // Options: ["never"]
|
5437 | type X = { [a:b]:c }
|
5438 |
|
5439 | // Options: ["always"]
|
5440 | type X = { +[a: b]: c }
|
5441 |
|
5442 | // Options: ["never"]
|
5443 | type X = { +[a:b]:c }
|
5444 |
|
5445 | // Options: ["always"]
|
5446 | type X = { [string]: c }
|
5447 |
|
5448 | // Options: ["never"]
|
5449 | type X = { [string]:c }
|
5450 |
|
5451 | // Options: ["never"]
|
5452 | const x = ({}:{})
|
5453 |
|
5454 | // Options: ["always"]
|
5455 | const x = ({}: {})
|
5456 |
|
5457 | // Options: ["never"]
|
5458 | ((x):(string))
|
5459 |
|
5460 | // Options: ["always"]
|
5461 | ((x): (string))
|
5462 |
|
5463 | // Options: ["always"]
|
5464 | const x: number = 7;
|
5465 |
|
5466 | // Options: ["always"]
|
5467 | let x: number = 42;
|
5468 |
|
5469 | // Options: ["always"]
|
5470 | var x: number = 42;
|
5471 | ```
|
5472 |
|
5473 |
|
5474 |
|
5475 | <a name="eslint-plugin-flowtype-rules-space-before-generic-bracket"></a>
|
5476 | ### <code>space-before-generic-bracket</code>
|
5477 |
|
5478 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
5479 |
|
5480 | Enforces consistent spacing before the opening `<` of generic type annotation parameters.
|
5481 |
|
5482 | This rule takes one argument. If it is `'never'` then a problem is raised when there is a space before the `<`. If it is `'always'` then a problem is raised when there is no space before the `<`.
|
5483 |
|
5484 | The default value is `'never'`.
|
5485 |
|
5486 | The following patterns are considered problems:
|
5487 |
|
5488 | ```js
|
5489 | type X = Promise <string>
|
5490 | // Message: There must be no space before "Promise" generic type annotation bracket
|
5491 |
|
5492 | // Options: ["never"]
|
5493 | type X = Promise <string>
|
5494 | // Message: There must be no space before "Promise" generic type annotation bracket
|
5495 |
|
5496 | type X = Promise <string>
|
5497 | // Message: There must be no space before "Promise" generic type annotation bracket
|
5498 |
|
5499 | // Options: ["always"]
|
5500 | type X = Promise<string>
|
5501 | // Message: There must be a space before "Promise" generic type annotation bracket
|
5502 |
|
5503 | // Options: ["always"]
|
5504 | type X = Promise <string>
|
5505 | // Message: There must be one space before "Promise" generic type annotation bracket
|
5506 | ```
|
5507 |
|
5508 | The following patterns are not considered problems:
|
5509 |
|
5510 | ```js
|
5511 | type X = Promise<string>
|
5512 |
|
5513 | // Options: ["always"]
|
5514 | type X = Promise <string>
|
5515 | ```
|
5516 |
|
5517 |
|
5518 |
|
5519 | <a name="eslint-plugin-flowtype-rules-space-before-type-colon"></a>
|
5520 | ### <code>space-before-type-colon</code>
|
5521 |
|
5522 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
5523 |
|
5524 | Enforces consistent spacing before the type annotation colon.
|
5525 |
|
5526 | 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'`.
|
5527 |
|
5528 | The following patterns are considered problems:
|
5529 |
|
5530 | ```js
|
5531 | // Options: ["never"]
|
5532 | (foo : string) => {}
|
5533 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5534 |
|
5535 | // Options: ["never"]
|
5536 | (foo ? : string) => {}
|
5537 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5538 |
|
5539 | // Options: ["always"]
|
5540 | (foo: string) => {}
|
5541 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5542 |
|
5543 | // Options: ["always"]
|
5544 | (foo : string) => {}
|
5545 | // Message: There must be 1 space before "foo" parameter type annotation colon.
|
5546 |
|
5547 | // Options: ["always"]
|
5548 | (foo?: string) => {}
|
5549 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5550 |
|
5551 | // Options: ["always"]
|
5552 | (foo ? : string) => {}
|
5553 | // Message: There must be 1 space before "foo" parameter type annotation colon.
|
5554 |
|
5555 | // Options: ["always"]
|
5556 | (foo ?: string) => {}
|
5557 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5558 |
|
5559 | ({ lorem, ipsum, dolor } : SomeType) => {}
|
5560 | // Message: There must be no space before "{ lorem, ipsum, dolor }" parameter type annotation colon.
|
5561 |
|
5562 | (foo : { a: string, b: number }) => {}
|
5563 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5564 |
|
5565 | ({ a, b } : { a: string, b: number }) => {}
|
5566 | // Message: There must be no space before "{ a, b }" parameter type annotation colon.
|
5567 |
|
5568 | ([ a, b ] : string[]) => {}
|
5569 | // Message: There must be no space before "[ a, b ]" parameter type annotation colon.
|
5570 |
|
5571 | () : x => {}
|
5572 | // Message: There must be no space before return type colon.
|
5573 |
|
5574 | // Options: ["always"]
|
5575 | (): x => {}
|
5576 | // Message: There must be a space before return type colon.
|
5577 |
|
5578 | // Options: ["always"]
|
5579 | () : x => {}
|
5580 | // Message: There must be 1 space before return type colon.
|
5581 |
|
5582 | function x(foo : string) {}
|
5583 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5584 |
|
5585 | // Options: ["always"]
|
5586 | function x(foo: string) {}
|
5587 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5588 |
|
5589 | var x = function (foo : string) {}
|
5590 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5591 |
|
5592 | // Options: ["always"]
|
5593 | var x = function (foo: string) {}
|
5594 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5595 |
|
5596 | class Foo { constructor(foo : string ) {} }
|
5597 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5598 |
|
5599 | // Options: ["always"]
|
5600 | class Foo { constructor(foo: string ) {} }
|
5601 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5602 |
|
5603 | async function foo({ lorem, ipsum, dolor } : SomeType) {}
|
5604 | // Message: There must be no space before "{ lorem, ipsum, dolor }" parameter type annotation colon.
|
5605 |
|
5606 | function a() : x {}
|
5607 | // Message: There must be no space before return type colon.
|
5608 |
|
5609 | // Options: ["always"]
|
5610 | function a(): x {}
|
5611 | // Message: There must be a space before return type colon.
|
5612 |
|
5613 | // Options: ["always"]
|
5614 | function a() : x {}
|
5615 | // Message: There must be 1 space before return type colon.
|
5616 |
|
5617 | type X = (foo :string) => string;
|
5618 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5619 |
|
5620 | // Options: ["always"]
|
5621 | type X = (foo:string) => string;
|
5622 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5623 |
|
5624 | // Options: ["always"]
|
5625 | type X = (foo :string) => string;
|
5626 | // Message: There must be 1 space before "foo" parameter type annotation colon.
|
5627 |
|
5628 | type X = (foo? :string) => string;
|
5629 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5630 |
|
5631 | type X = (foo? :string) => string;
|
5632 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5633 |
|
5634 | // Options: ["always"]
|
5635 | type X = (foo?:string) => string;
|
5636 | // Message: There must be a space before "foo" parameter type annotation colon.
|
5637 |
|
5638 | type X = (foo? :?string) => string;
|
5639 | // Message: There must be no space before "foo" parameter type annotation colon.
|
5640 |
|
5641 | class X { foo :string }
|
5642 | // Message: There must be no space before "foo" class property type annotation colon.
|
5643 |
|
5644 | // Options: ["always"]
|
5645 | class X { foo: string }
|
5646 | // Message: There must be a space before "foo" class property type annotation colon.
|
5647 |
|
5648 | class X { foo :?string }
|
5649 | // Message: There must be no space before "foo" class property type annotation colon.
|
5650 |
|
5651 | // Options: ["always"]
|
5652 | class X { foo: ?string }
|
5653 | // Message: There must be a space before "foo" class property type annotation colon.
|
5654 |
|
5655 | class X { static foo : number }
|
5656 | // Message: There must be no space before "foo" class property type annotation colon.
|
5657 |
|
5658 | class X { static foo :number }
|
5659 | // Message: There must be no space before "foo" class property type annotation colon.
|
5660 |
|
5661 | // Options: ["always"]
|
5662 | class X { static foo: number }
|
5663 | // Message: There must be a space before "foo" class property type annotation colon.
|
5664 |
|
5665 | // Options: ["always"]
|
5666 | class X { static foo:number }
|
5667 | // Message: There must be a space before "foo" class property type annotation colon.
|
5668 |
|
5669 | declare class Foo { static bar :number; }
|
5670 | // Message: There must be no space before "bar" type annotation colon.
|
5671 |
|
5672 | declare class Foo { static bar : number; }
|
5673 | // Message: There must be no space before "bar" type annotation colon.
|
5674 |
|
5675 | // Options: ["always"]
|
5676 | declare class Foo { static bar:number; }
|
5677 | // Message: There must be a space before "bar" type annotation colon.
|
5678 |
|
5679 | // Options: ["always"]
|
5680 | declare class Foo { static bar: number; }
|
5681 | // Message: There must be a space before "bar" type annotation colon.
|
5682 |
|
5683 | // Options: ["always"]
|
5684 | class X { +foo: string }
|
5685 | // Message: There must be a space before "foo" class property type annotation colon.
|
5686 |
|
5687 | // Options: ["always"]
|
5688 | class X { +foo : string }
|
5689 | // Message: There must be 1 space before "foo" class property type annotation colon.
|
5690 |
|
5691 | // Options: ["never"]
|
5692 | class X { +foo : string }
|
5693 | // Message: There must be no space before "foo" class property type annotation colon.
|
5694 |
|
5695 | // Options: ["always"]
|
5696 | class X { static +foo: string }
|
5697 | // Message: There must be a space before "foo" class property type annotation colon.
|
5698 |
|
5699 | // Options: ["always"]
|
5700 | class X { static +foo : string }
|
5701 | // Message: There must be 1 space before "foo" class property type annotation colon.
|
5702 |
|
5703 | // Options: ["never"]
|
5704 | class X { static +foo : string }
|
5705 | // Message: There must be no space before "foo" class property type annotation colon.
|
5706 |
|
5707 | type X = { foo : string }
|
5708 | // Message: There must be no space before "foo" type annotation colon.
|
5709 |
|
5710 | // Options: ["never"]
|
5711 | type X = { foo : string }
|
5712 | // Message: There must be no space before "foo" type annotation colon.
|
5713 |
|
5714 | // Options: ["always"]
|
5715 | type X = { foo: string }
|
5716 | // Message: There must be a space before "foo" type annotation colon.
|
5717 |
|
5718 | // Options: ["always"]
|
5719 | type X = { foo : string }
|
5720 | // Message: There must be 1 space before "foo" type annotation colon.
|
5721 |
|
5722 | type X = { foo? : string }
|
5723 | // Message: There must be no space before "foo" type annotation colon.
|
5724 |
|
5725 | // Options: ["always"]
|
5726 | type X = { foo?: string }
|
5727 | // Message: There must be a space before "foo" type annotation colon.
|
5728 |
|
5729 | // Options: ["always"]
|
5730 | type X = { foo? : string }
|
5731 | // Message: There must be 1 space before "foo" type annotation colon.
|
5732 |
|
5733 | // Options: ["always"]
|
5734 | type X = { foo ?: string }
|
5735 | // Message: There must be a space before "foo" type annotation colon.
|
5736 |
|
5737 | // Options: ["always"]
|
5738 | type X = { +foo: string }
|
5739 | // Message: There must be a space before "foo" type annotation colon.
|
5740 |
|
5741 | // Options: ["always"]
|
5742 | type X = { +foo : string }
|
5743 | // Message: There must be 1 space before "foo" type annotation colon.
|
5744 |
|
5745 | // Options: ["never"]
|
5746 | type X = { +foo : string }
|
5747 | // Message: There must be no space before "foo" type annotation colon.
|
5748 |
|
5749 | // Options: ["always"]
|
5750 | type X = { +foo?: string }
|
5751 | // Message: There must be a space before "foo" type annotation colon.
|
5752 |
|
5753 | // Options: ["always"]
|
5754 | type X = { +foo? : string }
|
5755 | // Message: There must be 1 space before "foo" type annotation colon.
|
5756 |
|
5757 | // Options: ["never"]
|
5758 | type X = { +foo? : string }
|
5759 | // Message: There must be no space before "foo" type annotation colon.
|
5760 |
|
5761 | // Options: ["always"]
|
5762 | type X = { [a: b] : c }
|
5763 | // Message: There must be a space before type annotation colon.
|
5764 |
|
5765 | // Options: ["never"]
|
5766 | type X = { [a : b]: c }
|
5767 | // Message: There must be no space before type annotation colon.
|
5768 |
|
5769 | // Options: ["always"]
|
5770 | type X = { [a : b] : c }
|
5771 | // Message: There must be 1 space before type annotation colon.
|
5772 |
|
5773 | // Options: ["always"]
|
5774 | type X = { +[a:b] : c }
|
5775 | // Message: There must be a space before type annotation colon.
|
5776 |
|
5777 | // Options: ["never"]
|
5778 | type X = { +[a : b]: c }
|
5779 | // Message: There must be no space before type annotation colon.
|
5780 |
|
5781 | // Options: ["always"]
|
5782 | type X = { +[a : b] : c }
|
5783 | // Message: There must be 1 space before type annotation colon.
|
5784 |
|
5785 | // Options: ["always"]
|
5786 | type X = { [a : b]: c }
|
5787 | // Message: There must be a space before type annotation colon.
|
5788 |
|
5789 | // Options: ["never"]
|
5790 | type X = { [a: b] : c }
|
5791 | // Message: There must be no space before type annotation colon.
|
5792 |
|
5793 | // Options: ["always"]
|
5794 | type X = { [a : b] : c }
|
5795 | // Message: There must be 1 space before type annotation colon.
|
5796 |
|
5797 | // Options: ["always"]
|
5798 | type X = { [a:b]:c }
|
5799 | // Message: There must be a space before type annotation colon.
|
5800 | // Message: There must be a space before type annotation colon.
|
5801 |
|
5802 | // Options: ["never"]
|
5803 | type X = { [a : b] : c }
|
5804 | // Message: There must be no space before type annotation colon.
|
5805 | // Message: There must be no space before type annotation colon.
|
5806 |
|
5807 | // Options: ["always"]
|
5808 | type X = { [a : b] : c }
|
5809 | // Message: There must be 1 space before type annotation colon.
|
5810 | // Message: There must be 1 space before type annotation colon.
|
5811 |
|
5812 | // Options: ["always"]
|
5813 | type X = { [a:(b)]:(c) }
|
5814 | // Message: There must be a space before type annotation colon.
|
5815 | // Message: There must be a space before type annotation colon.
|
5816 |
|
5817 | // Options: ["never"]
|
5818 | type X = { [a : (b)] : (c) }
|
5819 | // Message: There must be no space before type annotation colon.
|
5820 | // Message: There must be no space before type annotation colon.
|
5821 |
|
5822 | // Options: ["never"]
|
5823 | const x = ({} :{})
|
5824 | // Message: There must be no space before type cast colon.
|
5825 |
|
5826 | // Options: ["always"]
|
5827 | const x = ({}:{})
|
5828 | // Message: There must be a space before type cast colon.
|
5829 |
|
5830 | // Options: ["always"]
|
5831 | const x = ({} :{})
|
5832 | // Message: There must be 1 space before type cast colon.
|
5833 |
|
5834 | // Options: ["never"]
|
5835 | ((x) : string)
|
5836 | // Message: There must be no space before type cast colon.
|
5837 |
|
5838 | // Options: ["always"]
|
5839 | ((x): string)
|
5840 | // Message: There must be a space before type cast colon.
|
5841 |
|
5842 | // Options: ["always"]
|
5843 | ((x) : string)
|
5844 | // Message: There must be 1 space before type cast colon.
|
5845 |
|
5846 | // Options: ["always"]
|
5847 | const x:number = 7;
|
5848 | // Message: There must be a space before const type annotation colon.
|
5849 |
|
5850 | // Options: ["always"]
|
5851 | let x:number = 42;
|
5852 | // Message: There must be a space before let type annotation colon.
|
5853 |
|
5854 | // Options: ["always"]
|
5855 | var x:number = 42;
|
5856 | // Message: There must be a space before var type annotation colon.
|
5857 | ```
|
5858 |
|
5859 | The following patterns are not considered problems:
|
5860 |
|
5861 | ```js
|
5862 | (foo) => {}
|
5863 |
|
5864 | (foo: string) => {}
|
5865 |
|
5866 | (foo?: string) => {}
|
5867 |
|
5868 | (foo ?: string) => {}
|
5869 |
|
5870 | // Options: ["never"]
|
5871 | (foo: string) => {}
|
5872 |
|
5873 | // Options: ["always"]
|
5874 | (foo : string) => {}
|
5875 |
|
5876 | // Options: ["always"]
|
5877 | (foo? : string) => {}
|
5878 |
|
5879 | // Options: ["always"]
|
5880 | (foo ? : string) => {}
|
5881 |
|
5882 | // Options: ["always"]
|
5883 | (foo ? : string) => {}
|
5884 |
|
5885 | ({ lorem, ipsum, dolor }: SomeType) => {}
|
5886 |
|
5887 | (foo: { a: string, b: number }) => {}
|
5888 |
|
5889 | ({ a, b }: ?{ a: string, b: number }) => {}
|
5890 |
|
5891 | (): { a: number, b: string } => {}
|
5892 |
|
5893 | // Options: ["always"]
|
5894 | () : { a : number, b : string } => {}
|
5895 |
|
5896 | ([ a, b ]: string[]) => {}
|
5897 |
|
5898 | (): x => {}
|
5899 |
|
5900 | // Options: ["always"]
|
5901 | () : x => {}
|
5902 |
|
5903 | (): (number | string) => {}
|
5904 |
|
5905 | // Options: ["always"]
|
5906 | () : (number | string) => {}
|
5907 |
|
5908 | function x(foo: string) {}
|
5909 |
|
5910 | // Options: ["always"]
|
5911 | function x(foo : string) {}
|
5912 |
|
5913 | var x = function (foo: string) {}
|
5914 |
|
5915 | // Options: ["always"]
|
5916 | var x = function (foo : string) {}
|
5917 |
|
5918 | class X { foo({ bar }: Props = this.props) {} }
|
5919 |
|
5920 | class Foo { constructor(foo: string ) {} }
|
5921 |
|
5922 | // Options: ["always"]
|
5923 | class Foo { constructor(foo : string ) {} }
|
5924 |
|
5925 | async function foo({ lorem, ipsum, dolor }: SomeType) {}
|
5926 |
|
5927 | function x({ a, b }: { a: string, b: number }) {}
|
5928 |
|
5929 | function a(): x {}
|
5930 |
|
5931 | // Options: ["always"]
|
5932 | function a() : x {}
|
5933 |
|
5934 | function a(): (number | string) {}
|
5935 |
|
5936 | // Options: ["always"]
|
5937 | function a() : (number | string) {}
|
5938 |
|
5939 | type X = (foo:string) => number;
|
5940 |
|
5941 | type X = (foo: string) => number;
|
5942 |
|
5943 | type X = (foo: ?string) => number;
|
5944 |
|
5945 | type X = (foo?: string) => number;
|
5946 |
|
5947 | type X = (foo?: ?string) => number;
|
5948 |
|
5949 | type X = (foo ?: string) => number;
|
5950 |
|
5951 | // Options: ["always"]
|
5952 | type X = (foo? : string) => number
|
5953 |
|
5954 | // Options: ["always"]
|
5955 | type X = (foo? : ?string) => number
|
5956 |
|
5957 | type X = (number) => string;
|
5958 |
|
5959 | type X = (?number) => string;
|
5960 |
|
5961 | type X = number => string;
|
5962 |
|
5963 | type X = ?number => string;
|
5964 |
|
5965 | type X = ({ foo: bar }) => string;
|
5966 |
|
5967 | // Options: ["always"]
|
5968 | type X = (number) => string;
|
5969 |
|
5970 | // Options: ["always"]
|
5971 | type X = (?number) => string;
|
5972 |
|
5973 | // Options: ["always"]
|
5974 | type X = number => string;
|
5975 |
|
5976 | // Options: ["always"]
|
5977 | type X = ?number => string;
|
5978 |
|
5979 | // Options: ["always"]
|
5980 | type X = ({ foo : bar }) => string;
|
5981 |
|
5982 | class Foo { bar }
|
5983 |
|
5984 | class Foo { bar = 3 }
|
5985 |
|
5986 | class Foo { bar: string }
|
5987 |
|
5988 | class Foo { bar: ?string }
|
5989 |
|
5990 | class Foo { bar:?string }
|
5991 |
|
5992 | // Options: ["always"]
|
5993 | class Foo { bar : string }
|
5994 |
|
5995 | class X { static foo:number }
|
5996 |
|
5997 | class X { static foo: number }
|
5998 |
|
5999 | // Options: ["always"]
|
6000 | class X { static foo :number }
|
6001 |
|
6002 | // Options: ["always"]
|
6003 | class X { static foo : number }
|
6004 |
|
6005 | declare class Foo { static bar:number; }
|
6006 |
|
6007 | // Options: ["always"]
|
6008 | declare class Foo { static bar :number; }
|
6009 |
|
6010 | declare class Foo { static bar: number; }
|
6011 |
|
6012 | // Options: ["always"]
|
6013 | declare class Foo { static bar : number; }
|
6014 |
|
6015 | class X { +foo: string }
|
6016 |
|
6017 | class X { static +foo: string }
|
6018 |
|
6019 | // Options: ["always"]
|
6020 | class X { +foo : string }
|
6021 |
|
6022 | // Options: ["always"]
|
6023 | class X { static +foo : string }
|
6024 |
|
6025 | type X = { foo: string }
|
6026 |
|
6027 | // Options: ["always"]
|
6028 | type X = { foo : string }
|
6029 |
|
6030 | type X = { foo?: string }
|
6031 |
|
6032 | type X = { foo ?: string }
|
6033 |
|
6034 | // Options: ["always"]
|
6035 | type X = { foo? : string }
|
6036 |
|
6037 | type X = { +foo: string }
|
6038 |
|
6039 | type X = { +foo?: string }
|
6040 |
|
6041 | // Options: ["always"]
|
6042 | type X = { +foo : string }
|
6043 |
|
6044 | // Options: ["always"]
|
6045 | type X = { +foo? : string }
|
6046 |
|
6047 | // Options: ["always"]
|
6048 | type X = { [a : b] : c }
|
6049 |
|
6050 | // Options: ["never"]
|
6051 | type X = { [a:b]:c }
|
6052 |
|
6053 | // Options: ["always"]
|
6054 | type X = { [string] : c }
|
6055 |
|
6056 | // Options: ["never"]
|
6057 | type X = { [string]:c }
|
6058 |
|
6059 | // Options: ["always"]
|
6060 | type X = { +[a : b] : c }
|
6061 |
|
6062 | // Options: ["never"]
|
6063 | type X = { +[a:b]:c }
|
6064 |
|
6065 | // Options: ["always"]
|
6066 | type X = { [a : (b)] : (c) }
|
6067 |
|
6068 | // Options: ["never"]
|
6069 | type X = { [a:(b)]:(c) }
|
6070 |
|
6071 | // Options: ["never"]
|
6072 | const x = ({}:{})
|
6073 |
|
6074 | // Options: ["always"]
|
6075 | const x = ({} :{})
|
6076 |
|
6077 | // Options: ["never"]
|
6078 | ((x): string)
|
6079 |
|
6080 | // Options: ["always"]
|
6081 | ((x) : string)
|
6082 |
|
6083 | // Options: ["always"]
|
6084 | const x :number = 7;
|
6085 |
|
6086 | // Options: ["always"]
|
6087 | let x :number = 42;
|
6088 |
|
6089 | // Options: ["always"]
|
6090 | var x :number = 42;
|
6091 | ```
|
6092 |
|
6093 |
|
6094 |
|
6095 | <a name="eslint-plugin-flowtype-rules-spread-exact-type"></a>
|
6096 | ### <code>spread-exact-type</code>
|
6097 |
|
6098 | Enforce object types, that are spread to be exact type explicitly.
|
6099 |
|
6100 | The following patterns are considered problems:
|
6101 |
|
6102 | ```js
|
6103 | type bar = {...{test: string}}
|
6104 | // Message: Use $Exact to make type spreading safe.
|
6105 |
|
6106 | type foo = {test: number}; type bar = {...foo}
|
6107 | // Message: Use $Exact to make type spreading safe.
|
6108 | ```
|
6109 |
|
6110 | The following patterns are not considered problems:
|
6111 |
|
6112 | ```js
|
6113 | type bar = {...$Exact<{test: string}>}
|
6114 |
|
6115 | type foo = {test: number}; type bar = {...$Exact<foo>}
|
6116 | ```
|
6117 |
|
6118 |
|
6119 |
|
6120 | <a name="eslint-plugin-flowtype-rules-type-id-match"></a>
|
6121 | ### <code>type-id-match</code>
|
6122 |
|
6123 | Enforces a consistent naming pattern for type aliases.
|
6124 |
|
6125 | <a name="eslint-plugin-flowtype-rules-type-id-match-options-14"></a>
|
6126 | #### Options
|
6127 |
|
6128 | This rule requires a text RegExp:
|
6129 |
|
6130 | ```js
|
6131 | {
|
6132 | "rules": {
|
6133 | "flowtype/type-id-match": [
|
6134 | 2,
|
6135 | "^([A-Z][a-z0-9]*)+Type$"
|
6136 | ]
|
6137 | }
|
6138 | }
|
6139 | ```
|
6140 |
|
6141 | `'^([A-Z][a-z0-9]*)+Type$$'` is the default pattern.
|
6142 |
|
6143 | The following patterns are considered problems:
|
6144 |
|
6145 | ```js
|
6146 | opaque type foo = {};
|
6147 | // Message: Type identifier 'foo' does not match pattern '/^([A-Z][a-z0-9]*)+Type$/'.
|
6148 |
|
6149 | type foo = {};
|
6150 | // Message: Type identifier 'foo' does not match pattern '/^([A-Z][a-z0-9]*)+Type$/'.
|
6151 |
|
6152 | // Options: ["^foo$"]
|
6153 | type FooType = {};
|
6154 | // Message: Type identifier 'FooType' does not match pattern '/^foo$/'.
|
6155 | ```
|
6156 |
|
6157 | The following patterns are not considered problems:
|
6158 |
|
6159 | ```js
|
6160 | type FooType = {};
|
6161 |
|
6162 | // Options: ["^foo$"]
|
6163 | type foo = {};
|
6164 |
|
6165 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
6166 | type foo = {};
|
6167 | ```
|
6168 |
|
6169 |
|
6170 |
|
6171 | <a name="eslint-plugin-flowtype-rules-type-import-style"></a>
|
6172 | ### <code>type-import-style</code>
|
6173 |
|
6174 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
6175 |
|
6176 | Enforces a particular style for type imports:
|
6177 |
|
6178 | ```
|
6179 | // 'identifier' style
|
6180 | import {type T, type U, type V} from '...';
|
6181 |
|
6182 | // 'declaration' style
|
6183 | import type {T, U, V} from '...';
|
6184 | ```
|
6185 |
|
6186 | <a name="eslint-plugin-flowtype-rules-type-import-style-options-15"></a>
|
6187 | #### Options
|
6188 |
|
6189 | The rule has a string option:
|
6190 |
|
6191 | * `"identifier"` (default): Enforces that type imports are all in the
|
6192 | 'identifier' style.
|
6193 | * `"declaration"`: Enforces that type imports are all in the 'declaration'
|
6194 | style.
|
6195 |
|
6196 | This rule has an object option:
|
6197 |
|
6198 | * `ignoreTypeDefault` - if `true`, when in "identifier" mode, default type imports will be ignored. Default is `false`.
|
6199 |
|
6200 | The following patterns are considered problems:
|
6201 |
|
6202 | ```js
|
6203 | import type {A, B} from 'a';
|
6204 | // Message: Unexpected "import type"
|
6205 |
|
6206 | // Options: ["identifier"]
|
6207 | import type {A, B} from 'a';
|
6208 | // Message: Unexpected "import type"
|
6209 |
|
6210 | // Options: ["identifier"]
|
6211 | import type {A, B as C} from 'a';
|
6212 | // Message: Unexpected "import type"
|
6213 |
|
6214 | // Options: ["identifier"]
|
6215 | import type A from 'a';
|
6216 | // Message: Unexpected "import type"
|
6217 |
|
6218 | // Options: ["declaration"]
|
6219 | import {type A, type B} from 'a';
|
6220 | // Message: Unexpected type import
|
6221 | // Message: Unexpected type import
|
6222 | ```
|
6223 |
|
6224 | The following patterns are not considered problems:
|
6225 |
|
6226 | ```js
|
6227 | import {type A, type B} from 'a';
|
6228 |
|
6229 | // Options: ["identifier"]
|
6230 | import {type A, type B} from 'a';
|
6231 |
|
6232 | // Options: ["declaration"]
|
6233 | import type {A, B} from 'a';
|
6234 |
|
6235 | // Options: ["identifier"]
|
6236 | import typeof * as A from 'a';
|
6237 |
|
6238 | // Options: ["identifier",{"ignoreTypeDefault":true}]
|
6239 | import type A from 'a';
|
6240 |
|
6241 | // Options: ["identifier"]
|
6242 | declare module "m" { import type A from 'a'; }
|
6243 | ```
|
6244 |
|
6245 |
|
6246 |
|
6247 | <a name="eslint-plugin-flowtype-rules-union-intersection-spacing"></a>
|
6248 | ### <code>union-intersection-spacing</code>
|
6249 |
|
6250 | _The `--fix` option on the command line automatically fixes problems reported by this rule._
|
6251 |
|
6252 | Enforces consistent spacing around union and intersection type separators (`|` and `&`).
|
6253 |
|
6254 | This rule takes one argument. If it is `'always'` then a problem is raised when there is no space around the separator. If it is `'never'` then a problem is raised when there is a space around the separator.
|
6255 |
|
6256 | The default value is `'always'`.
|
6257 |
|
6258 | The following patterns are considered problems:
|
6259 |
|
6260 | ```js
|
6261 | type X = string| number;
|
6262 | // Message: There must be a space before union type annotation separator
|
6263 |
|
6264 | // Options: ["always"]
|
6265 | type X = string| number;
|
6266 | // Message: There must be a space before union type annotation separator
|
6267 |
|
6268 | type X = string |number;
|
6269 | // Message: There must be a space after union type annotation separator
|
6270 |
|
6271 | type X = string|number;
|
6272 | // Message: There must be a space before union type annotation separator
|
6273 | // Message: There must be a space after union type annotation separator
|
6274 |
|
6275 | type X = {x: string}|{y: number};
|
6276 | // Message: There must be a space before union type annotation separator
|
6277 | // Message: There must be a space after union type annotation separator
|
6278 |
|
6279 | type X = string | number |boolean;
|
6280 | // Message: There must be a space after union type annotation separator
|
6281 |
|
6282 | type X = string|number|boolean;
|
6283 | // Message: There must be a space before union type annotation separator
|
6284 | // Message: There must be a space after union type annotation separator
|
6285 | // Message: There must be a space before union type annotation separator
|
6286 | // Message: There must be a space after union type annotation separator
|
6287 |
|
6288 | type X = (string)| number;
|
6289 | // Message: There must be a space before union type annotation separator
|
6290 |
|
6291 | type X = ((string))|(number | foo);
|
6292 | // Message: There must be a space before union type annotation separator
|
6293 | // Message: There must be a space after union type annotation separator
|
6294 |
|
6295 | // Options: ["never"]
|
6296 | type X = string |number;
|
6297 | // Message: There must be no space before union type annotation separator
|
6298 |
|
6299 | // Options: ["never"]
|
6300 | type X = string| number;
|
6301 | // Message: There must be no space after union type annotation separator
|
6302 |
|
6303 | type X = string& number;
|
6304 | // Message: There must be a space before intersection type annotation separator
|
6305 |
|
6306 | // Options: ["always"]
|
6307 | type X = string& number;
|
6308 | // Message: There must be a space before intersection type annotation separator
|
6309 |
|
6310 | type X = string &number;
|
6311 | // Message: There must be a space after intersection type annotation separator
|
6312 |
|
6313 | type X = {x: string}&{y: number};
|
6314 | // Message: There must be a space before intersection type annotation separator
|
6315 | // Message: There must be a space after intersection type annotation separator
|
6316 |
|
6317 | type X = string&number;
|
6318 | // Message: There must be a space before intersection type annotation separator
|
6319 | // Message: There must be a space after intersection type annotation separator
|
6320 |
|
6321 | type X = string & number &boolean;
|
6322 | // Message: There must be a space after intersection type annotation separator
|
6323 |
|
6324 | type X = string&number&boolean;
|
6325 | // Message: There must be a space before intersection type annotation separator
|
6326 | // Message: There must be a space after intersection type annotation separator
|
6327 | // Message: There must be a space before intersection type annotation separator
|
6328 | // Message: There must be a space after intersection type annotation separator
|
6329 |
|
6330 | type X = (string)& number;
|
6331 | // Message: There must be a space before intersection type annotation separator
|
6332 |
|
6333 | type X = ((string))&(number & foo);
|
6334 | // Message: There must be a space before intersection type annotation separator
|
6335 | // Message: There must be a space after intersection type annotation separator
|
6336 |
|
6337 | // Options: ["never"]
|
6338 | type X = string &number;
|
6339 | // Message: There must be no space before intersection type annotation separator
|
6340 |
|
6341 | // Options: ["never"]
|
6342 | type X = string& number;
|
6343 | // Message: There must be no space after intersection type annotation separator
|
6344 | ```
|
6345 |
|
6346 | The following patterns are not considered problems:
|
6347 |
|
6348 | ```js
|
6349 | type X = string | number;
|
6350 |
|
6351 | type X = string | number | boolean;
|
6352 |
|
6353 | type X = (string) | number;
|
6354 |
|
6355 | type X = ((string)) | (number | foo);
|
6356 |
|
6357 | // Options: ["never"]
|
6358 | type X = string|number
|
6359 |
|
6360 | type X =
|
6361 | | string
|
6362 | | number
|
6363 |
|
6364 | function x() {
|
6365 | type X =
|
6366 | | string
|
6367 | | number
|
6368 | }
|
6369 |
|
6370 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
6371 | type X = string| number;
|
6372 |
|
6373 | type X = string & number;
|
6374 |
|
6375 | type X = string & number & boolean;
|
6376 |
|
6377 | type X = (string) & number;
|
6378 |
|
6379 | type X = ((string)) & (number & foo);
|
6380 |
|
6381 | // Options: ["never"]
|
6382 | type X = string&number
|
6383 |
|
6384 | type X =
|
6385 | & string
|
6386 | & number
|
6387 |
|
6388 | function x() {
|
6389 | type X =
|
6390 | & string
|
6391 | & number
|
6392 | }
|
6393 |
|
6394 | // Settings: {"flowtype":{"onlyFilesWithFlowAnnotation":true}}
|
6395 | type X = string& number;
|
6396 | ```
|
6397 |
|
6398 |
|
6399 |
|
6400 | <a name="eslint-plugin-flowtype-rules-use-flow-type"></a>
|
6401 | ### <code>use-flow-type</code>
|
6402 |
|
6403 | Marks Flow [type alias](https://flowtype.org/docs/type-aliases.html) declarations as used.
|
6404 |
|
6405 | Used to suppress [`no-unused-vars`](http://eslint.org/docs/rules/no-unused-vars) errors that are triggered by type aliases.
|
6406 |
|
6407 | The following patterns are not considered problems:
|
6408 |
|
6409 | ```js
|
6410 | declare class A {}
|
6411 | // Additional rules: {"no-unused-vars":1}
|
6412 |
|
6413 | declare function A(): Y
|
6414 | // Additional rules: {"no-unused-vars":1}
|
6415 |
|
6416 | declare module A {}
|
6417 | // Additional rules: {"no-unused-vars":1}
|
6418 |
|
6419 | declare module A { declare var a: Y }
|
6420 | // Additional rules: {"no-unused-vars":1}
|
6421 |
|
6422 | declare var A: Y
|
6423 | // Additional rules: {"no-unused-vars":1}
|
6424 |
|
6425 | import type A from "a"; type X<B = ComponentType<A>> = { b: B }; let x: X; console.log(x);
|
6426 | // Additional rules: {"no-unused-vars":1}
|
6427 |
|
6428 | import type A from "a"; type X<B = A<string>> = { b: B }; let x: X; console.log(x);
|
6429 | // Additional rules: {"no-unused-vars":1}
|
6430 | ```
|
6431 |
|
6432 |
|
6433 |
|
6434 | <a name="eslint-plugin-flowtype-rules-use-read-only-spread"></a>
|
6435 | ### <code>use-read-only-spread</code>
|
6436 |
|
6437 | Warns against accidentally creating an object which is no longer read-only because of how spread operator works in Flow. Imagine the following code:
|
6438 |
|
6439 | ```flow js
|
6440 | type INode = {|
|
6441 | +type: string,
|
6442 | |};
|
6443 |
|
6444 | type Identifier = {|
|
6445 | ...INode,
|
6446 | +name: string,
|
6447 | |};
|
6448 | ```
|
6449 |
|
6450 | You might expect the identifier name to be read-only, however, that's not true ([flow.org/try](https://flow.org/try/#0C4TwDgpgBAkgcgewCbQLxQN4B8BQUoDUokAXFAM7ABOAlgHYDmANDlgL4DcOOx0MKdYDQBmNCFSjpseKADp58ZBBb4CdAIYBbCGUq1GLdlxwBjBHUpQAHmX4RBIsRKlQN2sgHIPTKL08eoTm4rWV5JKA8AZQALBABXABskVwRgKAAjaAB3WmB1dISIAEIPLhC3NAiY+KSUtMyoHJo8guLSnCA)):
|
6451 |
|
6452 | ```flow js
|
6453 | const x: Identifier = { name: '', type: '' };
|
6454 |
|
6455 | x.type = 'Should not be writable!'; // No Flow error
|
6456 | x.name = 'Should not be writable!'; // No Flow error
|
6457 | ```
|
6458 |
|
6459 | This rule suggests to use `$ReadOnly<…>` to prevent accidental loss of readonly-ness:
|
6460 |
|
6461 | ```flow js
|
6462 | type Identifier = $ReadOnly<{|
|
6463 | ...INode,
|
6464 | +name: string,
|
6465 | |}>;
|
6466 |
|
6467 | const x: Identifier = { name: '', type: '' };
|
6468 |
|
6469 | x.type = 'Should not be writable!'; // $FlowExpectedError[cannot-write]
|
6470 | x.name = 'Should not be writable!'; // $FlowExpectedError[cannot-write]
|
6471 | ```
|
6472 |
|
6473 | The following patterns are considered problems:
|
6474 |
|
6475 | ```js
|
6476 | type INode = {||};
|
6477 | type Identifier = {|
|
6478 | ...INode,
|
6479 | +aaa: string,
|
6480 | |};
|
6481 | // Message: Flow type with spread property and all readonly properties should be wrapped in '$ReadOnly<…>' to prevent accidental loss of readonly-ness.
|
6482 |
|
6483 | type INode = {||};
|
6484 | type Identifier = {|
|
6485 | ...INode,
|
6486 | +aaa: string,
|
6487 | +bbb: string,
|
6488 | |};
|
6489 | // Message: Flow type with spread property and all readonly properties should be wrapped in '$ReadOnly<…>' to prevent accidental loss of readonly-ness.
|
6490 | ```
|
6491 |
|
6492 | The following patterns are not considered problems:
|
6493 |
|
6494 | ```js
|
6495 | type INode = {||};
|
6496 | type Identifier = {|
|
6497 | ...INode,
|
6498 | name: string,
|
6499 | |};
|
6500 |
|
6501 | type INode = {||};
|
6502 | type Identifier = {|
|
6503 | ...INode,
|
6504 | name: string, // writable on purpose
|
6505 | +surname: string,
|
6506 | |};
|
6507 |
|
6508 | type Identifier = {|
|
6509 | +name: string,
|
6510 | |};
|
6511 |
|
6512 | type INode = {||};
|
6513 | type Identifier = $ReadOnly<{|
|
6514 | ...INode,
|
6515 | +name: string,
|
6516 | |}>;
|
6517 |
|
6518 | type INode = {||};
|
6519 | type Identifier = $ReadOnly<{|
|
6520 | ...INode,
|
6521 | name: string, // writable on purpose
|
6522 | |}>;
|
6523 |
|
6524 | type INode = {||};
|
6525 | type Identifier = $ReadOnly<{|
|
6526 | ...INode,
|
6527 | -name: string,
|
6528 | |}>;
|
6529 | ```
|
6530 |
|
6531 |
|
6532 |
|
6533 | <a name="eslint-plugin-flowtype-rules-valid-syntax"></a>
|
6534 | ### <code>valid-syntax</code>
|
6535 |
|
6536 | **Deprecated** Babylon (the Babel parser) v6.10.0 fixes parsing of the invalid syntax this plugin warned against.
|
6537 |
|
6538 | Checks for simple Flow syntax errors.
|
6539 |
|
6540 | The following patterns are not considered problems:
|
6541 |
|
6542 | ```js
|
6543 | function x(foo: string = "1") {}
|
6544 |
|
6545 | function x(foo: Type = bar()) {}
|
6546 | ```
|
6547 |
|
6548 |
|
6549 |
|