-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
154 lines (142 loc) · 4.08 KB
/
index.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
const PACKAGE_URL = 'https://www.npmjs.com/package/eslint-plugin-require-explicit-generics';
let hasWarned = false;
function warnOnce() {
if (!hasWarned) {
console.warn('require-explicit-generics was not passed any function or constructor names to check');
console.warn('Pass an array of function and constructor names or a map of names to expected counts in your .eslintrc.json');
console.warn('For more details visit: ' + PACKAGE_URL)
hasWarned = true;
}
}
function createExpectedCountMap(countMapOrNameList) {
const expectedCountMap = new Map();
if (Array.isArray(countMapOrNameList)) {
countMapOrNameList.forEach(functionName =>
expectedCountMap.set(functionName, 1)
);
} else {
Object.entries(countMapOrNameList).forEach(([key, value]) =>
expectedCountMap.set(key, value)
);
}
return expectedCountMap;
}
function getParamsLength(item) {
if (!item) return;
return item.length || (item.params && item.params.length);
}
const ASCII_A_OFFSET = 65;
function getLetterOfAlphabet(index) {
return String.fromCharCode(ASCII_A_OFFSET + index % 26);
}
function getExampleGenerics(count) {
if (count === 1) {
return "SomeType";
}
const values = [];
for (let index = 0; index < count; index += 1) {
values.push("Type" + getLetterOfAlphabet(index));
}
return values.join(", ");
}
function getCalleeNames(node) {
if (node.callee.name) {
return [node.callee.name];
}
if (("object" in node.callee) && ("property" in node.callee)) {
const objectName = node.callee.object.name || "";
const propertyName = node.callee.property.name || "";
return [
objectName + "." + propertyName,
"*." + propertyName,
propertyName
];
}
return [];
}
function assertThatNodeHasExpectedGenerics({ context, expectedCountMap, node, nodeType }) {
const possibleNames = getCalleeNames(node);
const matchingRuleName = possibleNames.find(possibleName => expectedCountMap.has(possibleName));
if (!matchingRuleName) return;
const expectedCount = expectedCountMap.get(matchingRuleName);
if (!expectedCount) return;
const actualCount = getParamsLength(node.typeParameters) || getParamsLength(node.typeArguments) || 0;
const mostSpecificName = possibleNames[0];
const reportData = {
nodeType,
name: mostSpecificName,
generics: getExampleGenerics(expectedCount),
expectedCount,
actualCount,
};
if (actualCount === 0) {
context.report({
node: node.callee,
message:
"{{nodeType}} '{{name}}' must be called with explicit generics. " +
"Replace with '{{name}}<{{generics}}>(...)' to fix this.",
data: reportData
});
} else if (actualCount < expectedCount) {
context.report({
node: node.callee,
message:
"{{nodeType}} '{{name}}' called with too few explicit generics. " +
"Received {{actualCount}}, expected {{expectedCount}}. " +
"Replace with '{{name}}<{{generics}}>(...)' to fix this.",
data: reportData
});
}
}
const rules = {
"require-explicit-generics": {
meta: {
type: "problem",
schema: [{
oneOf: [
{
type: "array",
items: { type: "string" },
uniqueItems: true,
minItems: 1
},
{
type: "object",
additionalProperties: {
type: "integer",
minimum: 1
}
}
]
}]
},
create: function (context) {
if (context.options.length === 0) {
warnOnce();
return {};
}
const expectedCountMap = createExpectedCountMap(context.options[0]);
return {
NewExpression: (node) => {
assertThatNodeHasExpectedGenerics({
context,
node,
expectedCountMap,
nodeType: "Constructor"
});
},
CallExpression(node) {
assertThatNodeHasExpectedGenerics({
context,
node,
expectedCountMap,
nodeType: "Function"
});
}
};
},
},
};
module.exports = {
rules,
};