Skip to content

docgen: Automatic documentation does not handle TypeScript overloads #77457

Closed

Description

@aduth

What problem does this address?

Using TypeScript function overloads prevents auto-documentation from accurately extracting function descriptions.

Example problem:

### registerBlockType
Undocumented declaration.

See: https://github.com/WordPress/gutenberg/pull/77393/files#r3093041729

What is your proposed solution?

@wordpress/docgen should be updated to support extracting descriptions from common implementation patterns for function overloading.

AI-generated initial assessment:

Root cause

In packages/docgen/lib/, TypeScript function overloads break JSDoc extraction:

  1. JSDoc lands on the first overload signature, which babel parses as a TSDeclareFunction (not FunctionDeclaration). See my quick AST probe above — leading comments are on overload 1, none on the implementation.

  2. get-export-entries.js:70-93 only handles ClassDeclaration / FunctionDeclaration / VariableDeclaration. TSDeclareFunction falls through and produces zero export entries, so the two overload signatures are silently dropped.

  3. Only the implementation (a real FunctionDeclaration) produces an export entry. Its token has no leading comment, so getJSDocFromToken returns undefined.

  4. Fallback in get-intermediate-representation.js:90-101 (candidates = ast.body.filter(...)) also requires FunctionDeclaration via hasNamedExportWithName/hasFunctionWithName, so it just finds the same undocumented implementation node.

Result: description ?? UNDOCUMENTED → "Undocumented declaration." at get-intermediate-representation.js:154.

Is it easy?

Easy-to-medium, scoped entirely to packages/docgen/. Fixtures live in packages/docgen/test/ and the logic is a few small files. Probably a couple of hours including tests.

Technical direction

The cleanest fix is to make docgen aware that a FunctionDeclaration export may be preceded by sibling TSDeclareFunction exports with the same name, and that JSDoc belongs to the first one:

  • get-export-entries.js: add a TSDeclareFunction case (treat it like FunctionDeclaration for naming), OR explicitly skip overload signatures so only the implementation produces an entry — your call on which.
  • JSDoc lookup: when processing the implementation's token and its leading comments are empty, walk backwards through ast.body to find the first preceding ExportNamedDeclaration whose declaration is a TSDeclareFunction with id.name === name, and read leading comments from that token. This is the minimum change and matches how TypeScript itself associates docs with overloads.
  • Alternatively, relax hasFunctionWithName / hasNamedExportWithName to also match TSDeclareFunction and change the candidates.length !== 1 bailout to "use first match" — but that's more disruptive.

Tests should cover: overloads with JSDoc on the first signature only, on the implementation only, and on both (first should win to match TypeScript convention).

Activity

  1. added
    [Type] Enhancement
    [Tool] Docgen
    on Apr 17, 2026
  2. added
    [Status] In Progress
    on Apr 22, 2026
  3. Infinite-Null commented on Apr 23, 2026

    @Infinite-Null
    Contributor

    Hey @aduth 👋 I've opened a PR with a fix. Would love your feedback when you get a chance!

  4. aduth commented on Apr 27, 2026

    @aduth
    MemberAuthor

    @Infinite-Null That's great 🙌 My availability to review is pretty limited at the moment, but I'll add myself and some others to review if nobody beats me to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Status] In Progress[Tool] Docgen[Type] Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions