eslint-plugin-jsdoc 48.0.0 → 48.0.2

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.
Files changed (64) hide show
  1. package/package.json +2 -2
  2. package/src/WarnSettings.js +34 -0
  3. package/src/alignTransform.js +356 -0
  4. package/src/defaultTagOrder.js +168 -0
  5. package/src/exportParser.js +957 -0
  6. package/src/getDefaultTagStructureForMode.js +969 -0
  7. package/src/index.js +266 -0
  8. package/src/iterateJsdoc.js +2555 -0
  9. package/src/jsdocUtils.js +1693 -0
  10. package/src/rules/checkAccess.js +45 -0
  11. package/src/rules/checkAlignment.js +63 -0
  12. package/src/rules/checkExamples.js +594 -0
  13. package/src/rules/checkIndentation.js +75 -0
  14. package/src/rules/checkLineAlignment.js +364 -0
  15. package/src/rules/checkParamNames.js +404 -0
  16. package/src/rules/checkPropertyNames.js +152 -0
  17. package/src/rules/checkSyntax.js +30 -0
  18. package/src/rules/checkTagNames.js +314 -0
  19. package/src/rules/checkTypes.js +535 -0
  20. package/src/rules/checkValues.js +220 -0
  21. package/src/rules/emptyTags.js +88 -0
  22. package/src/rules/implementsOnClasses.js +64 -0
  23. package/src/rules/importsAsDependencies.js +131 -0
  24. package/src/rules/informativeDocs.js +182 -0
  25. package/src/rules/matchDescription.js +286 -0
  26. package/src/rules/matchName.js +147 -0
  27. package/src/rules/multilineBlocks.js +333 -0
  28. package/src/rules/noBadBlocks.js +109 -0
  29. package/src/rules/noBlankBlockDescriptions.js +69 -0
  30. package/src/rules/noBlankBlocks.js +53 -0
  31. package/src/rules/noDefaults.js +85 -0
  32. package/src/rules/noMissingSyntax.js +195 -0
  33. package/src/rules/noMultiAsterisks.js +134 -0
  34. package/src/rules/noRestrictedSyntax.js +91 -0
  35. package/src/rules/noTypes.js +73 -0
  36. package/src/rules/noUndefinedTypes.js +328 -0
  37. package/src/rules/requireAsteriskPrefix.js +189 -0
  38. package/src/rules/requireDescription.js +161 -0
  39. package/src/rules/requireDescriptionCompleteSentence.js +333 -0
  40. package/src/rules/requireExample.js +118 -0
  41. package/src/rules/requireFileOverview.js +154 -0
  42. package/src/rules/requireHyphenBeforeParamDescription.js +178 -0
  43. package/src/rules/requireJsdoc.js +629 -0
  44. package/src/rules/requireParam.js +592 -0
  45. package/src/rules/requireParamDescription.js +89 -0
  46. package/src/rules/requireParamName.js +55 -0
  47. package/src/rules/requireParamType.js +89 -0
  48. package/src/rules/requireProperty.js +48 -0
  49. package/src/rules/requirePropertyDescription.js +25 -0
  50. package/src/rules/requirePropertyName.js +25 -0
  51. package/src/rules/requirePropertyType.js +25 -0
  52. package/src/rules/requireReturns.js +238 -0
  53. package/src/rules/requireReturnsCheck.js +141 -0
  54. package/src/rules/requireReturnsDescription.js +59 -0
  55. package/src/rules/requireReturnsType.js +51 -0
  56. package/src/rules/requireThrows.js +111 -0
  57. package/src/rules/requireYields.js +216 -0
  58. package/src/rules/requireYieldsCheck.js +208 -0
  59. package/src/rules/sortTags.js +557 -0
  60. package/src/rules/tagLines.js +359 -0
  61. package/src/rules/textEscaping.js +146 -0
  62. package/src/rules/validTypes.js +368 -0
  63. package/src/tagNames.js +234 -0
  64. package/src/utils/hasReturnValue.js +549 -0
@@ -0,0 +1,557 @@
1
+ import defaultTagOrder from '../defaultTagOrder.js';
2
+ import iterateJsdoc from '../iterateJsdoc.js';
3
+
4
+ // eslint-disable-next-line complexity -- Temporary
5
+ export default iterateJsdoc(({
6
+ context,
7
+ jsdoc,
8
+ utils,
9
+ }) => {
10
+ const
11
+ /**
12
+ * @type {{
13
+ * linesBetween: import('../iterateJsdoc.js').Integer,
14
+ * tagSequence: {
15
+ * tags: string[]
16
+ * }[],
17
+ * alphabetizeExtras: boolean,
18
+ * reportTagGroupSpacing: boolean,
19
+ * reportIntraTagGroupSpacing: boolean,
20
+ * }}
21
+ */ {
22
+ linesBetween = 1,
23
+ tagSequence = defaultTagOrder,
24
+ alphabetizeExtras = false,
25
+ reportTagGroupSpacing = true,
26
+ reportIntraTagGroupSpacing = true,
27
+ } = context.options[0] || {};
28
+
29
+ const tagList = tagSequence.flatMap((obj) => {
30
+ /* typeof obj === 'string' ? obj : */
31
+ return obj.tags;
32
+ });
33
+
34
+ const otherPos = tagList.indexOf('-other');
35
+ const endPos = otherPos > -1 ? otherPos : tagList.length;
36
+
37
+ let ongoingCount = 0;
38
+ for (const [
39
+ idx,
40
+ tag,
41
+ ] of
42
+ /**
43
+ * @type {(
44
+ * import('@es-joy/jsdoccomment').JsdocTagWithInline & {
45
+ * originalIndex: import('../iterateJsdoc.js').Integer,
46
+ * originalLine: import('../iterateJsdoc.js').Integer,
47
+ * }
48
+ * )[]}
49
+ */ (jsdoc.tags).entries()) {
50
+ tag.originalIndex = idx;
51
+ ongoingCount += tag.source.length;
52
+ tag.originalLine = ongoingCount;
53
+ }
54
+
55
+ /** @type {import('../iterateJsdoc.js').Integer|undefined} */
56
+ let firstChangedTagLine;
57
+ /** @type {import('../iterateJsdoc.js').Integer|undefined} */
58
+ let firstChangedTagIndex;
59
+
60
+ /**
61
+ * @type {(import('comment-parser').Spec & {
62
+ * originalIndex: import('../iterateJsdoc.js').Integer,
63
+ * originalLine: import('../iterateJsdoc.js').Integer,
64
+ * })[]}
65
+ */
66
+ const sortedTags = JSON.parse(JSON.stringify(jsdoc.tags));
67
+ sortedTags.sort(({
68
+ tag: tagNew,
69
+ }, {
70
+ originalIndex,
71
+ originalLine,
72
+ tag: tagOld,
73
+ }) => {
74
+ // Optimize: Just keep relative positions if the same tag name
75
+ if (tagNew === tagOld) {
76
+ return 0;
77
+ }
78
+
79
+ const checkOrSetFirstChanged = () => {
80
+ if (!firstChangedTagLine || originalLine < firstChangedTagLine) {
81
+ firstChangedTagLine = originalLine;
82
+ firstChangedTagIndex = originalIndex;
83
+ }
84
+ };
85
+
86
+ const newPos = tagList.indexOf(tagNew);
87
+ const oldPos = tagList.indexOf(tagOld);
88
+
89
+ const preferredNewPos = newPos === -1 ? endPos : newPos;
90
+ const preferredOldPos = oldPos === -1 ? endPos : oldPos;
91
+
92
+ if (preferredNewPos < preferredOldPos) {
93
+ checkOrSetFirstChanged();
94
+ return -1;
95
+ }
96
+
97
+ if (preferredNewPos > preferredOldPos) {
98
+ return 1;
99
+ }
100
+
101
+ // preferredNewPos === preferredOldPos
102
+ if (
103
+ !alphabetizeExtras ||
104
+
105
+ // Optimize: If tagNew (or tagOld which is the same) was found in the
106
+ // priority array, it can maintain its relative position—without need
107
+ // of alphabetizing (secondary sorting)
108
+ newPos >= 0
109
+ ) {
110
+ return 0;
111
+ }
112
+
113
+ if (tagNew < tagOld) {
114
+ checkOrSetFirstChanged();
115
+ return -1;
116
+ }
117
+
118
+ // tagNew > tagOld
119
+ return 1;
120
+ });
121
+
122
+ if (firstChangedTagLine === undefined) {
123
+ // Should be ordered by now
124
+
125
+ /**
126
+ * @type {import('comment-parser').Spec[]}
127
+ */
128
+ const lastTagsOfGroup = [];
129
+
130
+ /**
131
+ * @type {[
132
+ * import('comment-parser').Spec,
133
+ * import('../iterateJsdoc.js').Integer
134
+ * ][]}
135
+ */
136
+ const badLastTagsOfGroup = [];
137
+
138
+ /**
139
+ * @param {import('comment-parser').Spec} tag
140
+ */
141
+ const countTagEmptyLines = (tag) => {
142
+ return tag.source.reduce((acc, {
143
+ tokens: {
144
+ description,
145
+ name,
146
+ type,
147
+ end,
148
+ tag: tg,
149
+ },
150
+ }) => {
151
+ const empty = !tg && !type && !name && !description;
152
+ // Reset the count so long as there is content
153
+ return empty ? acc + Number(empty && !end) : 0;
154
+ }, 0);
155
+ };
156
+
157
+ let idx = 0;
158
+ for (const {
159
+ tags,
160
+ } of tagSequence) {
161
+ let innerIdx;
162
+ /** @type {import('comment-parser').Spec} */
163
+ let currentTag;
164
+ /** @type {import('comment-parser').Spec|undefined} */
165
+ let lastTag;
166
+ do {
167
+ currentTag = jsdoc.tags[idx];
168
+ if (!currentTag) {
169
+ idx++;
170
+ break;
171
+ }
172
+
173
+ innerIdx = tags.indexOf(currentTag.tag);
174
+
175
+ if (
176
+ innerIdx === -1 &&
177
+ // eslint-disable-next-line no-loop-func -- Safe
178
+ (!tags.includes('-other') || tagSequence.some(({
179
+ tags: tgs,
180
+ }) => {
181
+ return tgs.includes(currentTag.tag);
182
+ }))
183
+ ) {
184
+ idx++;
185
+ break;
186
+ }
187
+
188
+ lastTag = currentTag;
189
+
190
+ idx++;
191
+ } while (true);
192
+
193
+ idx--;
194
+
195
+ if (lastTag) {
196
+ lastTagsOfGroup.push(lastTag);
197
+ const ct = countTagEmptyLines(lastTag);
198
+ if (
199
+ ct !== linesBetween &&
200
+ // Use another rule for adding to end (should be of interest outside this rule)
201
+ jsdoc.tags[idx]
202
+ ) {
203
+ badLastTagsOfGroup.push([
204
+ lastTag, ct,
205
+ ]);
206
+ }
207
+ }
208
+ }
209
+
210
+ if (reportTagGroupSpacing && badLastTagsOfGroup.length) {
211
+ /**
212
+ * @param {import('comment-parser').Spec} tg
213
+ * @returns {() => void}
214
+ */
215
+ const fixer = (tg) => {
216
+ return () => {
217
+ // Due to https://github.com/syavorsky/comment-parser/issues/110 ,
218
+ // we have to modify `jsdoc.source` rather than just modify tags
219
+ // directly
220
+ for (const [
221
+ currIdx,
222
+ {
223
+ tokens,
224
+ },
225
+ ] of jsdoc.source.entries()) {
226
+ if (tokens.tag !== '@' + tg.tag) {
227
+ continue;
228
+ }
229
+
230
+ // Cannot be `tokens.end`, as dropped off last tag, so safe to
231
+ // go on
232
+ let newIdx = currIdx;
233
+
234
+ const emptyLine = () => {
235
+ return {
236
+ number: 0,
237
+ source: '',
238
+ tokens: utils.seedTokens({
239
+ delimiter: '*',
240
+ start: jsdoc.source[newIdx - 1].tokens.start,
241
+ }),
242
+ };
243
+ };
244
+
245
+ let existingEmptyLines = 0;
246
+ while (true) {
247
+ const nextTokens = jsdoc.source[++newIdx]?.tokens;
248
+
249
+ /* c8 ignore next 3 -- Guard */
250
+ if (!nextTokens) {
251
+ return;
252
+ }
253
+
254
+ // Should be no `nextTokens.end` to worry about since ignored
255
+ // if not followed by tag
256
+
257
+ if (nextTokens.tag) {
258
+ // Haven't made it to last tag instance yet, so keep looking
259
+ if (nextTokens.tag === tokens.tag) {
260
+ existingEmptyLines = 0;
261
+ continue;
262
+ }
263
+
264
+ const lineDiff = linesBetween - existingEmptyLines;
265
+ if (lineDiff > 0) {
266
+ const lines = Array.from({
267
+ length: lineDiff,
268
+ }, () => {
269
+ return emptyLine();
270
+ });
271
+ jsdoc.source.splice(newIdx, 0, ...lines);
272
+ } else {
273
+ // lineDiff < 0
274
+ jsdoc.source.splice(
275
+ newIdx + lineDiff,
276
+ -lineDiff,
277
+ );
278
+ }
279
+
280
+ break;
281
+ }
282
+
283
+ const empty = !nextTokens.type && !nextTokens.name &&
284
+ !nextTokens.description;
285
+
286
+ if (empty) {
287
+ existingEmptyLines++;
288
+ } else {
289
+ // Has content again, so reset empty line count
290
+ existingEmptyLines = 0;
291
+ }
292
+ }
293
+
294
+ break;
295
+ }
296
+
297
+ for (const [
298
+ srcIdx,
299
+ src,
300
+ ] of jsdoc.source.entries()) {
301
+ src.number = srcIdx;
302
+ }
303
+ };
304
+ };
305
+
306
+ for (const [
307
+ tg,
308
+ ] of badLastTagsOfGroup) {
309
+ utils.reportJSDoc(
310
+ 'Tag groups do not have the expected whitespace',
311
+ tg,
312
+ fixer(tg),
313
+ );
314
+ }
315
+
316
+ return;
317
+ }
318
+
319
+ if (!reportIntraTagGroupSpacing) {
320
+ return;
321
+ }
322
+
323
+ for (const [
324
+ tagIdx,
325
+ tag,
326
+ ] of jsdoc.tags.entries()) {
327
+ if (!jsdoc.tags[tagIdx + 1] || lastTagsOfGroup.includes(tag)) {
328
+ continue;
329
+ }
330
+
331
+ const ct = countTagEmptyLines(tag);
332
+ if (ct) {
333
+ // eslint-disable-next-line complexity -- Temporary
334
+ const fixer = () => {
335
+ let foundFirstTag = false;
336
+
337
+ /** @type {string|undefined} */
338
+ let currentTag;
339
+
340
+ for (const [
341
+ currIdx,
342
+ {
343
+ tokens: {
344
+ description,
345
+ name,
346
+ type,
347
+ end,
348
+ tag: tg,
349
+ },
350
+ },
351
+ ] of jsdoc.source.entries()) {
352
+ if (tg) {
353
+ foundFirstTag = true;
354
+ currentTag = tg;
355
+ }
356
+
357
+ if (!foundFirstTag) {
358
+ continue;
359
+ }
360
+
361
+ if (currentTag && !tg && !type && !name && !description && !end) {
362
+ let nextIdx = currIdx;
363
+
364
+ let ignore = true;
365
+ // Even if a tag of the same name as the last tags in a group,
366
+ // could still be an earlier tag in that group
367
+
368
+ // eslint-disable-next-line no-loop-func -- Safe
369
+ if (lastTagsOfGroup.some((lastTagOfGroup) => {
370
+ return currentTag === '@' + lastTagOfGroup.tag;
371
+ })) {
372
+ while (true) {
373
+ const nextTokens = jsdoc.source[++nextIdx]?.tokens;
374
+ if (!nextTokens) {
375
+ break;
376
+ }
377
+
378
+ if (!nextTokens.tag) {
379
+ continue;
380
+ }
381
+
382
+ // Followed by the same tag name, so not actually last in group,
383
+ // and of interest
384
+ if (nextTokens.tag === currentTag) {
385
+ ignore = false;
386
+ }
387
+ }
388
+ } else {
389
+ while (true) {
390
+ const nextTokens = jsdoc.source[++nextIdx]?.tokens;
391
+ if (!nextTokens || nextTokens.end) {
392
+ break;
393
+ }
394
+
395
+ // Not the very last tag, so don't ignore
396
+ if (nextTokens.tag) {
397
+ ignore = false;
398
+ break;
399
+ }
400
+ }
401
+ }
402
+
403
+ if (!ignore) {
404
+ jsdoc.source.splice(currIdx, 1);
405
+ for (const [
406
+ srcIdx,
407
+ src,
408
+ ] of jsdoc.source.entries()) {
409
+ src.number = srcIdx;
410
+ }
411
+ }
412
+ }
413
+ }
414
+ };
415
+
416
+ utils.reportJSDoc(
417
+ 'Intra-group tags have unexpected whitespace',
418
+ tag,
419
+ fixer,
420
+ );
421
+ }
422
+ }
423
+
424
+ return;
425
+ }
426
+
427
+ const firstLine = utils.getFirstLine();
428
+
429
+ const fix = () => {
430
+ const itemsToMoveRange = [
431
+ ...Array.from({
432
+ length: jsdoc.tags.length -
433
+ /** @type {import('../iterateJsdoc.js').Integer} */ (
434
+ firstChangedTagIndex
435
+ ),
436
+ }).keys(),
437
+ ];
438
+
439
+ const unchangedPriorTagDescriptions = jsdoc.tags.slice(
440
+ 0,
441
+ firstChangedTagIndex,
442
+ ).reduce((ct, {
443
+ source,
444
+ }) => {
445
+ return ct + source.length - 1;
446
+ }, 0);
447
+
448
+ // This offset includes not only the offset from where the first tag
449
+ // must begin, and the additional offset of where the first changed
450
+ // tag begins, but it must also account for prior descriptions
451
+ const initialOffset = /** @type {import('../iterateJsdoc.js').Integer} */ (
452
+ firstLine
453
+ ) + /** @type {import('../iterateJsdoc.js').Integer} */ (firstChangedTagIndex) +
454
+
455
+ // May be the first tag, so don't try finding a prior one if so
456
+ unchangedPriorTagDescriptions;
457
+
458
+ // Use `firstChangedTagLine` for line number to begin reporting/splicing
459
+ for (const idx of itemsToMoveRange) {
460
+ utils.removeTag(
461
+ idx +
462
+ /** @type {import('../iterateJsdoc.js').Integer} */ (
463
+ firstChangedTagIndex
464
+ ),
465
+ );
466
+ }
467
+
468
+ const changedTags = sortedTags.slice(firstChangedTagIndex);
469
+ let extraTagCount = 0;
470
+
471
+ for (const idx of itemsToMoveRange) {
472
+ const changedTag = changedTags[idx];
473
+
474
+ utils.addTag(
475
+ changedTag.tag,
476
+ extraTagCount + initialOffset + idx,
477
+ {
478
+ ...changedTag.source[0].tokens,
479
+
480
+ // `comment-parser` puts the `end` within the `tags` section, so
481
+ // avoid adding another to jsdoc.source
482
+ end: '',
483
+ },
484
+ );
485
+
486
+ for (const {
487
+ tokens,
488
+ } of changedTag.source.slice(1)) {
489
+ if (!tokens.end) {
490
+ utils.addLine(
491
+ extraTagCount + initialOffset + idx + 1,
492
+ {
493
+ ...tokens,
494
+ end: '',
495
+ },
496
+ );
497
+ extraTagCount++;
498
+ }
499
+ }
500
+ }
501
+ };
502
+
503
+ utils.reportJSDoc(
504
+ `Tags are not in the prescribed order: ${
505
+ tagList.join(', ') || '(alphabetical)'
506
+ }`,
507
+ jsdoc.tags[/** @type {import('../iterateJsdoc.js').Integer} */ (
508
+ firstChangedTagIndex
509
+ )],
510
+ fix,
511
+ true,
512
+ );
513
+ }, {
514
+ iterateAllJsdocs: true,
515
+ meta: {
516
+ docs: {
517
+ description: 'Sorts tags by a specified sequence according to tag name.',
518
+ url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/sort-tags.md#repos-sticky-header',
519
+ },
520
+ fixable: 'code',
521
+ schema: [
522
+ {
523
+ additionalProperties: false,
524
+ properties: {
525
+ alphabetizeExtras: {
526
+ type: 'boolean',
527
+ },
528
+ linesBetween: {
529
+ type: 'integer',
530
+ },
531
+ reportIntraTagGroupSpacing: {
532
+ type: 'boolean',
533
+ },
534
+ reportTagGroupSpacing: {
535
+ type: 'boolean',
536
+ },
537
+ tagSequence: {
538
+ items: {
539
+ properties: {
540
+ tags: {
541
+ items: {
542
+ type: 'string',
543
+ },
544
+ type: 'array',
545
+ },
546
+ },
547
+ type: 'object',
548
+ },
549
+ type: 'array',
550
+ },
551
+ },
552
+ type: 'object',
553
+ },
554
+ ],
555
+ type: 'suggestion',
556
+ },
557
+ });