|
7 | 7 | findPluginByParser, |
8 | 8 | isDefaultTag, |
9 | 9 | } from "./utils.js"; |
10 | | -import { DESCRIPTION, PARAM, RETURNS, EXAMPLE } from "./tags.js"; |
| 10 | +import { DESCRIPTION, PARAM, RETURNS, EXAMPLE, IMPORT } from "./tags.js"; |
11 | 11 | import { |
12 | 12 | TAGS_DESCRIPTION_NEEDED, |
13 | 13 | TAGS_GROUP_HEAD, |
@@ -255,61 +255,122 @@ function sortTags( |
255 | 255 | ): Spec[] { |
256 | 256 | let canGroupNextTags = false; |
257 | 257 | let shouldSortAgain = false; |
| 258 | + const importDetailsBySource: { [tag: string]: ImportDetails[] } = {}; |
| 259 | + const importSourceByDescription: { [description: string]: string } = {}; |
| 260 | + |
| 261 | + const tagGroups = tags.reduce<Spec[][]>((tagGroups, cur) => { |
| 262 | + if ( |
| 263 | + tagGroups.length === 0 || |
| 264 | + (TAGS_GROUP_HEAD.includes(cur.tag) && canGroupNextTags) |
| 265 | + ) { |
| 266 | + canGroupNextTags = false; |
| 267 | + tagGroups.push([]); |
| 268 | + } |
258 | 269 |
|
259 | | - tags = tags |
260 | | - .reduce<Spec[][]>((tagGroups, cur) => { |
261 | | - if ( |
262 | | - tagGroups.length === 0 || |
263 | | - (TAGS_GROUP_HEAD.includes(cur.tag) && canGroupNextTags) |
264 | | - ) { |
265 | | - canGroupNextTags = false; |
266 | | - tagGroups.push([]); |
267 | | - } |
268 | | - if (TAGS_GROUP_CONDITION.includes(cur.tag)) { |
269 | | - canGroupNextTags = true; |
270 | | - } |
271 | | - tagGroups[tagGroups.length - 1].push(cur); |
272 | | - |
273 | | - return tagGroups; |
274 | | - }, []) |
275 | | - .flatMap((tagGroup, index, array) => { |
276 | | - // sort tags within groups |
277 | | - tagGroup.sort((a, b) => { |
278 | | - if ( |
279 | | - paramsOrder && |
280 | | - paramsOrder.length > 1 && |
281 | | - a.tag === PARAM && |
282 | | - b.tag === PARAM |
283 | | - ) { |
284 | | - const aIndex = paramsOrder.indexOf(a.name); |
285 | | - const bIndex = paramsOrder.indexOf(b.name); |
286 | | - if (aIndex > -1 && bIndex > -1) { |
287 | | - //sort params |
288 | | - return aIndex - bIndex; |
| 270 | + if (TAGS_GROUP_CONDITION.includes(cur.tag)) { |
| 271 | + canGroupNextTags = true; |
| 272 | + } |
| 273 | + |
| 274 | + if (options.jsdocFormatImports && cur.tag === IMPORT) { |
| 275 | + const importDetails = getImportDetails(cur); |
| 276 | + if (importDetails) { |
| 277 | + if (options.jsdocMergeImports) { |
| 278 | + const existingImport = importDetailsBySource[importDetails.src]; |
| 279 | + if (existingImport) { |
| 280 | + importDetailsBySource[importDetails.src].push(importDetails); |
| 281 | + // do not add duplicate import tags to tagGroups |
| 282 | + return tagGroups; |
289 | 283 | } |
290 | | - return 0; |
| 284 | + importDetailsBySource[importDetails.src] = [importDetails]; |
| 285 | + } else { |
| 286 | + writeImportDetailsToSpec(importDetails, options); |
| 287 | + importSourceByDescription[importDetails.spec.description] = |
| 288 | + importDetails.src; |
291 | 289 | } |
292 | | - return ( |
293 | | - getTagOrderWeight(a.tag, options) - getTagOrderWeight(b.tag, options) |
294 | | - ); |
295 | | - }); |
296 | | - |
297 | | - // add an empty line between groups |
298 | | - if (array.length - 1 !== index) { |
299 | | - tagGroup.push(SPACE_TAG_DATA); |
300 | 290 | } |
| 291 | + } |
| 292 | + |
| 293 | + tagGroups[tagGroups.length - 1].push(cur); |
| 294 | + |
| 295 | + return tagGroups; |
| 296 | + }, []); |
| 297 | + |
| 298 | + // Merge the import details for a given src into a printable tag description |
| 299 | + if (options.jsdocFormatImports && options.jsdocMergeImports) { |
| 300 | + Object.keys(importDetailsBySource).forEach((src) => { |
| 301 | + const importDetails = importDetailsBySource[src]; |
| 302 | + // the first spec is the only one added to tagGroups |
| 303 | + const firstImpSpec = importDetails[0].spec; |
| 304 | + const { defaultImport, namedImports } = importDetails.reduce( |
| 305 | + (prev, curr) => { |
| 306 | + prev.namedImports.push(...curr.namedImports); |
| 307 | + // NB: the last default import encountered will be the one used |
| 308 | + if (curr.defaultImport) prev.defaultImport = curr.defaultImport; |
| 309 | + return prev; |
| 310 | + }, |
| 311 | + { namedImports: [], defaultImport: undefined } as Pick< |
| 312 | + ImportDetails, |
| 313 | + "defaultImport" | "namedImports" |
| 314 | + >, |
| 315 | + ); |
301 | 316 |
|
| 317 | + writeImportDetailsToSpec( |
| 318 | + { src, defaultImport, namedImports, spec: firstImpSpec }, |
| 319 | + options, |
| 320 | + ); |
| 321 | + importSourceByDescription[firstImpSpec.description] = src; |
| 322 | + }); |
| 323 | + } |
| 324 | + |
| 325 | + tags = tagGroups.flatMap((tagGroup, index, array) => { |
| 326 | + // sort tags within groups |
| 327 | + tagGroup.sort((a, b) => { |
302 | 328 | if ( |
303 | | - index > 0 && |
304 | | - tagGroup[0]?.tag && |
305 | | - !TAGS_GROUP_HEAD.includes(tagGroup[0].tag) |
| 329 | + paramsOrder && |
| 330 | + paramsOrder.length > 1 && |
| 331 | + a.tag === PARAM && |
| 332 | + b.tag === PARAM |
306 | 333 | ) { |
307 | | - shouldSortAgain = true; |
| 334 | + const aIndex = paramsOrder.indexOf(a.name); |
| 335 | + const bIndex = paramsOrder.indexOf(b.name); |
| 336 | + if (aIndex > -1 && bIndex > -1) { |
| 337 | + //sort params |
| 338 | + return aIndex - bIndex; |
| 339 | + } |
| 340 | + return 0; |
| 341 | + } |
| 342 | + |
| 343 | + // sorts imports by source and places third party modes at the top |
| 344 | + if (options.jsdocFormatImports && a.tag === IMPORT && b.tag === IMPORT) { |
| 345 | + const aSrc = importSourceByDescription[a.description] ?? a.description; |
| 346 | + const bSrc = importSourceByDescription[b.description] ?? a.description; |
| 347 | + const aVal = aSrc.startsWith(".") ? 1 : 0; |
| 348 | + const bVal = bSrc.startsWith(".") ? 1 : 0; |
| 349 | + if (aVal === bVal) return aSrc.localeCompare(bSrc); |
| 350 | + return aVal - bVal; |
308 | 351 | } |
309 | 352 |
|
310 | | - return tagGroup; |
| 353 | + return ( |
| 354 | + getTagOrderWeight(a.tag, options) - getTagOrderWeight(b.tag, options) |
| 355 | + ); |
311 | 356 | }); |
312 | 357 |
|
| 358 | + // add an empty line between groups |
| 359 | + if (array.length - 1 !== index) { |
| 360 | + tagGroup.push(SPACE_TAG_DATA); |
| 361 | + } |
| 362 | + |
| 363 | + if ( |
| 364 | + index > 0 && |
| 365 | + tagGroup[0]?.tag && |
| 366 | + !TAGS_GROUP_HEAD.includes(tagGroup[0].tag) |
| 367 | + ) { |
| 368 | + shouldSortAgain = true; |
| 369 | + } |
| 370 | + |
| 371 | + return tagGroup; |
| 372 | + }); |
| 373 | + |
313 | 374 | return shouldSortAgain ? sortTags(tags, paramsOrder, options) : tags; |
314 | 375 | } |
315 | 376 |
|
@@ -615,3 +676,80 @@ function assignOptionalAndDefaultToName({ |
615 | 676 | default: default_, |
616 | 677 | }; |
617 | 678 | } |
| 679 | + |
| 680 | +type ImportDetails = { |
| 681 | + /** the spec associated with this import tag */ |
| 682 | + spec: Spec; |
| 683 | + /** the source of the module that types were imported from */ |
| 684 | + src: string; |
| 685 | + defaultImport?: string; |
| 686 | + /** the types that were imported */ |
| 687 | + namedImports: { |
| 688 | + name: string; |
| 689 | + /** the alias assigned to the type (EX: alias of "B as B0" is "B0") */ |
| 690 | + alias?: string; |
| 691 | + }[]; |
| 692 | +}; |
| 693 | + |
| 694 | +/** |
| 695 | + * Writes the import details given to the description field of the spec |
| 696 | + */ |
| 697 | +function writeImportDetailsToSpec( |
| 698 | + importDetails: ImportDetails, |
| 699 | + options: AllOptions, |
| 700 | +) { |
| 701 | + const { defaultImport, namedImports, src, spec } = importDetails; |
| 702 | + |
| 703 | + // sort the import details |
| 704 | + namedImports.sort((a, b) => |
| 705 | + (a.alias ?? a.name).localeCompare(b.alias ?? b.name), |
| 706 | + ); |
| 707 | + |
| 708 | + // write the merged import details to the spec description |
| 709 | + const importClauses = []; |
| 710 | + if (defaultImport) importClauses.push(defaultImport); |
| 711 | + if (namedImports.length > 0) { |
| 712 | + const makeMultiLine = |
| 713 | + options.jsdocNamedImportLineSplitting && namedImports.length > 1; |
| 714 | + const typeString = namedImports |
| 715 | + .map((t) => { |
| 716 | + const val = t.alias ? `${t.name} as ${t.alias}` : `${t.name}`; |
| 717 | + return makeMultiLine ? ` ${val}` : val; |
| 718 | + }) |
| 719 | + .join(makeMultiLine ? ",\n" : ", "); |
| 720 | + const namedImportClause = makeMultiLine |
| 721 | + ? `{\n${typeString}\n}` |
| 722 | + : options.jsdocNamedImportPadding |
| 723 | + ? `{ ${typeString} }` |
| 724 | + : `{${typeString}}`; |
| 725 | + importClauses.push(namedImportClause); |
| 726 | + } |
| 727 | + spec.description = `${importClauses.join(", ")} from "${src}"`; |
| 728 | +} |
| 729 | + |
| 730 | +/** |
| 731 | + * Extracts the defaultImports, namedImports, and src associated with a given import tag. |
| 732 | + */ |
| 733 | +function getImportDetails(spec: Spec): ImportDetails | null { |
| 734 | + // step 1: capture the default import, named import clause, and src |
| 735 | + const match = spec.description.match( |
| 736 | + /([^\s\\,\\{\\}]+)?(?:[^\\{\\}]*)\{?([^\\{\\}]*)?\}?(?:\s+from\s+)[\\'\\"](\S+)[\\'\\"]/s, |
| 737 | + ); |
| 738 | + if (!match) return null; |
| 739 | + |
| 740 | + const defaultImport = match[1] || ""; |
| 741 | + const namedImportsClause = match[2] || ""; |
| 742 | + const src = match[3] || ""; |
| 743 | + |
| 744 | + // step 2: get all named imports from the named import section |
| 745 | + const typeMatches = namedImportsClause.matchAll( |
| 746 | + /([^\s\\,\\{\\}]+)(?:\s+as\s+)?([^\s\\,\\{\\}]+)?/g, |
| 747 | + ); |
| 748 | + |
| 749 | + const namedImports = []; |
| 750 | + for (const typeMatch of typeMatches) { |
| 751 | + namedImports.push({ name: typeMatch[1], alias: typeMatch[2] }); |
| 752 | + } |
| 753 | + |
| 754 | + return { spec, src, namedImports, defaultImport }; |
| 755 | +} |
0 commit comments