Skip to content

Commit c10b0d9

Browse files
danwanggajus
authored andcommitted
feat: add no-existential-type rule (#316)
1 parent ad4cf82 commit c10b0d9

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

.README/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/d
148148
{"gitdown": "include", "file": "./rules/generic-spacing.md"}
149149
{"gitdown": "include", "file": "./rules/newline-after-flow-annotation"}
150150
{"gitdown": "include", "file": "./rules/no-dupe-keys.md"}
151+
{"gitdown": "include", "file": "./rules/no-existential-type.md"}
151152
{"gitdown": "include", "file": "./rules/no-flow-fix-me-comments.md"}
152153
{"gitdown": "include", "file": "./rules/no-mutable-array.md"}
153154
{"gitdown": "include", "file": "./rules/no-primitive-constructor-types.md"}

.README/rules/no-existential-type.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### `no-existential-type`
2+
3+
Disallows use of the existential type (*). [See more](https://flow.org/en/docs/types/utilities/#toc-existential-type)
4+
5+
```js
6+
{
7+
"rules": {
8+
"flowtype/no-existential-type": 2
9+
}
10+
}
11+
```
12+
13+
14+
<!-- assertions newlineAfterFlowAnnotation -->
15+

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import delimiterDangle from './rules/delimiterDangle';
66
import genericSpacing from './rules/genericSpacing';
77
import newlineAfterFlowAnnotation from './rules/newlineAfterFlowAnnotation';
88
import noDupeKeys from './rules/noDupeKeys';
9+
import noExistentialType from './rules/noExistentialType';
910
import noFlowFixMeComments from './rules/noFlowFixMeComments';
1011
import noMutableArray from './rules/noMutableArray';
1112
import noPrimitiveConstructorTypes from './rules/noPrimitiveConstructorTypes';
@@ -36,6 +37,7 @@ const rules = {
3637
'generic-spacing': genericSpacing,
3738
'newline-after-flow-annotation': newlineAfterFlowAnnotation,
3839
'no-dupe-keys': noDupeKeys,
40+
'no-existential-type': noExistentialType,
3941
'no-flow-fix-me-comments': noFlowFixMeComments,
4042
'no-mutable-array': noMutableArray,
4143
'no-primitive-constructor-types': noPrimitiveConstructorTypes,

src/rules/noExistentialType.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Support both node types for existential type
2+
// https://github.com/babel/babylon/issues/319
3+
const reporter = (context) => {
4+
return (node) => {
5+
context.report({
6+
message: 'Unexpected use of existential type (*).',
7+
node
8+
});
9+
};
10+
};
11+
12+
const create = (context) => {
13+
return {
14+
ExistentialTypeParam: reporter(context),
15+
ExistsTypeAnnotation: reporter(context)
16+
};
17+
};
18+
19+
export default {
20+
create
21+
};
22+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default {
2+
invalid: [
3+
{
4+
code: 'type T = *;',
5+
errors: [{message: 'Unexpected use of existential type (*).'}]
6+
},
7+
{
8+
code: 'type T = U<*, *>;',
9+
errors: [
10+
{column: 12,
11+
message: 'Unexpected use of existential type (*).'},
12+
{column: 15,
13+
message: 'Unexpected use of existential type (*).'}
14+
]
15+
},
16+
{
17+
code: 'const f: (*) => null = () => null;',
18+
errors: [{message: 'Unexpected use of existential type (*).'}]
19+
}
20+
],
21+
valid: [
22+
{
23+
code: 'type T = string | null'
24+
}
25+
]
26+
};
27+

0 commit comments

Comments
 (0)