/
checkParamNames.js
272 lines (233 loc) · 7.35 KB
/
checkParamNames.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import iterateJsdoc from '../iterateJsdoc';
const validateParameterNames = (
targetTagName : string,
allowExtraTrailingParamDocs: boolean,
checkDestructured : boolean,
checkRestProperty : boolean,
checkTypesRegex : RegExp,
enableFixer: boolean,
functionParameterNames : Array<string>, jsdoc, jsdocNode, utils, report,
) => {
const paramTags = Object.entries(jsdoc.tags).filter(([, tag]) => {
return tag.tag === targetTagName;
});
const paramTagsNonNested = paramTags.filter(([, tag]) => {
return !tag.name.includes('.');
});
let dotted = 0;
return paramTags.some(([, tag], index) => {
let tagsIndex;
const dupeTagInfo = paramTags.find(([tgsIndex, tg], idx) => {
tagsIndex = tgsIndex;
return tg.name === tag.name && idx !== index;
});
if (dupeTagInfo) {
utils.reportJSDoc(`Duplicate @${targetTagName} "${tag.name}"`, dupeTagInfo[1], enableFixer ? () => {
jsdoc.tags.splice(tagsIndex, 1);
} : null);
return true;
}
if (tag.name.includes('.')) {
dotted++;
return false;
}
const functionParameterName = functionParameterNames[index - dotted];
if (!functionParameterName) {
if (allowExtraTrailingParamDocs) {
return false;
}
report(
`@${targetTagName} "${tag.name}" does not match an existing function parameter.`,
null,
tag,
);
return true;
}
if (Array.isArray(functionParameterName)) {
if (!checkDestructured) {
return false;
}
if (tag.type && tag.type.search(checkTypesRegex) === -1) {
return false;
}
const [parameterName, {
names: properties, hasPropertyRest, rests, annotationParamName,
}] = functionParameterName;
if (annotationParamName !== undefined) {
const name = tag.name.trim();
if (name !== annotationParamName) {
report(`@${targetTagName} "${name}" does not match parameter name "${annotationParamName}"`, null, tag);
}
}
const tagName = parameterName === undefined ? tag.name.trim() : parameterName;
const expectedNames = properties.map((name) => {
return `${tagName}.${name}`;
});
const actualNames = paramTags.map(([, paramTag]) => {
return paramTag.name.trim();
});
const missingProperties = [];
expectedNames.forEach((name, idx) => {
if (!actualNames.some(utils.comparePaths(name))) {
if (!checkRestProperty && rests[idx]) {
return;
}
missingProperties.push(name);
}
});
const extraProperties = [];
if (!hasPropertyRest || checkRestProperty) {
actualNames.forEach((name, idx) => {
const match = name.startsWith(tag.name.trim() + '.');
if (match && !expectedNames.some(utils.comparePaths(name)) && !utils.comparePaths(name)(tag.name)) {
extraProperties.push([name, paramTags[idx][1]]);
}
});
}
const hasMissing = missingProperties.length;
if (hasMissing) {
missingProperties.forEach((missingProperty) => {
report(`Missing @${targetTagName} "${missingProperty}"`, null, tag);
});
}
if (extraProperties.length) {
extraProperties.forEach(([extraProperty, tg]) => {
report(`@${targetTagName} "${extraProperty}" does not exist on ${tag.name}`, null, tg);
});
return true;
}
return hasMissing;
}
let funcParamName;
if (typeof functionParameterName === 'object') {
const {name} = functionParameterName;
funcParamName = name;
} else {
funcParamName = functionParameterName;
}
if (funcParamName !== tag.name.trim()) {
// Todo: Improve for array or object child items
const actualNames = paramTagsNonNested.map(([, {name}]) => {
return name.trim();
});
const expectedNames = functionParameterNames.map((item, idx) => {
if (item?.[1]?.names) {
return actualNames[idx];
}
return item;
}).join(', ');
report(
`Expected @${targetTagName} names to be "${expectedNames}". Got "${actualNames.join(', ')}".`,
null,
tag,
);
return true;
}
return false;
});
};
const validateParameterNamesDeep = (
targetTagName : string, allowExtraTrailingParamDocs: boolean,
jsdocParameterNames : Array<string>, jsdoc, report : Function,
) => {
let lastRealParameter;
return jsdocParameterNames.some(({name: jsdocParameterName, idx}) => {
const isPropertyPath = jsdocParameterName.includes('.');
if (isPropertyPath) {
if (!lastRealParameter) {
report(`@${targetTagName} path declaration ("${jsdocParameterName}") appears before any real parameter.`, null, jsdoc.tags[idx]);
return true;
}
let pathRootNodeName = jsdocParameterName.slice(0, jsdocParameterName.indexOf('.'));
if (pathRootNodeName.endsWith('[]')) {
pathRootNodeName = pathRootNodeName.slice(0, -2);
}
if (pathRootNodeName !== lastRealParameter) {
report(
`@${targetTagName} path declaration ("${jsdocParameterName}") root node name ("${pathRootNodeName}") ` +
`does not match previous real parameter name ("${lastRealParameter}").`,
null,
jsdoc.tags[idx],
);
return true;
}
} else {
lastRealParameter = jsdocParameterName;
}
return false;
});
};
export default iterateJsdoc(({
context,
jsdoc,
jsdocNode,
report,
utils,
}) => {
const {
allowExtraTrailingParamDocs,
checkDestructured = true,
checkRestProperty = false,
checkTypesPattern = '/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/',
enableFixer = false,
} = context.options[0] || {};
const lastSlashPos = checkTypesPattern.lastIndexOf('/');
const checkTypesRegex = lastSlashPos === -1 ?
new RegExp(checkTypesPattern) :
new RegExp(checkTypesPattern.slice(1, lastSlashPos), checkTypesPattern.slice(lastSlashPos + 1));
const jsdocParameterNamesDeep = utils.getJsdocTagsDeep('param');
if (!jsdocParameterNamesDeep.length) {
return;
}
const functionParameterNames = utils.getFunctionParameterNames();
const targetTagName = utils.getPreferredTagName({tagName: 'param'});
const isError = validateParameterNames(
targetTagName,
allowExtraTrailingParamDocs,
checkDestructured,
checkRestProperty,
checkTypesRegex,
enableFixer,
functionParameterNames,
jsdoc, jsdocNode, utils, report,
);
if (isError || !checkDestructured) {
return;
}
validateParameterNamesDeep(
targetTagName, allowExtraTrailingParamDocs,
jsdocParameterNamesDeep,
jsdoc, report,
);
}, {
meta: {
docs: {
description: 'Ensures that parameter names in JSDoc match those in the function declaration.',
},
fixable: 'code',
schema: [
{
additionalProperties: false,
properties: {
allowExtraTrailingParamDocs: {
type: 'boolean',
},
checkDestructured: {
type: 'boolean',
},
checkRestProperty: {
type: 'boolean',
},
checkTypesPattern: {
type: 'string',
},
enableFixer: {
type: 'boolean',
},
},
type: 'object',
},
],
type: 'suggestion',
},
});