Skip to content

Fix basic generics parsing#668

Open
moshams272 wants to merge 6 commits intonodejs:mainfrom
moshams272:fix-basic-generics-parsing
Open

Fix basic generics parsing#668
moshams272 wants to merge 6 commits intonodejs:mainfrom
moshams272:fix-basic-generics-parsing

Conversation

@moshams272
Copy link

Description

Currently, the parser doesn't properly handle basic TypeScript generics like {Promise<string>}. This PR adds support for parsing these basic generic types within transformTypeToReferenceLink.

It safely separates the base type and inner type, mapping both to their respective Markdown links (e.g., [](...)&lt;[](...)&gt;).

Validation

I added new unit tests in src/utils/parser/__tests__/index.test.mjs to cover both fully recognized and partially recognized generic types.

Reviewers can verify the logic by running node --run test:coverage. All tests pass successfully without breaking the existing fallback parsing logic.
(Note: The parsed output intentionally uses &lt; and &gt; entities to avoid potential downstream MDX/HTML rendering errors).

Related Issues

Fixes #666

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 March 12, 2026 12:43
@vercel
Copy link

vercel bot commented Mar 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 Mar 12, 2026 4:47pm

Request Review

@avivkeller
Copy link
Member

Nice! A few stylistic comments, but implementation wise is relatively good to go!

@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 99.18033% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 76.10%. Comparing base (5f572b4) to head (713d050).

Files with missing lines Patch % Lines
src/utils/parser/index.mjs 98.86% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #668      +/-   ##
==========================================
+ Coverage   75.90%   76.10%   +0.19%     
==========================================
  Files         145      145              
  Lines       13718    13836     +118     
  Branches      991     1014      +23     
==========================================
+ Hits        10413    10530     +117     
- Misses       3299     3300       +1     
  Partials        6        6              

☔ 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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds basic TypeScript generic-type support to the parser so {Promise<string>}-style annotations are converted into appropriate Markdown links (with escaped &lt; / &gt;), improving type reference rendering in generated docs.

Changes:

  • Update transformTypeToReferenceLink to preserve < > initially, detect basic generics, and split outer unions safely.
  • Add unit tests covering recognized, partially recognized, union, multi-parameter, and outer-union generic types.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/utils/parser/index.mjs Implements generic parsing/formatting in transformTypeToReferenceLink, plus a new union-splitting helper.
src/utils/parser/tests/index.test.mjs Adds test coverage for basic generic parsing scenarios and expected escaped output.
Comments suppressed due to low confidence (3)

src/utils/parser/index.mjs:186

  • splitByOuterUnion decrements depth on every > character. If the type string contains > outside of generics (e.g. arrow types =>), depth can become negative and outer | unions will stop splitting, changing behavior vs the previous typeInput.split('|') implementation. Consider only decrementing when depth > 0 (or clamping to 0) so non-generic > doesn't affect union parsing.

    // Transform Node.js type/module references into Markdown links
    // that refer to other API docs pages within the Node.js API docs
    if (record && lookupPiece in record) {
      return record[lookupPiece];
    }

    // Transform Node.js types like 'vm.Something'.
    if (lookupPiece.indexOf('.') >= 0) {
      const [mod, ...pieces] = lookupPiece.split('.');

src/utils/parser/index.mjs:156

  • replace('[]', '') removes the first [] occurrence anywhere in the string; for array types the code appears to want to strip only a trailing [] before lookup. Consider removing only an end suffix to avoid incorrect matches.
   * Handles the mapping (if there's a match) of the input text
   * into the reference type from the API docs
   *
   * @param {string} lookupPiece

src/utils/parser/index.mjs:211

  • The comment says a regex is used to remove [] from the end of the type, but the code uses plainPiece.replace('[]', ''), which is not end-anchored and only removes the first occurrence. Update the implementation (and/or the comment) so it accurately strips only a trailing array suffix.
    const result = transformType(trimmedPiece.replace('[]', ''));

    // If we have a valid result and the piece is not empty, we return the Markdown link
    if (trimmedPiece.length && result.length) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@moshams272
Copy link
Author

Thank you so much for the review and the kind words 🤍 I really appreciate your time and the stylistic feedback,
If you have any other comment?

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.

Support TypeScript types

3 participants