forked from gajus/eslint-plugin-flowtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noWeakTypes.js
65 lines (55 loc) · 1.3 KB
/
noWeakTypes.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
import _ from 'lodash';
const schema = [
{
additionalProperties: false,
properties: {
any: {
type: 'boolean'
},
Function: {
type: 'boolean'
},
Object: {
type: 'boolean'
}
},
type: 'object'
}
];
const reportWeakType = (context, weakType) => {
return (node) => {
context.report({
data: {weakType},
message: 'Unexpected use of weak type "{{weakType}}"',
node
});
};
};
const genericTypeEvaluator = (context, {checkFunction, checkObject}) => {
return (node) => {
const name = _.get(node, 'id.name');
if (checkFunction && name === 'Function' || checkObject && name === 'Object') {
reportWeakType(context, name)(node);
}
};
};
const create = (context) => {
const checkAny = _.get(context, 'options[0].any', true) === true;
const checkFunction = _.get(context, 'options[0].Function', true) === true;
const checkObject = _.get(context, 'options[0].Object', true) === true;
const checks = {};
if (checkAny) {
checks.AnyTypeAnnotation = reportWeakType(context, 'any');
}
if (checkFunction || checkObject) {
checks.GenericTypeAnnotation = genericTypeEvaluator(context, {
checkFunction,
checkObject
});
}
return checks;
};
export default {
create,
schema
};