-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
flow-use-readonly-spread.js
46 lines (44 loc) · 1.42 KB
/
flow-use-readonly-spread.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
// @flow
/*::
import type { EslintRule } from '@adeira/flow-types-eslint';
*/
module.exports = ({
meta: {
messages: {
readonlySpread:
'Flow type with spread property and all readonly properties should be ' +
"wrapped in '$ReadOnly<…>' to prevent accidental loss of readonly-ness.",
},
},
create: function (context) {
return {
TypeAlias(node) {
if (node.right.type === 'GenericTypeAnnotation' && node.right.id.name === '$ReadOnly') {
// it's already $ReadOnly<…>, nothing to do
} else if (node.right.type === 'ObjectTypeAnnotation') {
// let's iterate all props and if everything is readonly then throw
let shouldThrow = false;
let hasSpread = false;
for (const property of node.right.properties) {
if (property.type === 'ObjectTypeProperty') {
if (property.variance && property.variance.kind === 'plus') {
shouldThrow = true;
} else {
shouldThrow = false;
break;
}
} else if (property.type === 'ObjectTypeSpreadProperty') {
hasSpread = true;
}
}
if (hasSpread === true && shouldThrow === true) {
context.report({
node: node.right,
messageId: 'readonlySpread',
});
}
}
},
};
},
} /*: EslintRule */);