Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 9, 2025

This PR ports the fix from TypeScript PR #59675 to resolve an issue where JSDoc comments were missing or duplicated in declarations for overloads declared in classes declared in functions.

Problem

When generating type declarations for method overloads in classes that are declared within functions, all overloads would incorrectly share the same JSDoc comment instead of preserving their individual comments.

Consider this TypeScript code:

function Foo() {
    return class Bar {
        /**
         * comment 1
         */
        foo(bar: string): void;
        /**
         * @deprecated This signature is deprecated
         *
         * comment 2
         */
        foo(): string;
        foo(bar?: string): string | void {
            return 'hi'
        }
    }
}

Before this fix: The generated .d.ts file would show both overloads with "comment 1"
After this fix: Each overload correctly preserves its own JSDoc comment

Solution

Updated the comment range logic in addPropertyToElementList() in internal/checker/nodebuilderimpl.go to prioritize signature-specific declarations over property symbol declarations for method overloads:

// Before: Always used propertySymbol.ValueDeclaration
b.setCommentRange(methodDeclaration, propertySymbol.ValueDeclaration)

// After: Use signature.Declaration() when available
var commentRange *ast.Node
if signature.Declaration() != nil {
    commentRange = signature.Declaration()
} else {
    commentRange = propertySymbol.ValueDeclaration
}
b.setCommentRange(methodDeclaration, commentRange)

This ensures that each overload signature gets its correct JSDoc comment instead of all overloads sharing the same comment from the property symbol.

Testing

The fix is validated by the signatureOverloadsWithComments.ts test case (imported from the TypeScript submodule), which now correctly generates declarations with preserved individual JSDoc comments for each overload.

Fixes #1126.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
Copilot AI changed the title [WIP] Port TypeScript PR #59675: fix(59397): JsDoc is missing/duplicated in declarations for overloads declared in classes declared in functions Port TypeScript PR #59675: fix(59397): JSDoc is missing/duplicated in declarations for overloads declared in classes declared in functions Jun 9, 2025
Copilot AI requested a review from andrewbranch June 9, 2025 22:58
Copilot finished work on behalf of andrewbranch June 9, 2025 22:59
@jakebailey jakebailey deleted the copilot/fix-1126 branch June 24, 2025 23:56
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.

Port TypeScript PR #59675: fix(59397): JsDoc is missing/duplicated in declarations for overloads declared in classes declared in functions

2 participants