/
requireCompoundTypeAlias.js
71 lines (65 loc) · 1.51 KB
/
requireCompoundTypeAlias.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const schema = [
{
enum: ['always', 'never'],
type: 'string',
},
{
additionalProperties: false,
properties: {
allowNull: {
type: 'boolean',
},
},
type: 'object',
},
];
const create = (context) => {
const always = (context.options[0] || 'always') === 'always';
const allowNull = !(context.options[1] && context.options[1].allowNull === false);
if (always) {
return {
IntersectionTypeAnnotation (node) {
if (
allowNull &&
node.types.length === 2 &&
(
node.types[0].type === 'NullLiteralTypeAnnotation' ||
node.types[1].type === 'NullLiteralTypeAnnotation'
)
) {
return;
}
if (node.parent.type !== 'TypeAlias') {
context.report({
message: 'All intersection types must be declared with named type alias.',
node,
});
}
},
UnionTypeAnnotation (node) {
if (
allowNull &&
node.types.length === 2 &&
(
node.types[0].type === 'NullLiteralTypeAnnotation' ||
node.types[1].type === 'NullLiteralTypeAnnotation'
)
) {
return;
}
if (node.parent.type !== 'TypeAlias') {
context.report({
message: 'All union types must be declared with named type alias.',
node,
});
}
},
};
} else {
return {};
}
};
export default {
create,
schema,
};