eslint-plugin-jsdoc 55.2.0 → 55.4.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,412 @@
1
+ import iterateJsdoc from '../iterateJsdoc.js';
2
+ import {
3
+ parse as parseType,
4
+ stringify,
5
+ traverse,
6
+ tryParse as tryParseType,
7
+ } from '@es-joy/jsdoccomment';
8
+
9
+ export default iterateJsdoc(({
10
+ context,
11
+ indent,
12
+ jsdoc,
13
+ settings,
14
+ utils,
15
+ }) => {
16
+ const {
17
+ arrayBrackets = 'square',
18
+ enableFixer = true,
19
+ genericDot = false,
20
+ objectFieldIndent = '',
21
+ objectFieldQuote = null,
22
+ objectFieldSeparator = 'comma',
23
+ objectFieldSeparatorTrailingPunctuation = false,
24
+ propertyQuotes = null,
25
+ separatorForSingleObjectField = false,
26
+ stringQuotes = 'single',
27
+ typeBracketSpacing = '',
28
+ unionSpacing = ' ',
29
+ } = context.options[0] || {};
30
+
31
+ const {
32
+ mode,
33
+ } = settings;
34
+
35
+ /**
36
+ * @param {import('@es-joy/jsdoccomment').JsdocTagWithInline} tag
37
+ */
38
+ const checkTypeFormats = (tag) => {
39
+ const potentialType = tag.type;
40
+ let parsedType;
41
+ try {
42
+ parsedType = mode === 'permissive' ?
43
+ tryParseType(/** @type {string} */ (potentialType)) :
44
+ parseType(/** @type {string} */ (potentialType), mode);
45
+ } catch {
46
+ return;
47
+ }
48
+
49
+ const fix = () => {
50
+ const typeLines = stringify(parsedType).split('\n');
51
+ const firstTypeLine = typeLines.shift();
52
+ const lastTypeLine = typeLines.pop();
53
+
54
+ const beginNameOrDescIdx = tag.source.findIndex(({
55
+ tokens,
56
+ }) => {
57
+ return tokens.name || tokens.description;
58
+ });
59
+
60
+ const nameAndDesc = beginNameOrDescIdx === -1 ?
61
+ null :
62
+ tag.source.slice(beginNameOrDescIdx);
63
+
64
+ const initialNumber = tag.source[0].number;
65
+ const src = [
66
+ // Get inevitably present tag from first `tag.source`
67
+ {
68
+ number: initialNumber,
69
+ source: '',
70
+ tokens: {
71
+ ...tag.source[0].tokens,
72
+ ...(typeLines.length || lastTypeLine ? {
73
+ end: '',
74
+ name: '',
75
+ postName: '',
76
+ postType: '',
77
+ } : {}),
78
+ type: '{' + typeBracketSpacing + firstTypeLine + (!typeLines.length && lastTypeLine === undefined ? typeBracketSpacing + '}' : ''),
79
+ },
80
+ },
81
+ // Get any intervening type lines
82
+ ...(typeLines.length ? typeLines.map((typeLine, idx) => {
83
+ return {
84
+ number: initialNumber + idx + 1,
85
+ source: '',
86
+ tokens: {
87
+ // Grab any delimiter info from first item
88
+ ...tag.source[0].tokens,
89
+ delimiter: tag.source[0].tokens.delimiter === '/**' ? '*' : tag.source[0].tokens.delimiter,
90
+ end: '',
91
+ name: '',
92
+ postName: '',
93
+ postTag: '',
94
+ postType: '',
95
+ start: indent + ' ',
96
+ tag: '',
97
+ type: typeLine,
98
+ },
99
+ };
100
+ }) : []),
101
+ ];
102
+
103
+ // Merge any final type line and name and description
104
+ if (
105
+ // Name and description may be already included if present with the tag
106
+ nameAndDesc && beginNameOrDescIdx > 0
107
+ ) {
108
+ src.push({
109
+ number: src.length + 1,
110
+ source: '',
111
+ tokens: {
112
+ ...nameAndDesc[0].tokens,
113
+ type: lastTypeLine + typeBracketSpacing + '}',
114
+ },
115
+ });
116
+
117
+ if (
118
+ // Get any remaining description lines
119
+ nameAndDesc.length > 1
120
+ ) {
121
+ src.push(
122
+ ...nameAndDesc.slice(1).map(({
123
+ source,
124
+ tokens,
125
+ }, idx) => {
126
+ return {
127
+ number: src.length + idx + 2,
128
+ source,
129
+ tokens,
130
+ };
131
+ }),
132
+ );
133
+ }
134
+ } else if (nameAndDesc) {
135
+ if (lastTypeLine) {
136
+ src.push({
137
+ number: src.length + 1,
138
+ source: '',
139
+ tokens: {
140
+ ...nameAndDesc[0].tokens,
141
+ delimiter: nameAndDesc[0].tokens.delimiter === '/**' ? '*' : nameAndDesc[0].tokens.delimiter,
142
+ postTag: '',
143
+ start: indent + ' ',
144
+ tag: '',
145
+ type: lastTypeLine + typeBracketSpacing + '}',
146
+ },
147
+ });
148
+ }
149
+
150
+ if (
151
+ // Get any remaining description lines
152
+ nameAndDesc.length > 1
153
+ ) {
154
+ src.push(
155
+ ...nameAndDesc.slice(1).map(({
156
+ source,
157
+ tokens,
158
+ }, idx) => {
159
+ return {
160
+ number: src.length + idx + 2,
161
+ source,
162
+ tokens,
163
+ };
164
+ }),
165
+ );
166
+ }
167
+ }
168
+
169
+ tag.source = src;
170
+
171
+ // Properly rewire `jsdoc.source`
172
+ const firstTagIdx = jsdoc.source.findIndex(({
173
+ tokens: {
174
+ tag: tg,
175
+ },
176
+ }) => {
177
+ return tg;
178
+ });
179
+
180
+ const initialEndSource = jsdoc.source.find(({
181
+ tokens: {
182
+ end,
183
+ },
184
+ }) => {
185
+ return end;
186
+ });
187
+
188
+ jsdoc.source = [
189
+ ...jsdoc.source.slice(0, firstTagIdx),
190
+ ...jsdoc.tags.flatMap(({
191
+ source,
192
+ }) => {
193
+ return source;
194
+ }),
195
+ ];
196
+
197
+ if (initialEndSource && !jsdoc.source.at(-1)?.tokens?.end) {
198
+ jsdoc.source.push(initialEndSource);
199
+ }
200
+ };
201
+
202
+ /** @type {string[]} */
203
+ const errorMessages = [];
204
+
205
+ if (typeBracketSpacing && (!tag.type.startsWith(typeBracketSpacing) || !tag.type.endsWith(typeBracketSpacing))) {
206
+ errorMessages.push(`Must have initial and final "${typeBracketSpacing}" spacing`);
207
+ } else if (!typeBracketSpacing && ((/^\s/v).test(tag.type) || (/\s$/v).test(tag.type))) {
208
+ errorMessages.push('Must have no initial spacing');
209
+ }
210
+
211
+ // eslint-disable-next-line complexity -- Todo
212
+ traverse(parsedType, (nde) => {
213
+ let errorMessage = '';
214
+
215
+ switch (nde.type) {
216
+ case 'JsdocTypeGeneric': {
217
+ const typeNode = /** @type {import('jsdoc-type-pratt-parser').GenericResult} */ (nde);
218
+ if ('value' in typeNode.left && typeNode.left.value === 'Array') {
219
+ if (typeNode.meta.brackets !== arrayBrackets) {
220
+ typeNode.meta.brackets = arrayBrackets;
221
+ errorMessage = `Array bracket style should be ${arrayBrackets}`;
222
+ }
223
+ } else if (typeNode.meta.dot !== genericDot) {
224
+ typeNode.meta.dot = genericDot;
225
+ errorMessage = `Dot usage should be ${genericDot}`;
226
+ }
227
+
228
+ break;
229
+ }
230
+
231
+ case 'JsdocTypeObject': {
232
+ const typeNode = /** @type {import('jsdoc-type-pratt-parser').ObjectResult} */ (nde);
233
+ if (
234
+ /* c8 ignore next -- Guard */
235
+ (typeNode.meta.separator ?? 'comma') !== objectFieldSeparator ||
236
+ (typeNode.meta.separatorForSingleObjectField ?? false) !== separatorForSingleObjectField ||
237
+ (typeNode.meta.propertyIndent ?? '') !== objectFieldIndent ||
238
+ (typeNode.meta.trailingPunctuation ?? false) !== objectFieldSeparatorTrailingPunctuation
239
+ ) {
240
+ typeNode.meta.separator = objectFieldSeparator;
241
+ typeNode.meta.separatorForSingleObjectField = separatorForSingleObjectField;
242
+ typeNode.meta.propertyIndent = objectFieldIndent;
243
+ typeNode.meta.trailingPunctuation = objectFieldSeparatorTrailingPunctuation;
244
+ errorMessage = `Inconsistent ${objectFieldSeparator} separator usage`;
245
+ }
246
+
247
+ break;
248
+ }
249
+
250
+ case 'JsdocTypeObjectField': {
251
+ const typeNode = /** @type {import('jsdoc-type-pratt-parser').ObjectFieldResult} */ (nde);
252
+ if ((objectFieldQuote ||
253
+ (typeof typeNode.key === 'string' && !(/\s/v).test(typeNode.key))) &&
254
+ typeNode.meta.quote !== (objectFieldQuote ?? undefined)
255
+ ) {
256
+ typeNode.meta.quote = objectFieldQuote ?? undefined;
257
+ errorMessage = `Inconsistent object field quotes ${objectFieldQuote}`;
258
+ }
259
+
260
+ break;
261
+ }
262
+
263
+ case 'JsdocTypeProperty': {
264
+ const typeNode = /** @type {import('jsdoc-type-pratt-parser').PropertyResult} */ (nde);
265
+
266
+ if ((propertyQuotes ||
267
+ (typeof typeNode.value === 'string' && !(/\s/v).test(typeNode.value))) &&
268
+ typeNode.meta.quote !== (propertyQuotes ?? undefined)
269
+ ) {
270
+ typeNode.meta.quote = propertyQuotes ?? undefined;
271
+ errorMessage = `Inconsistent ${propertyQuotes} property quotes usage`;
272
+ }
273
+
274
+ break;
275
+ }
276
+
277
+ case 'JsdocTypeStringValue': {
278
+ const typeNode = /** @type {import('jsdoc-type-pratt-parser').StringValueResult} */ (nde);
279
+ if (typeNode.meta.quote !== stringQuotes) {
280
+ typeNode.meta.quote = stringQuotes;
281
+ errorMessage = `Inconsistent ${stringQuotes} string quotes usage`;
282
+ }
283
+
284
+ break;
285
+ }
286
+
287
+ case 'JsdocTypeUnion': {
288
+ const typeNode = /** @type {import('jsdoc-type-pratt-parser').UnionResult} */ (nde);
289
+ /* c8 ignore next -- Guard */
290
+ if ((typeNode.meta?.spacing ?? ' ') !== unionSpacing) {
291
+ typeNode.meta = {
292
+ spacing: unionSpacing,
293
+ };
294
+ errorMessage = `Inconsistent "${unionSpacing}" union spacing usage`;
295
+ }
296
+
297
+ break;
298
+ }
299
+
300
+ default:
301
+ break;
302
+ }
303
+
304
+ if (errorMessage) {
305
+ errorMessages.push(errorMessage);
306
+ }
307
+ });
308
+
309
+ const differentResult = tag.type !==
310
+ typeBracketSpacing + stringify(parsedType) + typeBracketSpacing;
311
+
312
+ if (errorMessages.length && differentResult) {
313
+ for (const errorMessage of errorMessages) {
314
+ utils.reportJSDoc(
315
+ errorMessage, tag, enableFixer ? fix : null,
316
+ );
317
+ }
318
+ // Stringification may have been equal previously (and thus no error reported)
319
+ // because the stringification doesn't preserve everything
320
+ } else if (differentResult) {
321
+ utils.reportJSDoc(
322
+ 'There was an error with type formatting', tag, enableFixer ? fix : null,
323
+ );
324
+ }
325
+ };
326
+
327
+ const tags = utils.getPresentTags([
328
+ 'param',
329
+ 'returns',
330
+ 'type',
331
+ 'typedef',
332
+ ]);
333
+ for (const tag of tags) {
334
+ if (tag.type) {
335
+ checkTypeFormats(tag);
336
+ }
337
+ }
338
+ }, {
339
+ iterateAllJsdocs: true,
340
+ meta: {
341
+ docs: {
342
+ description: 'Formats JSDoc type values.',
343
+ url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/type-formatting.md#repos-sticky-header',
344
+ },
345
+ fixable: 'code',
346
+ schema: [
347
+ {
348
+ additionalProperties: false,
349
+ properties: {
350
+ arrayBrackets: {
351
+ enum: [
352
+ 'angle',
353
+ 'square',
354
+ ],
355
+ },
356
+ enableFixer: {
357
+ type: 'boolean',
358
+ },
359
+ genericDot: {
360
+ type: 'boolean',
361
+ },
362
+ objectFieldIndent: {
363
+ type: 'string',
364
+ },
365
+ objectFieldQuote: {
366
+ enum: [
367
+ 'double',
368
+ 'single',
369
+ null,
370
+ ],
371
+ },
372
+ objectFieldSeparator: {
373
+ enum: [
374
+ 'comma',
375
+ 'comma-and-linebreak',
376
+ 'linebreak',
377
+ 'semicolon',
378
+ 'semicolon-and-linebreak',
379
+ ],
380
+ },
381
+ objectFieldSeparatorTrailingPunctuation: {
382
+ type: 'boolean',
383
+ },
384
+ propertyQuotes: {
385
+ enum: [
386
+ 'double',
387
+ 'single',
388
+ null,
389
+ ],
390
+ },
391
+ separatorForSingleObjectField: {
392
+ type: 'boolean',
393
+ },
394
+ stringQuotes: {
395
+ enum: [
396
+ 'double',
397
+ 'single',
398
+ ],
399
+ },
400
+ typeBracketSpacing: {
401
+ type: 'string',
402
+ },
403
+ unionSpacing: {
404
+ type: 'string',
405
+ },
406
+ },
407
+ type: 'object',
408
+ },
409
+ ],
410
+ type: 'suggestion',
411
+ },
412
+ });
package/src/rules.d.ts CHANGED
@@ -756,6 +756,25 @@ export interface Rules {
756
756
  }
757
757
  ];
758
758
 
759
+ "jsdoc/type-formatting":
760
+ | []
761
+ | [
762
+ {
763
+ arrayBrackets?: "angle" | "square";
764
+ enableFixer?: boolean;
765
+ genericDot?: boolean;
766
+ objectFieldIndent?: string;
767
+ objectFieldQuote?: "double" | "single" | null;
768
+ objectFieldSeparator?: "comma" | "comma-and-linebreak" | "linebreak" | "semicolon" | "semicolon-and-linebreak";
769
+ objectFieldSeparatorTrailingPunctuation?: boolean;
770
+ propertyQuotes?: "double" | "single" | null;
771
+ separatorForSingleObjectField?: boolean;
772
+ stringQuotes?: "double" | "single";
773
+ typeBracketSpacing?: string;
774
+ unionSpacing?: string;
775
+ }
776
+ ];
777
+
759
778
  "jsdoc/valid-types":
760
779
  | []
761
780
  | [