Skip to content

Commit

Permalink
feat: add no-mixed rule (#382)
Browse files Browse the repository at this point in the history
* Add no-mixed rule

* Update the correct README

* Remove unused var
  • Loading branch information
kangax authored and gajus committed Feb 12, 2019
1 parent cf38564 commit 5c61606
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ npm install eslint babel-eslint eslint-plugin-flowtype --save-dev
2,
"never"
],
"flowtype/no-mixed": 2,
"flowtype/no-primitive-constructor-types": 2,
"flowtype/no-types-missing-file-annotation": 2,
"flowtype/no-weak-types": 2,
Expand Down Expand Up @@ -161,6 +162,7 @@ When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/d
{"gitdown": "include", "file": "./rules/no-dupe-keys.md"}
{"gitdown": "include", "file": "./rules/no-existential-type.md"}
{"gitdown": "include", "file": "./rules/no-flow-fix-me-comments.md"}
{"gitdown": "include", "file": "./rules/no-mixed.md"}
{"gitdown": "include", "file": "./rules/no-mutable-array.md"}
{"gitdown": "include", "file": "./rules/no-primitive-constructor-types.md"}
{"gitdown": "include", "file": "./rules/no-types-missing-file-annotation.md"}
Expand Down
8 changes: 8 additions & 0 deletions .README/rules/no-mixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### `no-mixed`

Warns against "mixed" type annotations.
These types are not strict enough and could often be made more specific.

The following patterns are considered problems:

<!-- assertions noMixed -->
1 change: 1 addition & 0 deletions src/configs/recommended.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
2,
"never"
],
"flowtype/no-mixed": 0,
"flowtype/no-types-missing-file-annotation": 2,
"flowtype/no-weak-types": 0,
"flowtype/require-parameter-type": 0,
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import noPrimitiveConstructorTypes from './rules/noPrimitiveConstructorTypes';
import noTypesMissingFileAnnotation from './rules/noTypesMissingFileAnnotation';
import noUnusedExpressions from './rules/noUnusedExpressions';
import noWeakTypes from './rules/noWeakTypes';
import noMixed from './rules/noMixed';
import objectTypeDelimiter from './rules/objectTypeDelimiter';
import requireCompoundTypeAlias from './rules/requireCompoundTypeAlias';
import requireExactType from './rules/requireExactType';
Expand Down Expand Up @@ -46,6 +47,7 @@ const rules = {
'no-dupe-keys': noDupeKeys,
'no-existential-type': noExistentialType,
'no-flow-fix-me-comments': noFlowFixMeComments,
'no-mixed': noMixed,
'no-mutable-array': noMutableArray,
'no-primitive-constructor-types': noPrimitiveConstructorTypes,
'no-types-missing-file-annotation': noTypesMissingFileAnnotation,
Expand Down Expand Up @@ -93,6 +95,7 @@ export default {
'newline-after-flow-annotation': 0,
'no-dupe-keys': 0,
'no-flow-fix-me-comments': 0,
'no-mixed': 0,
'no-mutable-array': 0,
'no-weak-types': 0,
'object-type-delimiter': 0,
Expand Down
17 changes: 17 additions & 0 deletions src/rules/noMixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const schema = [];

const create = (context) => {
return {
MixedTypeAnnotation (node) {
context.report({
message: 'Unexpected use of mixed type',
node
});
}
};
};

export default {
create,
schema
};
66 changes: 66 additions & 0 deletions tests/rules/assertions/noMixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export default {
invalid: [
{
code: 'function foo(thing): mixed {}',
errors: [{
message: 'Unexpected use of mixed type'
}]
},
{
code: 'function foo(thing): Promise<mixed> {}',
errors: [{
message: 'Unexpected use of mixed type'
}]
},
{
code: 'function foo(thing): Promise<Promise<mixed>> {}',
errors: [{
message: 'Unexpected use of mixed type'
}]
}
],
valid: [
{
code: 'function foo(thing): string {}'
},
{
code: 'function foo(thing): Promise<string> {}'
},
{
code: 'function foo(thing): Promise<Promise<string>> {}'
},
{
code: '(foo?: string) => {}'
},
{
code: '(foo: ?string) => {}'
},
{
code: '(foo: { a: string }) => {}'
},
{
code: '(foo: { a: ?string }) => {}'
},
{
code: '(foo: string[]) => {}'
},
{
code: 'type Foo = string'
},
{
code: 'type Foo = { a: string }'
},
{
code: 'type Foo = { (a: string): string }'
},
{
code: 'function foo(thing: string) {}'
},
{
code: 'var foo: string'
},
{
code: 'class Foo { props: string }'
}
]
};
1 change: 1 addition & 0 deletions tests/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const reportingRules = [
'no-types-missing-file-annotation',
'no-unused-expressions',
'no-weak-types',
'no-mixed',
'object-type-delimiter',
'require-compound-type-alias',
'require-exact-type',
Expand Down

0 comments on commit 5c61606

Please sign in to comment.