forked from gajus/eslint-plugin-jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkUsed.js
59 lines (57 loc) · 1.57 KB
/
markUsed.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
import iterateJsdoc from '../iterateJsdoc';
/**
* Extracts the type names from a type declaration string.
*
* @see https://jsdoc.app/tags-type.html
*/
const extractTypeNames = (type) => {
if (type.startsWith('(') && type.endsWith(')')) {
// Type union
return type.slice(1, type.length - 1).split('|').flatMap(extractTypeNames);
} else if (type.endsWith('[]')) {
// Arrays
return extractTypeNames(type.slice(0, Math.max(0, type.length - 2)));
} else if (type.startsWith('!') || type.startsWith('?')) {
// Nullable type or non-nullable type
return extractTypeNames(type.slice(1));
} else if (type.startsWith('...')) {
// Variable number of that type
return extractTypeNames(type.slice(3));
} else if (type.endsWith('=')) {
// Optional parameter
return extractTypeNames(type.slice(0, Math.max(0, type.length - 1)));
} else {
return [
type,
];
}
};
export default iterateJsdoc(({
jsdoc,
jsdocNode,
context,
}) => {
const sourceCode = context.getSourceCode();
for (const tag of jsdoc.tags) {
const typeNames = extractTypeNames(tag.type);
for (const typeName of typeNames) {
sourceCode.markVariableAsUsed(typeName, jsdocNode);
}
}
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: 'Marks all types referenced in {@link} tags as used',
url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-mark-used',
},
fixable: 'code',
schema: [
{
additionalProperties: false,
properties: {},
},
],
type: 'suggestion',
},
});