Skip to content

Preserve JSDoc @property comments when reconstructing typedef types#4674

Open
veksa wants to merge 1 commit into
microsoft:mainfrom
veksa:fix/4352-preserve-typedef-property-comments
Open

Preserve JSDoc @property comments when reconstructing typedef types#4674
veksa wants to merge 1 commit into
microsoft:mainfrom
veksa:fix/4352-preserve-typedef-property-comments

Conversation

@veksa

@veksa veksa commented Jul 19, 2026

Copy link
Copy Markdown

Fixes #4352

Problem

Block comments on @property entries of a @typedef were dropped when the structural type was reconstructed inline in another file. TypeScript 6 preserves them; tsgo did not.

// lib.js
/**
 * @typedef {Object} Foo
 * @property {boolean} bool Whether `.bool` is true or not
 */
export class C {
    /** @returns {Foo} */
    getFoo() { return { bool: false }; }
}

// main.js
import { C } from './lib.js';
export class Main {
    constructor() { this.c = new C(); }
    getFoo() { return { ...this.c.getFoo() }; }
}

main.d.ts before (comment lost):

getFoo(): {
    bool: boolean;
};

main.d.ts after (matches tsc):

getFoo(): {
    /**
     * Whether `.bool` is true or not
     */
    bool: boolean;
};

Fix

The old JS .d.ts emitter copied comments onto synthesized property signatures via preserveCommentsOn / setSyntheticLeadingComments. The Go node builder used a raw setCommentRange in addPropertyToElementList, which drops comments for properties reparsed from @typedef/@property tags — their comment lives on a synthetic reparsed JSDoc rather than in a source comment range.

This reimplements preserveCommentsOn inside addPropertyToElementList: for a property whose declaration is a reparsed node carrying JSDoc, the comment text is reattached as a synthetic leading comment; otherwise the existing setCommentRange behavior is preserved. Only reparsed JS @property declarations take the new path, so TS declaration emit is unaffected.

Tests

Adds jsDeclarationsTypedefPropertyComments.ts covering both the same-file type alias and the cross-file inline reconstruction. Full local compiler suite and checker/transformer package tests pass.

AI usage disclosure

This change was authored with the assistance of an AI agent (Claude Code). I have read and understand the change and will discuss and revise it in review like any other contribution.

Copilot AI review requested due to automatic review settings July 19, 2026 15:26
@veksa

veksa commented Jul 19, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Preserves JSDoc @property descriptions when typedef structures are synthesized into declaration types.

Changes:

  • Reattaches reparsed JSDoc as synthetic leading comments.
  • Adds a cross-file JavaScript declaration test and baselines.

Reviewed changes

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

Show a summary per file
File Description
internal/checker/nodebuilderimpl.go Preserves comments on synthesized properties.
testdata/tests/cases/conformance/jsdoc/declarations/jsDeclarationsTypedefPropertyComments.ts Adds regression coverage.
testdata/baselines/reference/conformance/jsDeclarationsTypedefPropertyComments.js Verifies declaration output.
testdata/baselines/reference/conformance/jsDeclarationsTypedefPropertyComments.types Records inferred types.
testdata/baselines/reference/conformance/jsDeclarationsTypedefPropertyComments.symbols Records symbol bindings.

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

Labels

None yet

Projects

None yet

2 participants