eslint-plugin-jsdoc 48.5.2 → 48.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ import {
2
+ parse as parseType,
3
+ traverse,
4
+ tryParse as tryParseType,
5
+ } from '@es-joy/jsdoccomment';
6
+ import iterateJsdoc from '../iterateJsdoc.js';
7
+
8
+ export default iterateJsdoc(({
9
+ context,
10
+ utils,
11
+ node,
12
+ settings,
13
+ report,
14
+ }) => {
15
+ const {
16
+ requireSeparateTemplates = false,
17
+ } = context.options[0] || {};
18
+
19
+ const {
20
+ mode
21
+ } = settings;
22
+
23
+ const usedNames = new Set();
24
+ const templateTags = utils.getTags('template');
25
+ const templateNames = templateTags.flatMap(({name}) => {
26
+ return name.split(/,\s*/);
27
+ });
28
+
29
+ for (const tag of templateTags) {
30
+ const {name} = tag;
31
+ const names = name.split(/,\s*/);
32
+ if (requireSeparateTemplates && names.length > 1) {
33
+ report(`Missing separate @template for ${names[1]}`, null, tag);
34
+ }
35
+ }
36
+
37
+ /**
38
+ * @param {import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration
39
+ */
40
+ const checkParameters = (aliasDeclaration) => {
41
+ /* c8 ignore next -- Guard */
42
+ const {params} = aliasDeclaration.typeParameters ?? {params: []};
43
+ for (const {name: {name}} of params) {
44
+ usedNames.add(name);
45
+ }
46
+ for (const usedName of usedNames) {
47
+ if (!templateNames.includes(usedName)) {
48
+ report(`Missing @template ${usedName}`);
49
+ }
50
+ }
51
+ };
52
+
53
+ const handleTypeAliases = () => {
54
+ const nde = /** @type {import('@typescript-eslint/types').TSESTree.Node} */ (
55
+ node
56
+ );
57
+ if (!nde) {
58
+ return;
59
+ }
60
+ switch (nde.type) {
61
+ case 'ExportNamedDeclaration':
62
+ if (nde.declaration?.type === 'TSTypeAliasDeclaration') {
63
+ checkParameters(nde.declaration);
64
+ }
65
+ break;
66
+ case 'TSTypeAliasDeclaration':
67
+ checkParameters(nde);
68
+ break;
69
+ }
70
+ };
71
+
72
+ const typedefTags = utils.getTags('typedef');
73
+ if (!typedefTags.length || typedefTags.length >= 2) {
74
+ handleTypeAliases();
75
+ return;
76
+ }
77
+
78
+ const potentialType = typedefTags[0].type;
79
+ const parsedType = mode === 'permissive' ?
80
+ tryParseType(/** @type {string} */ (potentialType)) :
81
+ parseType(/** @type {string} */ (potentialType), mode)
82
+
83
+ traverse(parsedType, (nde) => {
84
+ const {
85
+ type,
86
+ value,
87
+ } = /** @type {import('jsdoc-type-pratt-parser').NameResult} */ (nde);
88
+ if (type === 'JsdocTypeName' && (/^[A-Z]$/).test(value)) {
89
+ usedNames.add(value);
90
+ }
91
+ });
92
+
93
+ // Could check against whitelist/blacklist
94
+ for (const usedName of usedNames) {
95
+ if (!templateNames.includes(usedName)) {
96
+ report(`Missing @template ${usedName}`, null, typedefTags[0]);
97
+ }
98
+ }
99
+ }, {
100
+ iterateAllJsdocs: true,
101
+ meta: {
102
+ docs: {
103
+ description: 'Requires template tags for each generic type parameter',
104
+ url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/require-template.md#repos-sticky-header',
105
+ },
106
+ schema: [
107
+ {
108
+ additionalProperties: false,
109
+ properties: {
110
+ requireSeparateTemplates: {
111
+ type: 'boolean'
112
+ }
113
+ },
114
+ type: 'object',
115
+ },
116
+ ],
117
+ type: 'suggestion',
118
+ },
119
+ });