Skip to content

BridgeJS: Enum export support for case / raw / empty enums - #418

Merged
kateinoigakukun merged 4 commits into
swiftwasm:mainfrom
PassiveLogic:feat/enum-support
Aug 22, 2025
Merged

kateinoigakukun merged 4 commits into
swiftwasm:mainfrom
PassiveLogic:feat/enum-support

Conversation

@krodak

@krodak krodak commented Aug 21, 2025

Copy link
Copy Markdown
Member

Introduction

This PR adds basic enum export support to the BridgeJS plugin.

Feature Overview

Supported Enum Types

  • Case Enums
  • Raw Value Enums
  • Namespace Enums - empty enums used for organizing types into hierarchical namespaces

Enum Output Styles

New @JS(enumStyle: .tsEnum) parameter allows choosing between:

  • .const (default): Generates const objects with union types for maximum compatibility
  • .tsEnum: Generates native TypeScript enum declarations (available for case enums and raw value enums with String or numeric raw types only)

Examples

Case Enum with Both Styles

@JS enum Direction {
    case north, south, east, west
}

@JS(enumStyle: .tsEnum) enum TSDirection {
    case north, south, east, west
}

Generated TypeScript:

// Const object style (default)
const Direction: {
    readonly North: 0;
    readonly South: 1;
    readonly East: 2;
    readonly West: 3;
};
type Direction = typeof Direction[keyof typeof Direction];

// If @JS(enumStyle: .tsEnum) would be defined
enum TSDirection {
    North = 0,
    South = 1,
    East = 2,
    West = 3,
}

Raw Value Enum

@JS enum Theme: String {
    case light = "light"
    case dark = "dark"
    case auto = "auto"
}
// Const object style (default)
const Theme: {
    readonly Light: "light";
    readonly Dark: "dark";
    readonly Auto: "auto";
};
type Theme = typeof Theme[keyof typeof Theme];

// If @JS(enumStyle: .tsEnum) would be defined
enum Theme {
    Light = "light",
    Dark = "dark",
    Auto = "auto",
}

Namespace Enum

@JS enum Networking {
    @JS enum API {
        @JS enum Method {
            case get
            case post
            case put
            case delete
        }
        // Invalid to declare @JS(namespace) here as it would be conflicting with nesting
        @JS class HTTPServer {
            @JS init() {}
            @JS func call(_ method: Method)
        }
    }
}
// Top level enums can use namespace
@JS(namespace: "Networking.APIV2")
enum Internal {
    @JS enum SupportedMethod {
        case get
        case post
    }
    
    @JS class TestServer {
        @JS init() {}
        @JS func call(_ method: SupportedMethod)
    }
}
namespace Networking {
        namespace API {
            class HTTPServer {
                constructor();
                call(method: Networking.API.Method): void;
            }
            const Method: {
                readonly Get: 0;
                readonly Post: 1;
                readonly Put: 2;
                readonly Delete: 3;
            };
            type Method = typeof Method[keyof typeof Method];
        }
        namespace APIV2 {
            namespace Internal {
                class TestServer {
                    constructor();
                    call(method: Internal.SupportedMethod): void;
                }
                const SupportedMethod: {
                    readonly Get: 0;
                    readonly Post: 1;
                };
                type SupportedMethod = typeof SupportedMethod[keyof typeof SupportedMethod];
            }
        }
    }

What's Not Supported

  • Associated value enums (will be tackled as next step)
  • Computed properties or methods within enums

Testing

  • EnumCase.swift - Case enum tests with both output styles
  • EnumRawType.swift - Raw value enum tests for all supported types
  • EnumNamespace.swift - Namespace enums with mixing same namespace as enum and @JS(namespace)

Docs

Docs updated with example for each enum type, new macro parameter and basic relevant usage guide.

@krodak
krodak requested a review from kateinoigakukun August 21, 2025 16:24
@krodak krodak self-assigned this Aug 21, 2025
@kateinoigakukun

Copy link
Copy Markdown
Member

Thanks! I'lll take a closer look tomorrow but meanwhile would you mind adding runtime test in Tests/BridgeJSRuntimeTests?

@krodak
krodak force-pushed the feat/enum-support branch from 9837d89 to 5717f41 Compare August 21, 2025 17:19
@krodak
krodak force-pushed the feat/enum-support branch from 7d15d31 to 6333085 Compare August 21, 2025 17:28

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

Overall approach looks good to me!

@krodak
krodak requested a review from kateinoigakukun August 22, 2025 10:59
@krodak
krodak force-pushed the feat/enum-support branch 2 times, most recently from da01493 to 0ef902d Compare August 22, 2025 11:15
@krodak
krodak force-pushed the feat/enum-support branch from 0ef902d to 5935315 Compare August 22, 2025 11:52
@kateinoigakukun
kateinoigakukun merged commit 79a9c1b into swiftwasm:main Aug 22, 2025
@kateinoigakukun kateinoigakukun linked an issue Aug 23, 2025 that may be closed by this pull request
@krodak
krodak deleted the feat/enum-support branch November 20, 2025 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants