Skip to content

Commit 5e2bbe9

Browse files
kangaxgajus
authored andcommitted
feat: add mixed to no-weak-types (#362)
BREAKING CHANGE: `mixed` is now treated as a weak type by default.
1 parent 68ed515 commit 5e2bbe9

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ import Foo from './foo';
11311131
// Message: Expected newline after flow annotation
11321132

11331133
// Options: ["always-windows"]
1134-
// @flow
1134+
// @flow
11351135
import Foo from './foo';
11361136
// Message: Expected newline after flow annotation
11371137

@@ -1151,8 +1151,8 @@ The following patterns are not considered problems:
11511151
import Foo from './foo';
11521152

11531153
// Options: ["always-windows"]
1154-
// @flow
1155-
1154+
// @flow
1155+
11561156
import Foo from './foo';
11571157

11581158
// Options: ["never"]
@@ -1568,19 +1568,20 @@ The following patterns are not considered problems:
15681568
<a name="eslint-plugin-flowtype-rules-no-weak-types"></a>
15691569
### <code>no-weak-types</code>
15701570
1571-
Warns against weak type annotations *any*, *Object* and *Function*.
1571+
Warns against weak type annotations *any*, *mixed*, *Object* and *Function*.
15721572
These types can cause flow to silently skip over portions of your code,
15731573
which would have otherwise caused type errors.
15741574
15751575
This rule optionally takes one argument, an object to configure which type warnings to enable. By default, all of the
15761576
warnings are enabled. e.g. to disable the `any` warning (allowing it to exist in your code), while continuing to warn
1577-
about `Object` and `Function`:
1577+
about `mixed`, `Object` and `Function`:
15781578
15791579
```js
15801580
{
15811581
"rules": {
15821582
"flowtype/no-weak-types": [2, {
15831583
"any": false,
1584+
"mixed: true,
15841585
"Object": true,
15851586
"Function": true
15861587
}]
@@ -1610,6 +1611,15 @@ function foo(thing): Promise<any> {}
16101611
function foo(thing): Promise<Promise<any>> {}
16111612
// Message: Unexpected use of weak type "any"
16121613
1614+
function foo(thing): mixed {}
1615+
// Message: Unexpected use of weak type "mixed"
1616+
1617+
function foo(thing): Promise<mixed> {}
1618+
// Message: Unexpected use of weak type "mixed"
1619+
1620+
function foo(thing): Promise<Promise<mixed>> {}
1621+
// Message: Unexpected use of weak type "mixed"
1622+
16131623
function foo(thing): Object {}
16141624
// Message: Unexpected use of weak type "Object"
16151625
@@ -1732,6 +1742,9 @@ class Foo { props: string }
17321742
// Options: [{"Object":false,"any":false}]
17331743
type X = any; type Y = Object
17341744
1745+
// Options: [{"Object":false,"mixed":false}]
1746+
type X = mixed; type Y = Object
1747+
17351748
// Options: [{"Function":false}]
17361749
type X = Function
17371750
@@ -3411,7 +3424,7 @@ The following patterns are not considered problems:
34113424
{ a: string, b: number }) => {}
34123425
34133426
// Options: ["always",{"allowLineBreak":true}]
3414-
(foo:
3427+
(foo:
34153428
{ a: string, b: number }) => {}
34163429
34173430
// Options: ["never"]

src/rules/noWeakTypes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const schema = [
1010
Function: {
1111
type: 'boolean'
1212
},
13+
mixed: {
14+
type: 'boolean'
15+
},
1316
Object: {
1417
type: 'boolean'
1518
}
@@ -40,6 +43,7 @@ const genericTypeEvaluator = (context, {checkFunction, checkObject}) => {
4043

4144
const create = (context) => {
4245
const checkAny = _.get(context, 'options[0].any', true) === true;
46+
const checkMixed = _.get(context, 'options[0].mixed', true) === true;
4347
const checkFunction = _.get(context, 'options[0].Function', true) === true;
4448
const checkObject = _.get(context, 'options[0].Object', true) === true;
4549

@@ -48,6 +52,9 @@ const create = (context) => {
4852
if (checkAny) {
4953
checks.AnyTypeAnnotation = reportWeakType(context, 'any');
5054
}
55+
if (checkMixed) {
56+
checks.MixedTypeAnnotation = reportWeakType(context, 'mixed');
57+
}
5158

5259
if (checkFunction || checkObject) {
5360
checks.GenericTypeAnnotation = genericTypeEvaluator(context, {

tests/rules/assertions/noWeakTypes.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ export default {
1818
message: 'Unexpected use of weak type "any"'
1919
}]
2020
},
21+
{
22+
code: 'function foo(thing): mixed {}',
23+
errors: [{
24+
message: 'Unexpected use of weak type "mixed"'
25+
}]
26+
},
27+
{
28+
code: 'function foo(thing): Promise<mixed> {}',
29+
errors: [{
30+
message: 'Unexpected use of weak type "mixed"'
31+
}]
32+
},
33+
{
34+
code: 'function foo(thing): Promise<Promise<mixed>> {}',
35+
errors: [{
36+
message: 'Unexpected use of weak type "mixed"'
37+
}]
38+
},
2139
{
2240
code: 'function foo(thing): Object {}',
2341
errors: [{
@@ -191,6 +209,14 @@ export default {
191209
any: false,
192210
Object: false
193211
}]
212+
},
213+
{
214+
code: 'type X = mixed; type Y = Function; type Z = Object',
215+
errors: [{message: 'Unexpected use of weak type "mixed"'}],
216+
options: [{
217+
Function: false,
218+
Object: false
219+
}]
194220
}
195221
],
196222
misconfigured: [
@@ -215,6 +241,9 @@ export default {
215241
Function: {
216242
type: 'boolean'
217243
},
244+
mixed: {
245+
type: 'boolean'
246+
},
218247
Object: {
219248
type: 'boolean'
220249
}
@@ -297,6 +326,13 @@ export default {
297326
Object: false
298327
}]
299328
},
329+
{
330+
code: 'type X = mixed; type Y = Object',
331+
options: [{
332+
mixed: false,
333+
Object: false
334+
}]
335+
},
300336
{
301337
code: 'type X = Function',
302338
options: [{Function: false}]

0 commit comments

Comments
 (0)