Skip to content

Commit 36c9166

Browse files
committed
refactor: TS
1 parent 1350ed6 commit 36c9166

File tree

4 files changed

+169
-71
lines changed

4 files changed

+169
-71
lines changed

src/iterateJsdoc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ import {
323323
/**
324324
* @callback ComparePaths
325325
* @param {string} name
326-
* @returns {(otherPathName: string) => void}
326+
* @returns {(otherPathName: string) => boolean}
327327
*/
328328

329329
/**

src/jsdocUtils.js

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,42 @@ const setTagStructure = (mode) => {
3737
tagStructure = getDefaultTagStructureForMode(mode);
3838
};
3939

40-
/**
41-
* @typedef {{
42-
* hasPropertyRest: boolean,
43-
* hasRestElement: boolean,
44-
* names: string[],
45-
* rests: boolean[],
46-
* }} FlattendRootInfo
47-
*/
48-
4940
/**
5041
* @typedef {undefined|string|{
42+
* name: Integer,
43+
* restElement: boolean
44+
* }|{
5145
* isRestProperty: boolean|undefined,
5246
* name: string,
53-
* restElement: true
54-
* }|[undefined|string, FlattendRootInfo & {
55-
* annotationParamName?: string
47+
* restElement: boolean
5648
* }|{
57-
* name: Integer,
49+
* name: string,
5850
* restElement: boolean
59-
* }[]]} ParamNameInfo
51+
* }} ParamCommon
52+
*/
53+
/**
54+
* @typedef {ParamCommon|[string|undefined, (FlattendRootInfo & {
55+
* annotationParamName?: string,
56+
* })]|NestedParamInfo} ParamNameInfo
6057
*/
6158

6259
/**
63-
* @typedef {undefined|string|{
64-
* isRestProperty: boolean,
65-
* restElement: boolean,
66-
* name: string
67-
* }|[string, {
60+
* @typedef {{
6861
* hasPropertyRest: boolean,
6962
* hasRestElement: boolean,
7063
* names: string[],
7164
* rests: boolean[],
72-
* }]|[string, string[]]} ParamInfo
65+
* }} FlattendRootInfo
66+
*/
67+
/**
68+
* @typedef {[string, (string[]|ParamInfo[])]} NestedParamInfo
69+
*/
70+
/**
71+
* @typedef {ParamCommon|
72+
* [string|undefined, (FlattendRootInfo & {
73+
* annotationParamName?: string
74+
* })]|
75+
* NestedParamInfo} ParamInfo
7376
*/
7477

7578
/**
@@ -123,28 +126,28 @@ const flattenRoots = (params, root = '') => {
123126
hasPropertyRest = true;
124127
}
125128

126-
const inner = [
129+
const inner = /** @type {string[]} */ ([
127130
root ? `${root}.${cur[0]}` : cur[0],
128131
...flattened.names,
129-
].filter(Boolean);
132+
].filter(Boolean));
130133
rests.push(false, ...flattened.rests);
131134

132135
return acc.concat(inner);
133136
}
134137

135138
if (typeof cur === 'object') {
136-
if (cur.isRestProperty) {
139+
if ('isRestProperty' in cur && cur.isRestProperty) {
137140
hasPropertyRest = true;
138141
rests.push(true);
139142
} else {
140143
rests.push(false);
141144
}
142145

143-
if (cur.restElement) {
146+
if ('restElement' in cur && cur.restElement) {
144147
hasRestElement = true;
145148
}
146149

147-
acc.push(root ? `${root}.${cur.name}` : cur.name);
150+
acc.push(root ? `${root}.${String(cur.name)}` : String(cur.name));
148151
} else if (typeof cur !== 'undefined') {
149152
rests.push(false);
150153
acc.push(root ? `${root}.${cur}` : cur);
@@ -219,10 +222,11 @@ const getFunctionParameterNames = (
219222
* import('@typescript-eslint/types').TSESTree.RestElement|
220223
* import('@typescript-eslint/types').TSESTree.Identifier|
221224
* import('@typescript-eslint/types').TSESTree.ObjectPattern|
222-
* import('@typescript-eslint/types').TSESTree.BindingName
225+
* import('@typescript-eslint/types').TSESTree.BindingName|
226+
* import('@typescript-eslint/types').TSESTree.Parameter
223227
* } param
224228
* @param {boolean} [isProperty]
225-
* @returns {ParamNameInfo}
229+
* @returns {ParamNameInfo|[string, ParamNameInfo[]]}
226230
*/
227231
const getParamName = (param, isProperty) => {
228232
/* eslint-enable complexity -- Temporary */
@@ -305,9 +309,10 @@ const getFunctionParameterNames = (
305309
if (param.type === 'Property') {
306310
// eslint-disable-next-line default-case
307311
switch (param.value.type) {
308-
case 'ArrayPattern':
312+
case 'ArrayPattern': {
309313
return [
310-
param.key.name,
314+
/** @type {import('estree').Identifier} */
315+
(param.key).name,
311316
/** @type {import('estree').ArrayPattern} */ (
312317
param.value
313318
).elements.map((prop, idx) => {
@@ -317,42 +322,57 @@ const getFunctionParameterNames = (
317322
};
318323
}),
319324
];
320-
case 'ObjectPattern':
325+
}
326+
327+
case 'ObjectPattern': {
321328
return [
322-
param.key.name,
329+
/** @type {import('estree').Identifier} */ (param.key).name,
323330
/** @type {import('estree').ObjectPattern} */ (
324331
param.value
325332
).properties.map((prop) => {
326-
return getParamName(prop, isProperty);
333+
return /** @type {string|[string, string[]]} */ (getParamName(prop, isProperty));
327334
}),
328335
];
336+
}
337+
329338
case 'AssignmentPattern': {
330339
// eslint-disable-next-line default-case
331340
switch (param.value.left.type) {
332341
case 'Identifier':
333342
// Default parameter
334343
if (checkDefaultObjects && param.value.right.type === 'ObjectExpression') {
335344
return [
336-
param.key.name, /** @type {import('estree').AssignmentPattern} */ (
345+
/** @type {import('estree').Identifier} */ (
346+
param.key
347+
).name,
348+
/** @type {import('estree').AssignmentPattern} */ (
337349
param.value
338350
).right.properties.map((prop) => {
339-
return getParamName(prop, isProperty);
351+
return /** @type {string} */ (getParamName(
352+
/** @type {import('estree').Property} */
353+
(prop),
354+
isProperty,
355+
));
340356
}),
341357
];
342358
}
343359

344360
break;
345361
case 'ObjectPattern':
346362
return [
347-
param.key.name, /** @type {import('estree').ObjectPattern} */ (
363+
/** @type {import('estree').Identifier} */
364+
(param.key).name,
365+
/** @type {import('estree').ObjectPattern} */ (
348366
param.value.left
349367
).properties.map((prop) => {
350368
return getParamName(prop, isProperty);
351369
}),
352370
];
353371
case 'ArrayPattern':
354372
return [
355-
param.key.name, /** @type {import('estree').ArrayPattern} */ (
373+
/** @type {import('estree').Identifier} */
374+
(param.key).name,
375+
/** @type {import('estree').ArrayPattern} */ (
356376
param.value.left
357377
).elements.map((prop, idx) => {
358378
return {
@@ -371,9 +391,9 @@ const getFunctionParameterNames = (
371391

372392
// The key of an object could also be a string or number
373393
case 'Literal':
374-
return param.key.raw ||
394+
return /** @type {string} */ (param.key.raw ||
375395
// istanbul ignore next -- `raw` may not be present in all parsers
376-
param.key.value;
396+
param.key.value);
377397

378398
// case 'MemberExpression':
379399
default:
@@ -385,11 +405,18 @@ const getFunctionParameterNames = (
385405
}
386406
}
387407

388-
if (param.type === 'ArrayPattern' || param.left?.type === 'ArrayPattern') {
408+
if (
409+
param.type === 'ArrayPattern' ||
410+
/** @type {import('estree').AssignmentPattern} */ (
411+
param
412+
).left?.type === 'ArrayPattern'
413+
) {
389414
const elements = /** @type {import('estree').ArrayPattern} */ (
390415
param
391416
).elements || /** @type {import('estree').ArrayPattern} */ (
392-
param.left
417+
/** @type {import('estree').AssignmentPattern} */ (
418+
param
419+
).left
393420
)?.elements;
394421
const roots = elements.map((prop, idx) => {
395422
return {
@@ -408,7 +435,10 @@ const getFunctionParameterNames = (
408435
].includes(param.type)) {
409436
return {
410437
isRestProperty: isProperty,
411-
name: param.argument.name,
438+
name: /** @type {import('@typescript-eslint/types').TSESTree.Identifier} */ (
439+
/** @type {import('@typescript-eslint/types').TSESTree.RestElement} */ (
440+
param
441+
).argument).name,
412442
restElement: true,
413443
};
414444
}
@@ -431,7 +461,11 @@ const getFunctionParameterNames = (
431461
return [];
432462
}
433463

434-
return (functionNode.params || functionNode.value?.params || []).map((param) => {
464+
return (/** @type {import('@typescript-eslint/types').TSESTree.FunctionDeclaration} */ (
465+
functionNode
466+
).params || /** @type {import('@typescript-eslint/types').TSESTree.MethodDefinition} */ (
467+
functionNode
468+
).value?.params || []).map((param) => {
435469
return getParamName(param);
436470
});
437471
};
@@ -1570,7 +1604,7 @@ const dropPathSegmentQuotes = (str) => {
15701604

15711605
/**
15721606
* @param {string} name
1573-
* @returns {(otherPathName: string) => void}
1607+
* @returns {(otherPathName: string) => boolean}
15741608
*/
15751609
const comparePaths = (name) => {
15761610
return (otherPathName) => {

src/rules/checkParamNames.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ const validateParameterNames = (
103103
rests,
104104
annotationParamName,
105105
},
106-
] = functionParameterName;
106+
] =
107+
/**
108+
* @type {[string | undefined, import('../jsdocUtils.js').FlattendRootInfo & {
109+
* annotationParamName?: string | undefined;
110+
}]} */ (functionParameterName);
107111
if (annotationParamName !== undefined) {
108112
const name = tag.name.trim();
109113
if (name !== annotationParamName) {
@@ -127,6 +131,8 @@ const validateParameterNames = (
127131
});
128132

129133
const missingProperties = [];
134+
135+
/** @type {string[]} */
130136
const notCheckingNames = [];
131137

132138
for (const [
@@ -173,6 +179,7 @@ const validateParameterNames = (
173179
}
174180

175181
if (!hasPropertyRest || checkRestProperty) {
182+
/** @type {[string, import('comment-parser').Spec][]} */
176183
const extraProperties = [];
177184
for (const [
178185
idx,
@@ -228,7 +235,10 @@ const validateParameterNames = (
228235
return name.trim();
229236
});
230237
const expectedNames = functionParameterNames.map((item, idx) => {
231-
if (item?.[1]?.names) {
238+
if (/**
239+
* @type {[string|undefined, (import('../jsdocUtils.js').FlattendRootInfo & {
240+
* annotationParamName?: string,
241+
})]} */ (item)?.[1]?.names) {
232242
return actualNames[idx];
233243
}
234244

@@ -324,14 +334,14 @@ export default iterateJsdoc(({
324334
const checkTypesRegex = utils.getRegexFromString(checkTypesPattern);
325335

326336
const jsdocParameterNamesDeep = utils.getJsdocTagsDeep('param');
327-
if (!jsdocParameterNamesDeep.length) {
337+
if (!jsdocParameterNamesDeep || !jsdocParameterNamesDeep.length) {
328338
return;
329339
}
330340

331341
const functionParameterNames = utils.getFunctionParameterNames(useDefaultObjectProperties);
332-
const targetTagName = utils.getPreferredTagName({
342+
const targetTagName = /** @type {string} */ (utils.getPreferredTagName({
333343
tagName: 'param',
334-
});
344+
}));
335345
const isError = validateParameterNames(
336346
targetTagName,
337347
allowExtraTrailingParamDocs,

0 commit comments

Comments
 (0)