eslint-plugin-jsdoc 60.1.1 → 60.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.
@@ -0,0 +1,452 @@
1
+ import iterateJsdoc, {
2
+ parseComment,
3
+ } from '../iterateJsdoc.js';
4
+ import {
5
+ commentParserToESTree,
6
+ estreeToString,
7
+ // getJSDocComment,
8
+ parse as parseType,
9
+ stringify,
10
+ traverse,
11
+ tryParse as tryParseType,
12
+ } from '@es-joy/jsdoccomment';
13
+ import {
14
+ parseImportsExports,
15
+ } from 'parse-imports-exports';
16
+
17
+ export default iterateJsdoc(({
18
+ context,
19
+ jsdoc,
20
+ settings,
21
+ sourceCode,
22
+ utils,
23
+ }) => {
24
+ const {
25
+ mode,
26
+ } = settings;
27
+
28
+ const {
29
+ enableFixer = true,
30
+ exemptTypedefs = true,
31
+ outputType = 'namespaced-import',
32
+ } = context.options[0] || {};
33
+
34
+ const allComments = sourceCode.getAllComments();
35
+ const comments = allComments
36
+ .filter((comment) => {
37
+ return (/^\*(?!\*)/v).test(comment.value);
38
+ })
39
+ .map((commentNode) => {
40
+ return commentParserToESTree(
41
+ parseComment(commentNode, ''), mode === 'permissive' ? 'typescript' : mode,
42
+ );
43
+ });
44
+
45
+ const typedefs = comments
46
+ .flatMap((doc) => {
47
+ return doc.tags.filter(({
48
+ tag,
49
+ }) => {
50
+ return utils.isNamepathDefiningTag(tag);
51
+ });
52
+ });
53
+
54
+ const imports = comments
55
+ .flatMap((doc) => {
56
+ return doc.tags.filter(({
57
+ tag,
58
+ }) => {
59
+ return tag === 'import';
60
+ });
61
+ }).map((tag) => {
62
+ // Causes problems with stringification otherwise
63
+ tag.delimiter = '';
64
+ return tag;
65
+ });
66
+
67
+ /**
68
+ * @param {import('@es-joy/jsdoccomment').JsdocTagWithInline} tag
69
+ */
70
+ const iterateInlineImports = (tag) => {
71
+ const potentialType = tag.type;
72
+ let parsedType;
73
+ try {
74
+ parsedType = mode === 'permissive' ?
75
+ tryParseType(/** @type {string} */ (potentialType)) :
76
+ parseType(/** @type {string} */ (potentialType), mode);
77
+ } catch {
78
+ return;
79
+ }
80
+
81
+ traverse(parsedType, (nde, parentNode) => {
82
+ // @ts-expect-error Adding our own property for use below
83
+ nde.parentNode = parentNode;
84
+ });
85
+
86
+ traverse(parsedType, (nde) => {
87
+ const {
88
+ element,
89
+ type,
90
+ } = /** @type {import('jsdoc-type-pratt-parser').ImportResult} */ (nde);
91
+ if (type !== 'JsdocTypeImport') {
92
+ return;
93
+ }
94
+
95
+ let currentNode = nde;
96
+
97
+ /** @type {string[]} */
98
+ const pathSegments = [];
99
+
100
+ /** @type {import('jsdoc-type-pratt-parser').NamePathResult[]} */
101
+ const nodes = [];
102
+
103
+ /** @type {string[]} */
104
+ const extraPathSegments = [];
105
+
106
+ /** @type {(import('jsdoc-type-pratt-parser').QuoteStyle|undefined)[]} */
107
+ const quotes = [];
108
+
109
+ const propertyOrBrackets = /** @type {import('jsdoc-type-pratt-parser').NamePathResult['pathType'][]} */ ([]);
110
+
111
+ // @ts-expect-error Referencing our own property added above
112
+ while (currentNode && currentNode.parentNode) {
113
+ // @ts-expect-error Referencing our own property added above
114
+ currentNode = currentNode.parentNode;
115
+ /* c8 ignore next 3 -- Guard */
116
+ if (currentNode.type !== 'JsdocTypeNamePath') {
117
+ break;
118
+ }
119
+
120
+ pathSegments.unshift(currentNode.right.value);
121
+ nodes.unshift(currentNode);
122
+ propertyOrBrackets.unshift(currentNode.pathType);
123
+ quotes.unshift(currentNode.right.meta.quote);
124
+ }
125
+
126
+ /**
127
+ * @param {string} matchingName
128
+ * @param {string[]} extrPathSegments
129
+ */
130
+ const getFixer = (matchingName, extrPathSegments) => {
131
+ return () => {
132
+ /** @type {import('jsdoc-type-pratt-parser').NamePathResult|undefined} */
133
+ let node = nodes.at(0);
134
+ if (!node) {
135
+ // Not really a NamePathResult, but will be converted later anyways
136
+ node = /** @type {import('jsdoc-type-pratt-parser').NamePathResult} */ (
137
+ /** @type {unknown} */
138
+ (nde)
139
+ );
140
+ }
141
+
142
+ const keys = /** @type {(keyof import('jsdoc-type-pratt-parser').NamePathResult)[]} */ (
143
+ Object.keys(node)
144
+ );
145
+
146
+ for (const key of keys) {
147
+ delete node[key];
148
+ }
149
+
150
+ if (extrPathSegments.length) {
151
+ let newNode = /** @type {import('jsdoc-type-pratt-parser').NamePathResult} */ (
152
+ /** @type {unknown} */
153
+ (node)
154
+ );
155
+ while (extrPathSegments.length && newNode) {
156
+ newNode.type = 'JsdocTypeNamePath';
157
+ newNode.right = {
158
+ meta: {
159
+ quote: quotes.shift(),
160
+ },
161
+ type: 'JsdocTypeProperty',
162
+ value: /** @type {string} */ (extrPathSegments.shift()),
163
+ };
164
+
165
+ newNode.pathType = /** @type {import('jsdoc-type-pratt-parser').NamePathResult['pathType']} */ (
166
+ propertyOrBrackets.shift()
167
+ );
168
+ // @ts-expect-error Temporary
169
+ newNode.left = {};
170
+ newNode = /** @type {import('jsdoc-type-pratt-parser').NamePathResult} */ (
171
+ newNode.left
172
+ );
173
+ }
174
+
175
+ const nameNode = /** @type {import('jsdoc-type-pratt-parser').NameResult} */ (
176
+ /** @type {unknown} */
177
+ (newNode)
178
+ );
179
+ nameNode.type = 'JsdocTypeName';
180
+ nameNode.value = matchingName;
181
+ } else {
182
+ const newNode = /** @type {import('jsdoc-type-pratt-parser').NameResult} */ (
183
+ /** @type {unknown} */
184
+ (node)
185
+ );
186
+ newNode.type = 'JsdocTypeName';
187
+ newNode.value = matchingName;
188
+ }
189
+
190
+ for (const src of tag.source) {
191
+ if (src.tokens.type) {
192
+ src.tokens.type = `{${stringify(parsedType)}}`;
193
+ break;
194
+ }
195
+ }
196
+ };
197
+ };
198
+
199
+ /** @type {string[]} */
200
+ let unusedPathSegments = [];
201
+
202
+ const findMatchingTypedef = () => {
203
+ // Don't want typedefs to find themselves
204
+ if (!exemptTypedefs) {
205
+ return undefined;
206
+ }
207
+
208
+ const pthSegments = [
209
+ ...pathSegments,
210
+ ];
211
+ return typedefs.find((typedef) => {
212
+ let typedefNode = typedef.parsedType;
213
+ let namepathMatch;
214
+ while (typedefNode && typedefNode.type === 'JsdocTypeNamePath') {
215
+ const pathSegment = pthSegments.shift();
216
+ if (!pathSegment) {
217
+ namepathMatch = false;
218
+ break;
219
+ }
220
+
221
+ if (typedefNode.right.value !== pathSegment) {
222
+ if (namepathMatch === true) {
223
+ // It stopped matching, so stop
224
+ break;
225
+ }
226
+
227
+ extraPathSegments.push(pathSegment);
228
+ namepathMatch = false;
229
+ continue;
230
+ }
231
+
232
+ namepathMatch = true;
233
+
234
+ unusedPathSegments = pthSegments;
235
+
236
+ typedefNode = typedefNode.left;
237
+ }
238
+
239
+ return namepathMatch &&
240
+ // `import('eslint')` matches
241
+ typedefNode &&
242
+ typedefNode.type === 'JsdocTypeImport' &&
243
+ typedefNode.element.value === element.value;
244
+ });
245
+ };
246
+
247
+ // Check @typedef's first as should be longest match, allowing
248
+ // for shorter abbreviations
249
+ const matchingTypedef = findMatchingTypedef();
250
+ if (matchingTypedef) {
251
+ utils.reportJSDoc(
252
+ 'Inline `import()` found; using `@typedef`',
253
+ tag,
254
+ enableFixer ? getFixer(matchingTypedef.name, [
255
+ ...extraPathSegments,
256
+ ...unusedPathSegments.slice(-1),
257
+ ...unusedPathSegments.slice(0, -1),
258
+ ]) : null,
259
+ );
260
+ return;
261
+ }
262
+
263
+ const findMatchingImport = () => {
264
+ for (const imprt of imports) {
265
+ const parsedImport = parseImportsExports(
266
+ estreeToString(imprt).replace(/^\s*@/v, '').trim(),
267
+ );
268
+
269
+ const namedImportsModuleSpecifier = Object.keys(parsedImport.namedImports || {})[0];
270
+
271
+ const namedImports = Object.values(parsedImport.namedImports || {})[0]?.[0];
272
+ const namedImportNames = (namedImports && namedImports.names && Object.keys(namedImports.names)) ?? [];
273
+
274
+ const namespaceImports = Object.values(parsedImport.namespaceImports || {})[0]?.[0];
275
+
276
+ const namespaceImportsDefault = namespaceImports && namespaceImports.default;
277
+ const namespaceImportsNamespace = namespaceImports && namespaceImports.namespace;
278
+ const namespaceImportsModuleSpecifier = Object.keys(parsedImport.namespaceImports || {})[0];
279
+
280
+ const lastPathSegment = pathSegments.at(-1);
281
+
282
+ if (
283
+ (namespaceImportsDefault &&
284
+ namespaceImportsModuleSpecifier === element.value) ||
285
+ (element.value === namedImportsModuleSpecifier && (
286
+ (lastPathSegment && namedImportNames.includes(lastPathSegment)) ||
287
+ lastPathSegment === 'default'
288
+ )) ||
289
+ (namespaceImportsNamespace &&
290
+ namespaceImportsModuleSpecifier === element.value)
291
+ ) {
292
+ return {
293
+ namedImportNames,
294
+ namedImports,
295
+ namedImportsModuleSpecifier,
296
+ namespaceImports,
297
+ namespaceImportsDefault,
298
+ namespaceImportsModuleSpecifier,
299
+ namespaceImportsNamespace,
300
+ };
301
+ }
302
+ }
303
+
304
+ return undefined;
305
+ };
306
+
307
+ const matchingImport = findMatchingImport();
308
+ if (matchingImport) {
309
+ const {
310
+ namedImportNames,
311
+ namedImports,
312
+ namedImportsModuleSpecifier,
313
+ namespaceImportsNamespace,
314
+ } = matchingImport;
315
+ if (!namedImportNames.length && namedImportsModuleSpecifier && namedImports.default) {
316
+ utils.reportJSDoc(
317
+ 'Inline `import()` found; prefer `@import`',
318
+ tag,
319
+ enableFixer ? getFixer(namedImports.default, []) : null,
320
+ );
321
+ return;
322
+ }
323
+
324
+ const lastPthSegment = pathSegments.at(-1);
325
+ if (lastPthSegment && namedImportNames.includes(lastPthSegment)) {
326
+ utils.reportJSDoc(
327
+ 'Inline `import()` found; prefer `@import`',
328
+ tag,
329
+ enableFixer ? getFixer(lastPthSegment, pathSegments.slice(0, -1)) : null,
330
+ );
331
+ return;
332
+ }
333
+
334
+ if (namespaceImportsNamespace) {
335
+ utils.reportJSDoc(
336
+ 'Inline `import()` found; prefer `@import`',
337
+ tag,
338
+ enableFixer ? getFixer(namespaceImportsNamespace, [
339
+ ...pathSegments,
340
+ ]) : null,
341
+ );
342
+ return;
343
+ }
344
+ }
345
+
346
+ if (!pathSegments.length) {
347
+ utils.reportJSDoc(
348
+ 'Inline `import()` found; prefer `@import`',
349
+ tag,
350
+ enableFixer ? (fixer) => {
351
+ getFixer(element.value, [])();
352
+
353
+ const programNode = sourceCode.getNodeByRangeIndex(0);
354
+ return fixer.insertTextBefore(
355
+ /** @type {import('estree').Program} */ (programNode),
356
+ `/** @import * as ${element.value} from '${element.value}'; */`,
357
+ );
358
+ } : null,
359
+ );
360
+ return;
361
+ }
362
+
363
+ const lstPathSegment = pathSegments.at(-1);
364
+ if (lstPathSegment && lstPathSegment === 'default') {
365
+ utils.reportJSDoc(
366
+ 'Inline `import()` found; prefer `@import`',
367
+ tag,
368
+ enableFixer ? (fixer) => {
369
+ getFixer(element.value, [])();
370
+
371
+ const programNode = sourceCode.getNodeByRangeIndex(0);
372
+ return fixer.insertTextBefore(
373
+ /** @type {import('estree').Program} */ (programNode),
374
+ `/** @import ${element.value} from '${element.value}'; */`,
375
+ );
376
+ } : null,
377
+ );
378
+ return;
379
+ }
380
+
381
+ utils.reportJSDoc(
382
+ 'Inline `import()` found; prefer `@import`',
383
+ tag,
384
+ enableFixer ? (fixer) => {
385
+ if (outputType === 'namespaced-import') {
386
+ getFixer(element.value, [
387
+ ...pathSegments,
388
+ ])();
389
+ } else {
390
+ getFixer(
391
+ /** @type {string} */ (pathSegments.at(-1)),
392
+ pathSegments.slice(0, -1),
393
+ )();
394
+ }
395
+
396
+ const programNode = sourceCode.getNodeByRangeIndex(0);
397
+ return fixer.insertTextBefore(
398
+ /** @type {import('estree').Program} */ (programNode),
399
+ outputType === 'namespaced-import' ?
400
+ `/** @import * as ${element.value} from '${element.value}'; */` :
401
+ `/** @import { ${pathSegments.at(-1)} } from '${element.value}'; */`,
402
+ );
403
+ } : null,
404
+ );
405
+ });
406
+ };
407
+
408
+ for (const tag of jsdoc.tags) {
409
+ const mightHaveTypePosition = utils.tagMightHaveTypePosition(tag.tag);
410
+ const hasTypePosition = mightHaveTypePosition === true && Boolean(tag.type);
411
+ if (hasTypePosition && (!exemptTypedefs || !utils.isNamepathDefiningTag(tag.tag))) {
412
+ iterateInlineImports(tag);
413
+ }
414
+ }
415
+ }, {
416
+ iterateAllJsdocs: true,
417
+ meta: {
418
+ docs: {
419
+ description: 'Prefer `@import` tags to inline `import()` statements.',
420
+ url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/prefer-import-tag.md#repos-sticky-header',
421
+ },
422
+ fixable: 'code',
423
+ schema: [
424
+ {
425
+ additionalProperties: false,
426
+ properties: {
427
+ enableFixer: {
428
+ description: 'Whether or not to enable the fixer to add `@import` tags.',
429
+ type: 'boolean',
430
+ },
431
+ exemptTypedefs: {
432
+ description: 'Whether to allow `import()` statements within `@typedef`',
433
+ type: 'boolean',
434
+ },
435
+
436
+ // We might add `typedef` and `typedef-local-only`, but also raises
437
+ // question of how deep the generated typedef should be
438
+ outputType: {
439
+ description: 'What kind of `@import` to generate when no matching `@typedef` or `@import` is found',
440
+ enum: [
441
+ 'named-import',
442
+ 'namespaced-import',
443
+ ],
444
+ type: 'string',
445
+ },
446
+ },
447
+ type: 'object',
448
+ },
449
+ ],
450
+ type: 'suggestion',
451
+ },
452
+ });
package/src/rules.d.ts CHANGED
@@ -1250,6 +1250,10 @@ export interface Rules {
1250
1250
  | []
1251
1251
  | [
1252
1252
  {
1253
+ /**
1254
+ * Whether to check typedefs for use within the file
1255
+ */
1256
+ checkUsedTypedefs?: boolean;
1253
1257
  /**
1254
1258
  * This array can be populated to indicate other types which
1255
1259
  * are automatically considered as defined (in addition to globals, etc.).
@@ -1259,7 +1263,7 @@ export interface Rules {
1259
1263
  /**
1260
1264
  * Whether to disable reporting of errors. Defaults to
1261
1265
  * `false`. This may be set to `true` in order to take advantage of only
1262
- * marking defined variables as used.
1266
+ * marking defined variables as used or checking used typedefs.
1263
1267
  */
1264
1268
  disableReporting?: boolean;
1265
1269
  /**
@@ -1272,6 +1276,26 @@ export interface Rules {
1272
1276
  }
1273
1277
  ];
1274
1278
 
1279
+ /** Prefer `@import` tags to inline `import()` statements. */
1280
+ "jsdoc/prefer-import-tag":
1281
+ | []
1282
+ | [
1283
+ {
1284
+ /**
1285
+ * Whether or not to enable the fixer to add `@import` tags.
1286
+ */
1287
+ enableFixer?: boolean;
1288
+ /**
1289
+ * Whether to allow `import()` statements within `@typedef`
1290
+ */
1291
+ exemptTypedefs?: boolean;
1292
+ /**
1293
+ * What kind of `@import` to generate when no matching `@typedef` or `@import` is found
1294
+ */
1295
+ outputType?: "named-import" | "namespaced-import";
1296
+ }
1297
+ ];
1298
+
1275
1299
  /** Reports use of `any` or `*` type */
1276
1300
  "jsdoc/reject-any-type": [];
1277
1301