Skip to content

Commit

Permalink
feat: add no-existential-type rule (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
danwang authored and gajus committed Feb 13, 2018
1 parent ad4cf82 commit c10b0d9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/d
{"gitdown": "include", "file": "./rules/generic-spacing.md"}
{"gitdown": "include", "file": "./rules/newline-after-flow-annotation"}
{"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-mutable-array.md"}
{"gitdown": "include", "file": "./rules/no-primitive-constructor-types.md"}
Expand Down
15 changes: 15 additions & 0 deletions .README/rules/no-existential-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### `no-existential-type`

Disallows use of the existential type (*). [See more](https://flow.org/en/docs/types/utilities/#toc-existential-type)

```js
{
"rules": {
"flowtype/no-existential-type": 2
}
}
```


<!-- assertions newlineAfterFlowAnnotation -->

2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import delimiterDangle from './rules/delimiterDangle';
import genericSpacing from './rules/genericSpacing';
import newlineAfterFlowAnnotation from './rules/newlineAfterFlowAnnotation';
import noDupeKeys from './rules/noDupeKeys';
import noExistentialType from './rules/noExistentialType';
import noFlowFixMeComments from './rules/noFlowFixMeComments';
import noMutableArray from './rules/noMutableArray';
import noPrimitiveConstructorTypes from './rules/noPrimitiveConstructorTypes';
Expand Down Expand Up @@ -36,6 +37,7 @@ const rules = {
'generic-spacing': genericSpacing,
'newline-after-flow-annotation': newlineAfterFlowAnnotation,
'no-dupe-keys': noDupeKeys,
'no-existential-type': noExistentialType,
'no-flow-fix-me-comments': noFlowFixMeComments,
'no-mutable-array': noMutableArray,
'no-primitive-constructor-types': noPrimitiveConstructorTypes,
Expand Down
22 changes: 22 additions & 0 deletions src/rules/noExistentialType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Support both node types for existential type
// https://github.com/babel/babylon/issues/319
const reporter = (context) => {
return (node) => {
context.report({
message: 'Unexpected use of existential type (*).',
node
});
};
};

const create = (context) => {
return {
ExistentialTypeParam: reporter(context),
ExistsTypeAnnotation: reporter(context)
};
};

export default {
create
};

27 changes: 27 additions & 0 deletions tests/rules/assertions/noExistentialType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default {
invalid: [
{
code: 'type T = *;',
errors: [{message: 'Unexpected use of existential type (*).'}]
},
{
code: 'type T = U<*, *>;',
errors: [
{column: 12,
message: 'Unexpected use of existential type (*).'},
{column: 15,
message: 'Unexpected use of existential type (*).'}
]
},
{
code: 'const f: (*) => null = () => null;',
errors: [{message: 'Unexpected use of existential type (*).'}]
}
],
valid: [
{
code: 'type T = string | null'
}
]
};

0 comments on commit c10b0d9

Please sign in to comment.