-
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathcheckTemplateNames.js
More file actions
208 lines (187 loc) · 5.35 KB
/
checkTemplateNames.js
File metadata and controls
208 lines (187 loc) · 5.35 KB
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
import iterateJsdoc, {
parseComment,
} from '../iterateJsdoc.js';
import {
getTags,
} from '../jsdocUtils.js';
import {
getJSDocComment,
parse as parseType,
traverse,
tryParse as tryParseType,
} from '@es-joy/jsdoccomment';
export default iterateJsdoc(({
jsdoc,
node,
report,
settings,
sourceCode,
utils,
}) => {
const {
mode,
} = settings;
const tgName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'template',
}));
if (!tgName) {
return;
}
const templateTags = utils.getTags(tgName);
const usedNames = new Set();
/**
* @param {string} potentialType
*/
const checkForUsedTypes = (potentialType) => {
let parsedType;
try {
parsedType = mode === 'permissive' ?
tryParseType(/** @type {string} */ (potentialType)) :
parseType(/** @type {string} */ (potentialType), mode);
} catch {
return;
}
traverse(parsedType, (nde) => {
const {
type,
value,
} = /** @type {import('jsdoc-type-pratt-parser').NameResult} */ (nde);
if (type === 'JsdocTypeName') {
usedNames.add(value);
}
});
};
const checkParamsAndReturnsTags = (jsdc = jsdoc) => {
const paramName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'param',
}));
const paramTags = getTags(jsdc, paramName);
for (const paramTag of paramTags) {
checkForUsedTypes(paramTag.type);
}
const returnsName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'returns',
}));
const returnsTags = getTags(jsdc, returnsName);
for (const returnsTag of returnsTags) {
checkForUsedTypes(returnsTag.type);
}
};
const checkTemplateTags = () => {
for (const tag of templateTags) {
const names = utils.parseClosureTemplateTag(tag);
for (const nme of names) {
if (!usedNames.has(nme)) {
report(`@${tgName} ${nme} not in use`, null, tag);
}
}
}
};
/**
* @param {import('@typescript-eslint/types').TSESTree.FunctionDeclaration|
* import('@typescript-eslint/types').TSESTree.ClassDeclaration|
* import('@typescript-eslint/types').TSESTree.TSInterfaceDeclaration|
* import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration
* @param {boolean} [checkParamsAndReturns]
*/
const checkParameters = (aliasDeclaration, checkParamsAndReturns) => {
/* c8 ignore next -- Guard */
const {
params,
} = aliasDeclaration.typeParameters ?? {
params: [],
};
for (const {
name: {
name,
},
} of params) {
usedNames.add(name);
}
if (checkParamsAndReturns) {
checkParamsAndReturnsTags();
} else if (aliasDeclaration.type === 'ClassDeclaration') {
/* c8 ignore next -- TS */
for (const nde of aliasDeclaration?.body?.body ?? []) {
// @ts-expect-error Should be ok
const commentNode = getJSDocComment(sourceCode, nde, settings);
if (!commentNode) {
continue;
}
const innerJsdoc = parseComment(commentNode, '');
checkParamsAndReturnsTags(innerJsdoc);
const typeName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'type',
}));
const typeTags = getTags(innerJsdoc, typeName);
for (const typeTag of typeTags) {
checkForUsedTypes(typeTag.type);
}
}
}
checkTemplateTags();
};
const handleTypeAliases = () => {
const nde = /** @type {import('@typescript-eslint/types').TSESTree.Node} */ (
node
);
if (!nde) {
return;
}
switch (nde.type) {
case 'ClassDeclaration':
case 'TSInterfaceDeclaration':
case 'TSTypeAliasDeclaration':
checkParameters(nde);
break;
case 'ExportDefaultDeclaration':
case 'ExportNamedDeclaration':
switch (nde.declaration?.type) {
case 'ClassDeclaration':
case 'TSInterfaceDeclaration':
case 'TSTypeAliasDeclaration':
checkParameters(nde.declaration);
break;
case 'FunctionDeclaration':
checkParameters(nde.declaration, true);
break;
}
break;
case 'FunctionDeclaration':
checkParameters(nde, true);
break;
}
};
const callbackTags = utils.getTags('callback');
const functionTags = utils.getTags('function');
if (callbackTags.length || functionTags.length) {
checkParamsAndReturnsTags();
checkTemplateTags();
return;
}
const typedefTags = utils.getTags('typedef');
if (!typedefTags.length || typedefTags.length >= 2) {
handleTypeAliases();
return;
}
const potentialTypedefType = typedefTags[0].type;
checkForUsedTypes(potentialTypedefType);
const propertyName = /** @type {string} */ (utils.getPreferredTagName({
tagName: 'property',
}));
const propertyTags = utils.getTags(propertyName);
for (const propertyTag of propertyTags) {
checkForUsedTypes(propertyTag.type);
}
checkTemplateTags();
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: 'Checks that any `@template` names are actually used in the connected `@typedef` or type alias.',
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/check-template-names.md#repos-sticky-header',
},
schema: [],
type: 'suggestion',
},
});