Skip to content

BridgeJS: Include Swift doc comments in generated d.ts#771

Merged
krodak merged 1 commit into
swiftwasm:mainfrom
PassiveLogic:kr/export-swift-doc-comments
Jun 23, 2026
Merged

BridgeJS: Include Swift doc comments in generated d.ts#771
krodak merged 1 commit into
swiftwasm:mainfrom
PassiveLogic:kr/export-swift-doc-comments

Conversation

@krodak

@krodak krodak commented Jun 23, 2026

Copy link
Copy Markdown
Member

Overview

Resolves #769. Swift API doc comments on exported declarations are now propagated into the generated TypeScript declarations as JSDoc, so editors surface hover documentation for the bridged API.

The exporter captures the leading doc comment (/// and /** */) for each exported declaration into the skeleton, and the linker renders it as a single JSDoc block. The mapping is the inverse of the JSDoc to Swift DocC conversion the TS2Swift importer already performs, so the two directions stay symmetric:

  • The leading description becomes the JSDoc body.
  • - Parameters: / - Parameter name: items become @param name <desc>.
  • - Returns: becomes @returns <desc>.
  • - Throws: becomes @throws <desc>.

1. Coverage. Doc comments are emitted for exported functions, classes (description, constructor, methods, properties), structs (description, fields), enums (description and const-style object-type static members), protocols (description, methods, properties), and namespaced declarations in both the Exports type hierarchy and the declare global { namespace ... } declarations.

2. Merged with default-value annotations. The default-value JSDoc that was previously the only thing emitted is now folded into the same block, so a parameter never produces two separate comment blocks. Declarations without doc comments produce byte-identical output to before.

3. Comment terminators are escaped. A */ inside a Swift doc comment is rewritten so it cannot prematurely close the generated JSDoc block.

Example

/// API doc comment for eat
/// - Parameters:
///   - food: The food for the sloth to eat.
///   - quantity: The quantity of the food.
@JS func eat(_ food: String, quantity: Int) -> Int { ... }
/**
 * API doc comment for eat
 * @param food The food for the sloth to eat.
 * @param quantity The quantity of the food.
 */
eat(food: string, quantity: number): number;

Testing

Adds a DocComments codegen/link fixture covering the mapping (functions, classes, structs, enums, protocols, namespaced functions, default-value merge, and */ escaping), and documents a namespaced declaration in the existing Namespaces fixture to cover the declare global path. Snapshot, codegen, and generated-d.ts validation all pass.

@krodak krodak force-pushed the kr/export-swift-doc-comments branch from 94347db to 5ccc689 Compare June 23, 2026 10:57
@krodak krodak requested a review from kateinoigakukun June 23, 2026 11:11
@krodak krodak self-assigned this Jun 23, 2026

@kateinoigakukun kateinoigakukun left a comment

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.

Thanks!

Propagate Swift `///` and `/** */` documentation on exported declarations
into the generated TypeScript declarations as JSDoc, so editors surface
hover docs for the bridged API.

The exporter now captures the leading doc comment for functions, classes,
methods, properties, constructors, structs, and enums into the skeleton,
and the linker renders it as a single JSDoc block. The Swift DocC field
list is mapped as the inverse of the TS2Swift importer: the leading
description becomes the JSDoc body, `- Parameters:`/`- Parameter x:` become
`@param`, `- Returns:` becomes `@returns`, and `- Throws:` becomes
`@throws`. Existing default-value annotations are merged into the same
block so a parameter never emits two comment blocks; declarations without
doc comments produce byte-identical output to before.
@krodak krodak force-pushed the kr/export-swift-doc-comments branch from 5ccc689 to 45088be Compare June 23, 2026 12:05
@krodak krodak enabled auto-merge June 23, 2026 12:33
@krodak krodak merged commit 210689e into swiftwasm:main Jun 23, 2026
10 checks passed
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.

BridgeJS: Include Swift API doc comments in generated d.ts

2 participants