Skip to content

feat(metadata): support advanced generics using recursion#763

Open
moshams272 wants to merge 9 commits intonodejs:mainfrom
moshams272:feat/advanced-generics
Open

feat(metadata): support advanced generics using recursion#763
moshams272 wants to merge 9 commits intonodejs:mainfrom
moshams272:feat/advanced-generics

Conversation

@moshams272
Copy link
Copy Markdown
Contributor

Description

This PR uses the Recursive approach instead of using Regex that just covered the basic generic and can't handle:

  • Nested Generics Transformer<T, Awaitable<U>>
  • Function Signature (str: MyType) => Promise<T>
  • Complex Union and Intersection string & number | boolean
  • Depth

The new implementation uses a top-down Recursive approach, breaking down types layer by layer starting from the weakest operators to the strongest.

Validation

  • Added comprehensive test cases in transformers.test.mjs
  • Run node --run test and verified that all test cases (old and new) passed successfully.

Related Issues

This PR acts as an extension to #666. While #666 solved the basic mapping, this implementation introduces a recursive parser to handle more complex nested types.

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run node --run test and all tests passed.
  • I have check code formatting with node --run format & node --run lint.
  • I've covered new added functionality with unit tests if necessary.

@moshams272 moshams272 requested a review from a team as a code owner April 12, 2026 08:46
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
api-docs-tooling Ready Ready Preview Apr 17, 2026 1:37pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 12, 2026

PR Summary

Medium Risk
Replaces the type-to-link transformation logic with a new recursive parser that changes how unions/intersections/functions/generics are interpreted and rendered, which could subtly affect generated docs formatting. Risk is mitigated by expanded unit coverage but still impacts core metadata generation behavior.

Overview
Type reference linking now uses a recursive parser instead of regex splitting. transformTypeToReferenceLink delegates to new parseType logic that understands nested generics (including generic arrays), unions/intersections with correct precedence, and arrow-function signatures (including destructured params).

Removes the old TYPE_GENERIC_REGEX/generic formatting helpers and adds targeted tests covering function-returning-generic types and deeply nested combinations of generics, arrays, unions, intersections, and destructuring.

Reviewed by Cursor Bugbot for commit 0115291. Bugbot is set up for automated code reviews on this repo. Configure here.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 12, 2026

Codecov Report

❌ Patch coverage is 83.95904% with 47 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.91%. Comparing base (3d00fa8) to head (0115291).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/generators/metadata/utils/typeParser.mjs 81.12% 43 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #763      +/-   ##
==========================================
- Coverage   78.43%   77.91%   -0.52%     
==========================================
  Files         157      160       +3     
  Lines       13962    14236     +274     
  Branches     1152     1181      +29     
==========================================
+ Hits        10951    11092     +141     
- Misses       3006     3135     +129     
- Partials        5        9       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/generators/metadata/utils/transformers.mjs Outdated
Comment thread src/generators/metadata/utils/transformers.mjs
Comment thread src/generators/metadata/utils/transformers.mjs Outdated
Comment thread src/generators/metadata/utils/transformers.mjs Outdated
const input =
'(str: MyType) => Promise<Map<string, number & string>, Map<string | number>>';
const expected =
'(str: MyType) =&gt; [`<Promise>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)&lt;[`<Map>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)&lt;[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type), [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type) & [`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type)&gt;, [`<Map>`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)&lt;[`<string>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#string_type) | [`<number>`](https://developer.mozilla.org/docs/Web/JavaScript/Data_structures#number_type)&gt;&gt;';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MyType should be handled, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Parsing individual types inside the parameter string (handling colons, commas, optional params ?, etc.) requires a much deeper level of AST-like parsing and I focused on generics and left the parameter string for now as I will open another PR soon to handle this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to drop a quick update: regarding your question about myType not being parsed, I went ahead and implemented the function signature parsing.

Comment thread src/generators/metadata/utils/typeParser.mjs
- Enforce exact TS operator precedence: Arrow functions (=>) before Unions (|) and Intersections (&).

- Implement stripOuterParentheses to unwrap redundant outer groups and prevent parser depth-blindness.

- Fix array loss: safely preserve [] notation for both base types and generics.

- Correctly handle higher-order functions by re-joining subsequent arrow operator segments.
Comment thread src/generators/metadata/utils/typeParser.mjs Outdated
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d343adb. Configure here.

const result = transformType(cleanType);
if (cleanType.length && result) {
return `[\`<${cleanType}>\`](${result})${isArray ? '[]' : ''}`;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array type link text formatting changed from old behavior

Low Severity

For plain array types like string[], the old code produced link text <string[]> (with [] inside the link), but the new base-case logic uses cleanType (with [] stripped) in the link text and appends [] outside. This changes rendered documentation output from [`<string[]>`](URL) to [`<string>`](URL)[], making the array brackets unlinked. The old code used trimmedPiece (which preserved []) in the Markdown link template.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d343adb. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think <string>[] is better than <string[]> as the link is just on word string!

Comment thread src/generators/metadata/utils/typeParser.mjs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants