|
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,118 @@ function sortTags( |
255 | 255 | ): Spec[] { |
256 | 256 | let canGroupNextTags = false; |
257 | 257 | let shouldSortAgain = false; |
| 258 | + const importDetailsBySource: { [tag: string]: ImportDetails[] } = {}; |
| 259 | + |
| 260 | + const tagGroups = tags.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 | + } |
258 | 268 |
|
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; |
289 | | - } |
290 | | - return 0; |
291 | | - } |
292 | | - return ( |
293 | | - getTagOrderWeight(a.tag, options) - getTagOrderWeight(b.tag, options) |
294 | | - ); |
295 | | - }); |
| 269 | + if (TAGS_GROUP_CONDITION.includes(cur.tag)) { |
| 270 | + canGroupNextTags = true; |
| 271 | + } |
296 | 272 |
|
297 | | - // add an empty line between groups |
298 | | - if (array.length - 1 !== index) { |
299 | | - tagGroup.push(SPACE_TAG_DATA); |
| 273 | + if (cur.tag === IMPORT) { |
| 274 | + const importDetails = getImportDetails(cur); |
| 275 | + if (importDetails) { |
| 276 | + const existingImport = importDetailsBySource[importDetails.src]; |
| 277 | + if (existingImport) { |
| 278 | + importDetailsBySource[importDetails.src].push(importDetails); |
| 279 | + // do not add duplicate import tags to tagGroups |
| 280 | + return tagGroups; |
| 281 | + } |
| 282 | + importDetailsBySource[importDetails.src] = [importDetails]; |
300 | 283 | } |
| 284 | + } |
| 285 | + |
| 286 | + tagGroups[tagGroups.length - 1].push(cur); |
| 287 | + |
| 288 | + return tagGroups; |
| 289 | + }, []); |
| 290 | + |
| 291 | + // Merge the import details for a given src into a printable tag description |
| 292 | + Object.keys(importDetailsBySource).forEach((src) => { |
| 293 | + const importDetails = importDetailsBySource[src]; |
| 294 | + // the first spec is the only one added to tagGroups |
| 295 | + const firstImpSpec = importDetails[0].spec; |
| 296 | + const { defaultImport, namedImports } = importDetails.reduce( |
| 297 | + (prev, curr) => { |
| 298 | + prev.namedImports.push(...curr.namedImports); |
| 299 | + // NB: the last default import encountered will be the one used |
| 300 | + if (curr.defaultImport) prev.defaultImport = curr.defaultImport; |
| 301 | + return prev; |
| 302 | + }, |
| 303 | + { namedImports: [], defaultImport: undefined } as Pick< |
| 304 | + ImportDetails, |
| 305 | + "defaultImport" | "namedImports" |
| 306 | + >, |
| 307 | + ); |
| 308 | + // sort the import details |
| 309 | + namedImports.sort((a, b) => |
| 310 | + (a.alias ?? a.name).localeCompare(b.alias ?? b.name), |
| 311 | + ); |
| 312 | + |
| 313 | + // write the merged import details to the spec description |
| 314 | + const importClauses = []; |
| 315 | + if (defaultImport) importClauses.push(defaultImport); |
| 316 | + if (namedImports.length > 0) { |
| 317 | + const makeMultiLine = namedImports.length > 1; |
| 318 | + const typeString = namedImports |
| 319 | + .map((t) => { |
| 320 | + const val = t.alias ? `${t.name} as ${t.alias}` : `${t.name}`; |
| 321 | + return makeMultiLine ? ` ${val}` : val; |
| 322 | + }) |
| 323 | + .join(",\n"); |
| 324 | + const namedImportClause = makeMultiLine |
| 325 | + ? `{\n${typeString}\n}` |
| 326 | + : `{${typeString}}`; |
| 327 | + importClauses.push(namedImportClause); |
| 328 | + } |
| 329 | + firstImpSpec.description = `${importClauses.join(", ")} from "${src}"`; |
| 330 | + }); |
301 | 331 |
|
| 332 | + tags = tagGroups.flatMap((tagGroup, index, array) => { |
| 333 | + // sort tags within groups |
| 334 | + tagGroup.sort((a, b) => { |
302 | 335 | if ( |
303 | | - index > 0 && |
304 | | - tagGroup[0]?.tag && |
305 | | - !TAGS_GROUP_HEAD.includes(tagGroup[0].tag) |
| 336 | + paramsOrder && |
| 337 | + paramsOrder.length > 1 && |
| 338 | + a.tag === PARAM && |
| 339 | + b.tag === PARAM |
306 | 340 | ) { |
307 | | - shouldSortAgain = true; |
| 341 | + const aIndex = paramsOrder.indexOf(a.name); |
| 342 | + const bIndex = paramsOrder.indexOf(b.name); |
| 343 | + if (aIndex > -1 && bIndex > -1) { |
| 344 | + //sort params |
| 345 | + return aIndex - bIndex; |
| 346 | + } |
| 347 | + return 0; |
308 | 348 | } |
309 | | - |
310 | | - return tagGroup; |
| 349 | + return ( |
| 350 | + getTagOrderWeight(a.tag, options) - getTagOrderWeight(b.tag, options) |
| 351 | + ); |
311 | 352 | }); |
312 | 353 |
|
| 354 | + // add an empty line between groups |
| 355 | + if (array.length - 1 !== index) { |
| 356 | + tagGroup.push(SPACE_TAG_DATA); |
| 357 | + } |
| 358 | + |
| 359 | + if ( |
| 360 | + index > 0 && |
| 361 | + tagGroup[0]?.tag && |
| 362 | + !TAGS_GROUP_HEAD.includes(tagGroup[0].tag) |
| 363 | + ) { |
| 364 | + shouldSortAgain = true; |
| 365 | + } |
| 366 | + |
| 367 | + return tagGroup; |
| 368 | + }); |
| 369 | + |
313 | 370 | return shouldSortAgain ? sortTags(tags, paramsOrder, options) : tags; |
314 | 371 | } |
315 | 372 |
|
@@ -615,3 +672,44 @@ function assignOptionalAndDefaultToName({ |
615 | 672 | default: default_, |
616 | 673 | }; |
617 | 674 | } |
| 675 | + |
| 676 | +type ImportDetails = { |
| 677 | + /** the spec associated with this import tag */ |
| 678 | + spec: Spec; |
| 679 | + /** the source of the module that types were imported from */ |
| 680 | + src: string; |
| 681 | + defaultImport?: string; |
| 682 | + /** the types that were imported */ |
| 683 | + namedImports: { |
| 684 | + name: string; |
| 685 | + /** the alias assigned to the type (EX: alias of "B as B0" is "B0") */ |
| 686 | + alias?: string; |
| 687 | + }[]; |
| 688 | +}; |
| 689 | + |
| 690 | +/** |
| 691 | + * Extracts the defaultImports, namedImports, and src associated with a given import tag. |
| 692 | + */ |
| 693 | +function getImportDetails(spec: Spec): ImportDetails | null { |
| 694 | + // step 1: capture the default import, named import clause, and src |
| 695 | + const match = spec.description.match( |
| 696 | + /([^\s\\,\\{\\}]+)?(?:[^\\{\\}]*)\{?([^\\{\\}]*)?\}?(?:\s+from\s+)[\\'\\"](\S)[\\'\\"]/s, |
| 697 | + ); |
| 698 | + if (!match) return null; |
| 699 | + |
| 700 | + const defaultImport = match[1] || ""; |
| 701 | + const namedImportsClause = match[2] || ""; |
| 702 | + const src = match[3] || ""; |
| 703 | + |
| 704 | + // step 2: get all named imports from the named import section |
| 705 | + const typeMatches = namedImportsClause.matchAll( |
| 706 | + /([^\s\\,\\{\\}]+)(?:\s+as\s+)?([^\s\\,\\{\\}]+)?/g, |
| 707 | + ); |
| 708 | + |
| 709 | + const namedImports = []; |
| 710 | + for (const typeMatch of typeMatches) { |
| 711 | + namedImports.push({ name: typeMatch[1], alias: typeMatch[2] }); |
| 712 | + } |
| 713 | + |
| 714 | + return { spec, src, namedImports, defaultImport }; |
| 715 | +} |
0 commit comments