-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
requireReadonlyReactProps.js
186 lines (154 loc) · 5.17 KB
/
requireReadonlyReactProps.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import _ from 'lodash';
const schema = [
{
additionalProperties: false,
properties: {
useImplicitExactTypes: {
type: 'boolean',
},
},
type: 'object',
},
];
const reComponentName = /^(Pure)?Component$/u;
const reReadOnly = /^\$(ReadOnly|FlowFixMe)$/u;
const isReactComponent = (node) => {
if (!node.superClass) {
return false;
}
return (
// class Foo extends Component { }
// class Foo extends PureComponent { }
node.superClass.type === 'Identifier' && reComponentName.test(node.superClass.name) ||
// class Foo extends React.Component { }
// class Foo extends React.PureComponent { }
node.superClass.type === 'MemberExpression' &&
(node.superClass.object.name === 'React' && reComponentName.test(node.superClass.property.name))
);
};
// type Props = {| +foo: string |}
const isReadOnlyObjectType = (node, {useImplicitExactTypes}) => {
if (!node || node.type !== 'ObjectTypeAnnotation') {
return false;
}
if (node.properties.length === 0) {
// we consider `{}` to be ReadOnly since it's exact AND has no props (when `implicitExactTypes=true`)
// we consider `{||}` to be ReadOnly since it's exact AND has no props (when `implicitExactTypes=false`)
if (useImplicitExactTypes === true && node.exact === false) {
return true;
}
if (node.exact === true) {
return true;
}
}
// { +foo: ..., +bar: ..., ... }
return node.properties.length > 0 &&
node.properties.every((prop) => {
return prop.variance && prop.variance.kind === 'plus';
});
};
// type Props = {| +foo: string |} | {| +bar: number |}
const isReadOnlyObjectUnionType = (node, options) => {
if (!node || node.type !== 'UnionTypeAnnotation') {
return false;
}
return node.types.every((type) => {
return isReadOnlyObjectType(type, options);
});
};
const isReadOnlyType = (node, options) => {
return node.right.id && reReadOnly.test(node.right.id.name) ||
isReadOnlyObjectType(node.right, options) ||
isReadOnlyObjectUnionType(node.right, options);
};
const create = (context) => {
const useImplicitExactTypes = _.get(context, ['options', 0, 'useImplicitExactTypes'], false);
const options = {useImplicitExactTypes};
const readOnlyTypes = [];
const foundTypes = [];
const reportedFunctionalComponents = [];
const isReadOnlyClassProp = (node) => {
const id = node.superTypeParameters && node.superTypeParameters.params[0].id;
return id && !reReadOnly.test(id.name) && !readOnlyTypes.includes(id.name) && foundTypes.includes(id.name);
};
for (const node of context.getSourceCode().ast.body) {
let idName;
let typeNode;
// type Props = $ReadOnly<{}>
if (node.type === 'TypeAlias') {
idName = node.id.name;
typeNode = node;
// export type Props = $ReadOnly<{}>
} else if (node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.type === 'TypeAlias') {
idName = node.declaration.id.name;
typeNode = node.declaration;
}
if (idName) {
foundTypes.push(idName);
if (isReadOnlyType(typeNode, options)) {
readOnlyTypes.push(idName);
}
}
}
return {
// class components
ClassDeclaration (node) {
if (isReactComponent(node) && isReadOnlyClassProp(node)) {
context.report({
message: node.superTypeParameters.params[0].id.name + ' must be $ReadOnly',
node,
});
} else if (node.superTypeParameters &&
node.superTypeParameters.params[0].type === 'ObjectTypeAnnotation' &&
!isReadOnlyObjectType(node.superTypeParameters.params[0], options)) {
context.report({
message: node.id.name + ' class props must be $ReadOnly',
node,
});
}
},
// functional components
JSXElement (node) {
let currentNode = node;
let identifier;
let typeAnnotation;
while (currentNode && currentNode.type !== 'FunctionDeclaration') {
currentNode = currentNode.parent;
}
// functional components can only have 1 param
if (!currentNode || currentNode.params.length !== 1) {
return;
}
if (currentNode.params[0].type === 'Identifier' &&
(typeAnnotation = currentNode.params[0].typeAnnotation)) {
if ((identifier = typeAnnotation.typeAnnotation.id) &&
foundTypes.includes(identifier.name) &&
!readOnlyTypes.includes(identifier.name) &&
!reReadOnly.test(identifier.name)) {
if (reportedFunctionalComponents.includes(identifier)) {
return;
}
context.report({
message: identifier.name + ' must be $ReadOnly',
node: identifier,
});
reportedFunctionalComponents.push(identifier);
return;
}
if (typeAnnotation.typeAnnotation.type === 'ObjectTypeAnnotation' &&
!isReadOnlyObjectType(typeAnnotation.typeAnnotation, options)) {
context.report({
message: currentNode.id.name + ' component props must be $ReadOnly',
node,
});
}
}
},
};
};
export default {
create,
schema,
};