eslint-plugin-jsdoc 55.2.0 → 55.3.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.
- package/dist/generateRule.cjs +4 -4
- package/dist/generateRule.cjs.map +1 -1
- package/dist/index-cjs.cjs +3 -0
- package/dist/index-cjs.cjs.map +1 -1
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/rules/typeFormatting.cjs +289 -0
- package/dist/rules/typeFormatting.cjs.map +1 -0
- package/dist/rules/typeFormatting.d.ts +3 -0
- package/dist/rules.d.ts +22 -0
- package/package.json +1 -1
- package/src/index-cjs.js +3 -0
- package/src/index.js +3 -0
- package/src/rules/typeFormatting.js +348 -0
- package/src/rules.d.ts +22 -0
|
@@ -0,0 +1,348 @@
|
|
|
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 = null,
|
|
23
|
+
propertyQuotes = null,
|
|
24
|
+
separatorForSingleObjectField = false,
|
|
25
|
+
stringQuotes = 'single',
|
|
26
|
+
} = context.options[0] || {};
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
mode,
|
|
30
|
+
} = settings;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {import('@es-joy/jsdoccomment').JsdocTagWithInline} tag
|
|
34
|
+
*/
|
|
35
|
+
const checkTypeFormats = (tag) => {
|
|
36
|
+
const potentialType = tag.type;
|
|
37
|
+
let parsedType;
|
|
38
|
+
try {
|
|
39
|
+
parsedType = mode === 'permissive' ?
|
|
40
|
+
tryParseType(/** @type {string} */ (potentialType)) :
|
|
41
|
+
parseType(/** @type {string} */ (potentialType), mode);
|
|
42
|
+
} catch {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const fix = () => {
|
|
47
|
+
const typeLines = stringify(parsedType).split('\n');
|
|
48
|
+
const firstTypeLine = typeLines.shift();
|
|
49
|
+
const lastTypeLine = typeLines.pop();
|
|
50
|
+
|
|
51
|
+
const beginNameOrDescIdx = tag.source.findIndex(({
|
|
52
|
+
tokens,
|
|
53
|
+
}) => {
|
|
54
|
+
return tokens.name || tokens.description;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const nameAndDesc = tag.source.slice(beginNameOrDescIdx);
|
|
58
|
+
const initialNumber = tag.source[0].number;
|
|
59
|
+
const src = [
|
|
60
|
+
// Get inevitably present tag from first `tag.source`
|
|
61
|
+
{
|
|
62
|
+
number: initialNumber,
|
|
63
|
+
source: '',
|
|
64
|
+
tokens: {
|
|
65
|
+
...tag.source[0].tokens,
|
|
66
|
+
...(typeLines.length || lastTypeLine ? {
|
|
67
|
+
end: '',
|
|
68
|
+
name: '',
|
|
69
|
+
postName: '',
|
|
70
|
+
postType: '',
|
|
71
|
+
} : {}),
|
|
72
|
+
type: '{' + firstTypeLine + (!typeLines.length && lastTypeLine === undefined ? '}' : ''),
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
// Get any intervening type lines
|
|
76
|
+
...(typeLines.length ? typeLines.map((typeLine, idx) => {
|
|
77
|
+
return {
|
|
78
|
+
number: initialNumber + idx + 1,
|
|
79
|
+
source: '',
|
|
80
|
+
tokens: {
|
|
81
|
+
// Grab any delimiter info from first item
|
|
82
|
+
...tag.source[0].tokens,
|
|
83
|
+
delimiter: tag.source[0].tokens.delimiter === '/**' ? '*' : tag.source[0].tokens.delimiter,
|
|
84
|
+
end: '',
|
|
85
|
+
name: '',
|
|
86
|
+
postName: '',
|
|
87
|
+
postTag: '',
|
|
88
|
+
postType: '',
|
|
89
|
+
start: indent + ' ',
|
|
90
|
+
tag: '',
|
|
91
|
+
type: typeLine,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}) : []),
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
// Merge any final type line and name and description
|
|
98
|
+
if (
|
|
99
|
+
// Name and description may be already included if present with the tag
|
|
100
|
+
beginNameOrDescIdx > 0
|
|
101
|
+
) {
|
|
102
|
+
src.push({
|
|
103
|
+
number: src.length + 1,
|
|
104
|
+
source: '',
|
|
105
|
+
tokens: {
|
|
106
|
+
...nameAndDesc[0].tokens,
|
|
107
|
+
type: lastTypeLine + '}',
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
if (
|
|
112
|
+
// Get any remaining description lines
|
|
113
|
+
nameAndDesc.length > 1
|
|
114
|
+
) {
|
|
115
|
+
src.push(
|
|
116
|
+
...nameAndDesc.slice(1).map(({
|
|
117
|
+
source,
|
|
118
|
+
tokens,
|
|
119
|
+
}, idx) => {
|
|
120
|
+
return {
|
|
121
|
+
number: src.length + idx + 2,
|
|
122
|
+
source,
|
|
123
|
+
tokens,
|
|
124
|
+
};
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
if (lastTypeLine) {
|
|
130
|
+
src.push({
|
|
131
|
+
number: src.length + 1,
|
|
132
|
+
source: '',
|
|
133
|
+
tokens: {
|
|
134
|
+
...nameAndDesc[0].tokens,
|
|
135
|
+
delimiter: nameAndDesc[0].tokens.delimiter === '/**' ? '*' : nameAndDesc[0].tokens.delimiter,
|
|
136
|
+
postTag: '',
|
|
137
|
+
start: indent + ' ',
|
|
138
|
+
tag: '',
|
|
139
|
+
type: lastTypeLine + '}',
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (
|
|
145
|
+
// Get any remaining description lines
|
|
146
|
+
nameAndDesc.length > 1
|
|
147
|
+
) {
|
|
148
|
+
src.push(
|
|
149
|
+
...nameAndDesc.slice(1).map(({
|
|
150
|
+
source,
|
|
151
|
+
tokens,
|
|
152
|
+
}, idx) => {
|
|
153
|
+
return {
|
|
154
|
+
number: src.length + idx + 2,
|
|
155
|
+
source,
|
|
156
|
+
tokens,
|
|
157
|
+
};
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
tag.source = src;
|
|
164
|
+
|
|
165
|
+
// Properly rewire `jsdoc.source`
|
|
166
|
+
const firstTagIdx = jsdoc.source.findIndex(({
|
|
167
|
+
tokens: {
|
|
168
|
+
tag: tg,
|
|
169
|
+
},
|
|
170
|
+
}) => {
|
|
171
|
+
return tg;
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
jsdoc.source = [
|
|
175
|
+
...jsdoc.source.slice(0, firstTagIdx),
|
|
176
|
+
...jsdoc.tags.flatMap(({
|
|
177
|
+
source,
|
|
178
|
+
}) => {
|
|
179
|
+
return source;
|
|
180
|
+
}),
|
|
181
|
+
];
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/** @type {string[]} */
|
|
185
|
+
const errorMessages = [];
|
|
186
|
+
let needToReport = false;
|
|
187
|
+
traverse(parsedType, (nde) => {
|
|
188
|
+
let typeFound = true;
|
|
189
|
+
let errorMessage = '';
|
|
190
|
+
const initialType = stringify(
|
|
191
|
+
/** @type {import('jsdoc-type-pratt-parser').RootResult} */ (nde),
|
|
192
|
+
);
|
|
193
|
+
switch (nde.type) {
|
|
194
|
+
case 'JsdocTypeGeneric': {
|
|
195
|
+
const typeNode = /** @type {import('jsdoc-type-pratt-parser').GenericResult} */ (nde);
|
|
196
|
+
if ('value' in typeNode.left && typeNode.left.value === 'Array') {
|
|
197
|
+
typeNode.meta.brackets = arrayBrackets;
|
|
198
|
+
errorMessage = `Array bracket style should be ${arrayBrackets}`;
|
|
199
|
+
} else {
|
|
200
|
+
typeNode.meta.dot = genericDot;
|
|
201
|
+
errorMessage = `Dot usage should be ${genericDot}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
case 'JsdocTypeObject': {
|
|
208
|
+
const typeNode = /** @type {import('jsdoc-type-pratt-parser').ObjectResult} */ (nde);
|
|
209
|
+
typeNode.meta.separator = objectFieldSeparator ?? undefined;
|
|
210
|
+
typeNode.meta.separatorForSingleObjectField = separatorForSingleObjectField;
|
|
211
|
+
typeNode.meta.propertyIndent = objectFieldIndent;
|
|
212
|
+
errorMessage = `Inconsistent ${objectFieldSeparator} usage`;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
case 'JsdocTypeObjectField': {
|
|
217
|
+
const typeNode = /** @type {import('jsdoc-type-pratt-parser').ObjectFieldResult} */ (nde);
|
|
218
|
+
if (objectFieldQuote ||
|
|
219
|
+
(typeof typeNode.key === 'string' && !(/\s/v).test(typeNode.key))
|
|
220
|
+
) {
|
|
221
|
+
typeNode.meta.quote = objectFieldQuote ?? undefined;
|
|
222
|
+
errorMessage = `Inconsistent object field quotes ${objectFieldQuote}`;
|
|
223
|
+
} else {
|
|
224
|
+
typeFound = false;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
case 'JsdocTypeProperty': {
|
|
231
|
+
const typeNode = /** @type {import('jsdoc-type-pratt-parser').PropertyResult} */ (nde);
|
|
232
|
+
|
|
233
|
+
if (propertyQuotes ||
|
|
234
|
+
(typeof typeNode.value === 'string' && !(/\s/v).test(typeNode.value))
|
|
235
|
+
) {
|
|
236
|
+
typeNode.meta.quote = propertyQuotes ?? undefined;
|
|
237
|
+
errorMessage = `Inconsistent ${propertyQuotes} property quotes usage`;
|
|
238
|
+
} else {
|
|
239
|
+
typeFound = false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
case 'JsdocTypeStringValue': {
|
|
246
|
+
const typeNode = /** @type {import('jsdoc-type-pratt-parser').StringValueResult} */ (nde);
|
|
247
|
+
typeNode.meta.quote = stringQuotes;
|
|
248
|
+
errorMessage = `Inconsistent ${stringQuotes} string quotes usage`;
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
default:
|
|
253
|
+
typeFound = false;
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (typeFound) {
|
|
258
|
+
const convertedType = stringify(/** @type {import('jsdoc-type-pratt-parser').RootResult} */ (nde));
|
|
259
|
+
if (initialType !== convertedType) {
|
|
260
|
+
needToReport = true;
|
|
261
|
+
errorMessages.push(errorMessage);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
if (needToReport) {
|
|
267
|
+
for (const errorMessage of errorMessages) {
|
|
268
|
+
utils.reportJSDoc(
|
|
269
|
+
errorMessage, tag, enableFixer ? fix : null, true,
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const tags = utils.getPresentTags([
|
|
276
|
+
'param',
|
|
277
|
+
'returns',
|
|
278
|
+
]);
|
|
279
|
+
for (const tag of tags) {
|
|
280
|
+
checkTypeFormats(tag);
|
|
281
|
+
}
|
|
282
|
+
}, {
|
|
283
|
+
iterateAllJsdocs: true,
|
|
284
|
+
meta: {
|
|
285
|
+
docs: {
|
|
286
|
+
description: 'Formats JSDoc type values.',
|
|
287
|
+
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/type-formatting.md#repos-sticky-header',
|
|
288
|
+
},
|
|
289
|
+
fixable: 'code',
|
|
290
|
+
schema: [
|
|
291
|
+
{
|
|
292
|
+
additionalProperties: false,
|
|
293
|
+
properties: {
|
|
294
|
+
arrayBrackets: {
|
|
295
|
+
enum: [
|
|
296
|
+
'angle',
|
|
297
|
+
'square',
|
|
298
|
+
],
|
|
299
|
+
},
|
|
300
|
+
enableFixer: {
|
|
301
|
+
type: 'boolean',
|
|
302
|
+
},
|
|
303
|
+
genericDot: {
|
|
304
|
+
type: 'boolean',
|
|
305
|
+
},
|
|
306
|
+
objectFieldIndent: {
|
|
307
|
+
type: 'string',
|
|
308
|
+
},
|
|
309
|
+
objectFieldQuote: {
|
|
310
|
+
enum: [
|
|
311
|
+
'double',
|
|
312
|
+
'single',
|
|
313
|
+
null,
|
|
314
|
+
],
|
|
315
|
+
},
|
|
316
|
+
objectFieldSeparator: {
|
|
317
|
+
enum: [
|
|
318
|
+
'comma',
|
|
319
|
+
'comma-and-linebreak',
|
|
320
|
+
'linebreak',
|
|
321
|
+
'semicolon',
|
|
322
|
+
'semicolon-and-linebreak',
|
|
323
|
+
null,
|
|
324
|
+
],
|
|
325
|
+
},
|
|
326
|
+
propertyQuotes: {
|
|
327
|
+
enum: [
|
|
328
|
+
'double',
|
|
329
|
+
'single',
|
|
330
|
+
null,
|
|
331
|
+
],
|
|
332
|
+
},
|
|
333
|
+
separatorForSingleObjectField: {
|
|
334
|
+
type: 'boolean',
|
|
335
|
+
},
|
|
336
|
+
stringQuotes: {
|
|
337
|
+
enum: [
|
|
338
|
+
'double',
|
|
339
|
+
'single',
|
|
340
|
+
],
|
|
341
|
+
},
|
|
342
|
+
},
|
|
343
|
+
type: 'object',
|
|
344
|
+
},
|
|
345
|
+
],
|
|
346
|
+
type: 'suggestion',
|
|
347
|
+
},
|
|
348
|
+
});
|
package/src/rules.d.ts
CHANGED
|
@@ -756,6 +756,28 @@ 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?:
|
|
769
|
+
| "comma"
|
|
770
|
+
| "comma-and-linebreak"
|
|
771
|
+
| "linebreak"
|
|
772
|
+
| "semicolon"
|
|
773
|
+
| "semicolon-and-linebreak"
|
|
774
|
+
| null;
|
|
775
|
+
propertyQuotes?: "double" | "single" | null;
|
|
776
|
+
separatorForSingleObjectField?: boolean;
|
|
777
|
+
stringQuotes?: "double" | "single";
|
|
778
|
+
}
|
|
779
|
+
];
|
|
780
|
+
|
|
759
781
|
"jsdoc/valid-types":
|
|
760
782
|
| []
|
|
761
783
|
| [
|