Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ import Foo from './foo';
// Message: Expected newline after flow annotation

// Options: ["always-windows"]
// @flow
// @flow
import Foo from './foo';
// Message: Expected newline after flow annotation

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

// Options: ["always-windows"]
// @flow
// @flow

import Foo from './foo';

// Options: ["never"]
Expand Down Expand Up @@ -1568,19 +1568,20 @@ The following patterns are not considered problems:
<a name="eslint-plugin-flowtype-rules-no-weak-types"></a>
### <code>no-weak-types</code>

Warns against weak type annotations *any*, *Object* and *Function*.
Warns against weak type annotations *any*, *mixed*, *Object* and *Function*.
These types can cause flow to silently skip over portions of your code,
which would have otherwise caused type errors.

This rule optionally takes one argument, an object to configure which type warnings to enable. By default, all of the
warnings are enabled. e.g. to disable the `any` warning (allowing it to exist in your code), while continuing to warn
about `Object` and `Function`:
about `mixed`, `Object` and `Function`:

```js
{
"rules": {
"flowtype/no-weak-types": [2, {
"any": false,
"mixed: true,
"Object": true,
"Function": true
}]
Expand Down Expand Up @@ -1610,6 +1611,15 @@ function foo(thing): Promise<any> {}
function foo(thing): Promise<Promise<any>> {}
// Message: Unexpected use of weak type "any"

function foo(thing): mixed {}
// Message: Unexpected use of weak type "mixed"

function foo(thing): Promise<mixed> {}
// Message: Unexpected use of weak type "mixed"

function foo(thing): Promise<Promise<mixed>> {}
// Message: Unexpected use of weak type "mixed"

function foo(thing): Object {}
// Message: Unexpected use of weak type "Object"

Expand Down Expand Up @@ -1732,6 +1742,9 @@ class Foo { props: string }
// Options: [{"Object":false,"any":false}]
type X = any; type Y = Object

// Options: [{"Object":false,"mixed":false}]
type X = mixed; type Y = Object

// Options: [{"Function":false}]
type X = Function

Expand Down Expand Up @@ -3411,7 +3424,7 @@ The following patterns are not considered problems:
{ a: string, b: number }) => {}

// Options: ["always",{"allowLineBreak":true}]
(foo:
(foo:
{ a: string, b: number }) => {}

// Options: ["never"]
Expand Down
7 changes: 7 additions & 0 deletions src/rules/noWeakTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const schema = [
Function: {
type: 'boolean'
},
mixed: {
type: 'boolean'
},
Object: {
type: 'boolean'
}
Expand Down Expand Up @@ -40,6 +43,7 @@ const genericTypeEvaluator = (context, {checkFunction, checkObject}) => {

const create = (context) => {
const checkAny = _.get(context, 'options[0].any', true) === true;
const checkMixed = _.get(context, 'options[0].mixed', true) === true;
const checkFunction = _.get(context, 'options[0].Function', true) === true;
const checkObject = _.get(context, 'options[0].Object', true) === true;

Expand All @@ -48,6 +52,9 @@ const create = (context) => {
if (checkAny) {
checks.AnyTypeAnnotation = reportWeakType(context, 'any');
}
if (checkMixed) {
checks.MixedTypeAnnotation = reportWeakType(context, 'mixed');
}

if (checkFunction || checkObject) {
checks.GenericTypeAnnotation = genericTypeEvaluator(context, {
Expand Down
36 changes: 36 additions & 0 deletions tests/rules/assertions/noWeakTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ export default {
message: 'Unexpected use of weak type "any"'
}]
},
{
code: 'function foo(thing): mixed {}',
errors: [{
message: 'Unexpected use of weak type "mixed"'
}]
},
{
code: 'function foo(thing): Promise<mixed> {}',
errors: [{
message: 'Unexpected use of weak type "mixed"'
}]
},
{
code: 'function foo(thing): Promise<Promise<mixed>> {}',
errors: [{
message: 'Unexpected use of weak type "mixed"'
}]
},
{
code: 'function foo(thing): Object {}',
errors: [{
Expand Down Expand Up @@ -191,6 +209,14 @@ export default {
any: false,
Object: false
}]
},
{
code: 'type X = mixed; type Y = Function; type Z = Object',
errors: [{message: 'Unexpected use of weak type "mixed"'}],
options: [{
Function: false,
Object: false
}]
}
],
misconfigured: [
Expand All @@ -215,6 +241,9 @@ export default {
Function: {
type: 'boolean'
},
mixed: {
type: 'boolean'
},
Object: {
type: 'boolean'
}
Expand Down Expand Up @@ -297,6 +326,13 @@ export default {
Object: false
}]
},
{
code: 'type X = mixed; type Y = Object',
options: [{
mixed: false,
Object: false
}]
},
{
code: 'type X = Function',
options: [{Function: false}]
Expand Down