-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
validTypes.js
384 lines (331 loc) · 9.83 KB
/
validTypes.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import iterateJsdoc from '../iterateJsdoc.js';
import {
parse,
traverse,
tryParse,
} from '@es-joy/jsdoccomment';
const inlineTags = new Set([
'link', 'linkcode', 'linkplain',
'tutorial',
]);
const jsdocTypePrattKeywords = new Set([
'typeof',
'readonly',
'import',
'is',
]);
const asExpression = /as\s+/u;
const suppressTypes = new Set([
// https://github.com/google/closure-compiler/wiki/@suppress-annotations
// https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/parsing/ParserConfig.properties#L154
'accessControls',
'checkDebuggerStatement',
'checkPrototypalTypes',
'checkRegExp',
'checkTypes',
'checkVars',
'closureDepMethodUsageChecks',
'const',
'constantProperty',
'deprecated',
'duplicate',
'es5Strict',
'externsValidation',
'extraProvide',
'extraRequire',
'globalThis',
'invalidCasts',
'lateProvide',
'legacyGoogScopeRequire',
'lintChecks',
'messageConventions',
'misplacedTypeAnnotation',
'missingOverride',
'missingPolyfill',
'missingProperties',
'missingProvide',
'missingRequire',
'missingSourcesWarnings',
'moduleLoad',
'nonStandardJsDocs',
'partialAlias',
'polymer',
'reportUnknownTypes',
'strictMissingProperties',
'strictModuleDepCheck',
'strictPrimitiveOperators',
'suspiciousCode',
// Not documented in enum
'switch',
'transitionalSuspiciousCodeWarnings',
'undefinedNames',
'undefinedVars',
'underscore',
'unknownDefines',
'untranspilableFeatures',
'unusedLocalVariables',
'unusedPrivateMembers',
'useOfGoogProvide',
'uselessCode',
'visibility',
'with',
]);
/**
* @param {string} path
* @returns {boolean}
*/
const tryParsePathIgnoreError = (path) => {
try {
tryParse(path);
return true;
} catch {
// Keep the original error for including the whole type
}
return false;
};
// eslint-disable-next-line complexity
export default iterateJsdoc(({
jsdoc,
report,
utils,
context,
settings,
}) => {
const {
allowEmptyNamepaths = false,
} = context.options[0] || {};
const {
mode,
} = settings;
for (const tag of jsdoc.tags) {
/**
* @param {string} namepath
* @param {string} [tagName]
* @returns {boolean}
*/
const validNamepathParsing = function (namepath, tagName) {
if (
tryParsePathIgnoreError(namepath) ||
jsdocTypePrattKeywords.has(namepath)
) {
return true;
}
let handled = false;
if (tagName) {
switch (tagName) {
case 'requires':
case 'module': {
if (!namepath.startsWith('module:')) {
handled = tryParsePathIgnoreError(`module:${namepath}`);
}
break;
}
case 'memberof': case 'memberof!': {
const endChar = namepath.slice(-1);
if ([
'#', '.', '~',
].includes(endChar)) {
handled = tryParsePathIgnoreError(namepath.slice(0, -1));
}
break;
}
case 'borrows': {
const startChar = namepath.charAt(0);
if ([
'#', '.', '~',
].includes(startChar)) {
handled = tryParsePathIgnoreError(namepath.slice(1));
}
}
}
}
if (!handled) {
report(`Syntax error in namepath: ${namepath}`, null, tag);
return false;
}
return true;
};
/**
* @param {string} type
* @returns {boolean}
*/
const validTypeParsing = function (type) {
let parsedTypes;
try {
if (mode === 'permissive') {
parsedTypes = tryParse(type);
} else {
parsedTypes = parse(type, mode);
}
} catch {
report(`Syntax error in type: ${type}`, null, tag);
return false;
}
if (mode === 'closure' || mode === 'typescript') {
traverse(parsedTypes, (node) => {
const {
type: typ,
} = node;
if (
(typ === 'JsdocTypeObjectField' || typ === 'JsdocTypeKeyValue') &&
node.right?.type === 'JsdocTypeNullable' &&
node.right?.meta?.position === 'suffix'
) {
report(`Syntax error in type: ${node.right.type}`, null, tag);
}
});
}
return true;
};
if (tag.problems.length) {
const msg = tag.problems.reduce((str, {
message,
}) => {
return str + '; ' + message;
}, '').slice(2);
report(`Invalid name: ${msg}`, null, tag);
continue;
}
if (tag.tag === 'import') {
// A named import will look like a type, but not be valid; we also don't
// need to check the name/namepath
continue;
}
if (tag.tag === 'borrows') {
const thisNamepath = /** @type {string} */ (
utils.getTagDescription(tag)
).replace(asExpression, '')
.trim();
if (!asExpression.test(/** @type {string} */ (
utils.getTagDescription(tag)
)) || !thisNamepath) {
report(`@borrows must have an "as" expression. Found "${utils.getTagDescription(tag)}"`, null, tag);
continue;
}
if (validNamepathParsing(thisNamepath, 'borrows')) {
const thatNamepath = tag.name;
validNamepathParsing(thatNamepath);
}
continue;
}
if (tag.tag === 'suppress' && mode === 'closure') {
let parsedTypes;
try {
parsedTypes = tryParse(tag.type);
} catch {
// Ignore
}
if (parsedTypes) {
traverse(parsedTypes, (node) => {
let type;
if ('value' in node && typeof node.value === 'string') {
type = node.value;
}
if (type !== undefined && !suppressTypes.has(type)) {
report(`Syntax error in suppress type: ${type}`, null, tag);
}
});
}
}
const otherModeMaps = /** @type {import('../jsdocUtils.js').ParserMode[]} */ ([
'jsdoc', 'typescript', 'closure', 'permissive',
]).filter(
(mde) => {
return mde !== mode;
},
).map((mde) => {
return utils.getTagStructureForMode(mde);
});
const tagMightHaveNamePosition = utils.tagMightHaveNamePosition(tag.tag, otherModeMaps);
if (tagMightHaveNamePosition !== true && tag.name) {
const modeInfo = tagMightHaveNamePosition === false ? '' : ` in "${mode}" mode`;
report(`@${tag.tag} should not have a name${modeInfo}.`, null, tag);
continue;
}
const mightHaveTypePosition = utils.tagMightHaveTypePosition(tag.tag, otherModeMaps);
if (mightHaveTypePosition !== true && tag.type) {
const modeInfo = mightHaveTypePosition === false ? '' : ` in "${mode}" mode`;
report(`@${tag.tag} should not have a bracketed type${modeInfo}.`, null, tag);
continue;
}
// REQUIRED NAME
const tagMustHaveNamePosition = utils.tagMustHaveNamePosition(tag.tag, otherModeMaps);
// Don't handle `@param` here though it does require name as handled by
// `require-param-name` (`@property` would similarly seem to require one,
// but is handled by `require-property-name`)
if (tagMustHaveNamePosition !== false && !tag.name && !allowEmptyNamepaths && ![
'param', 'arg', 'argument',
'property', 'prop',
].includes(tag.tag) &&
(tag.tag !== 'see' || !utils.getTagDescription(tag).includes('{@link'))
) {
const modeInfo = tagMustHaveNamePosition === true ? '' : ` in "${mode}" mode`;
report(`Tag @${tag.tag} must have a name/namepath${modeInfo}.`, null, tag);
continue;
}
// REQUIRED TYPE
const mustHaveTypePosition = utils.tagMustHaveTypePosition(tag.tag, otherModeMaps);
if (mustHaveTypePosition !== false && !tag.type) {
const modeInfo = mustHaveTypePosition === true ? '' : ` in "${mode}" mode`;
report(`Tag @${tag.tag} must have a type${modeInfo}.`, null, tag);
continue;
}
// REQUIRED TYPE OR NAME/NAMEPATH
const tagMissingRequiredTypeOrNamepath = utils.tagMissingRequiredTypeOrNamepath(tag, otherModeMaps);
if (tagMissingRequiredTypeOrNamepath !== false && !allowEmptyNamepaths) {
const modeInfo = tagMissingRequiredTypeOrNamepath === true ? '' : ` in "${mode}" mode`;
report(`Tag @${tag.tag} must have either a type or namepath${modeInfo}.`, null, tag);
continue;
}
// VALID TYPE
const hasTypePosition = mightHaveTypePosition === true && Boolean(tag.type);
if (hasTypePosition) {
validTypeParsing(tag.type);
}
// VALID NAME/NAMEPATH
const hasNameOrNamepathPosition = (
tagMustHaveNamePosition !== false ||
utils.tagMightHaveNamepath(tag.tag)
) && Boolean(tag.name);
if (hasNameOrNamepathPosition) {
if (mode !== 'jsdoc' && tag.tag === 'template') {
for (const namepath of utils.parseClosureTemplateTag(tag)) {
validNamepathParsing(namepath);
}
} else {
validNamepathParsing(tag.name, tag.tag);
}
}
for (const inlineTag of tag.inlineTags) {
if (inlineTags.has(inlineTag.tag) && !inlineTag.text && !inlineTag.namepathOrURL) {
report(`Inline tag "${inlineTag.tag}" missing content`, null, tag);
}
}
}
for (const inlineTag of jsdoc.inlineTags) {
if (inlineTags.has(inlineTag.tag) && !inlineTag.text && !inlineTag.namepathOrURL) {
report(`Inline tag "${inlineTag.tag}" missing content`);
}
}
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: 'Requires all types to be valid JSDoc or Closure compiler types without syntax errors.',
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/valid-types.md#repos-sticky-header',
},
schema: [
{
additionalProperties: false,
properties: {
allowEmptyNamepaths: {
default: false,
type: 'boolean',
},
},
type: 'object',
},
],
type: 'suggestion',
},
});