Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BridgeJS: Macro extension to define namespace #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
kateinoigakukun merged 4 commits into swiftwasm:main from PassiveLogic:feat/macro-namespace
Aug 15, 2025

Conversation

@krodak
Copy link
Member

@krodak krodak commented Aug 14, 2025
edited
Loading

Introduction

This PR adds namespace-based convention support to the BridgeJS @JS macro, which enables creating TypeScript namespaces that are accessible globally in JavaScript, while maintaining direct export access.

Reasoning

Change allows up to align better with existing code conventions, but we believe it could be useful for others as well, without breaking any existing functionality

Feature example

New @JS(namespace: "my.namespace") macro

Example Swift code:

@JS(namespace: "__Swift.Foundation.UUID.create") func createUUID() -> String {
 UUID().uuidString
}
@JS(namespace: "__Swift.Foundation.UUID.validate") func validateUUID(_ uuid: String) -> Bool {
 UUID(uuidString: uuid) != nil
}

Generated TypeScript:

export {};
declare global {
 namespace __Swift {
 namespace Foundation {
 namespace UUID {
 function create(): string;
 function validate(uuid: string): boolean;
 }
 }
 }
}
export type Exports = {
 create(): string;
 validate(uuid: string): boolean;
}

Testing

Included new tests within Namespaces.swift

Copy link
Member

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, much appreciated!

I have a couple of questions:

  1. For something like:

    @JS("__Swift.Foundation")
    class UUID {
     @JS("__Swift.Foundation")
     func uuidString() -> String {
     Foundation.UUID().uuidString
     }
    }

    Do we actually need the namespace attribute on uuidString here? It seems that namespace on method decl is currently ignored. For such cases where namespace has no effect, it might be less confusing to emit an error or warning. In other words, how about restricting namespace parameter only for top-level declarations?

  2. About the public macro JS(_ namespace: String) interface:
    To keep it simple but extensible, how about changing it to public macro JS(namespace: String? = nil)?. Using a labeled parameter leaves room for adding more String parameters in the future. Also since macros can have default arguments, we probably don't need to have a dedicated overload. This would allow us to document both @JS and @JS(namespace:) together in a single DocC page.

krodak reacted with thumbs up emoji
@krodak krodak force-pushed the feat/macro-namespace branch from 7a2de9f to fff2554 Compare August 14, 2025 16:50
@krodak krodak force-pushed the feat/macro-namespace branch from fff2554 to 43a88d4 Compare August 14, 2025 16:57
Copy link
Member Author

krodak commented Aug 14, 2025

@kateinoigakukun sounds good, thanks for valuable feedback 🙇🏼‍♂️

  • added warnings for non-top-level namespaces on functions and init
    • aligned examples and tests to reflect that
  • merged macros; was considering this, but wanted to avoid changing existing one, if you are fine with that, I think it's nicer this way 👌🏻
  • added example to existing docc documentation now that you mentioned this 🙏🏻
  • fixed swiftformat issues, added to pre-commit hooks, so hopefully won't break CI again
  • cleaned up commits a bit to prefix with BridgeJS

BridgeJS: fix: run swiftformat
BridgeJS: chore: extend docc documentaion with namespace example
@krodak krodak force-pushed the feat/macro-namespace branch from 43a88d4 to e701702 Compare August 14, 2025 18:52
Copy link
Member

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

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

Thanks!

@kateinoigakukun kateinoigakukun merged commit 5a553c4 into swiftwasm:main Aug 15, 2025
13 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@kateinoigakukun kateinoigakukun kateinoigakukun approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /