From 0c44eba9478801354561bacecf96aa911f080fb8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 12 Feb 2026 23:03:26 +0900 Subject: [PATCH 1/3] Fix ts2swift type alias names and add console alias test --- .../TS2Swift/JavaScript/src/processor.js | 8 +++++--- .../test/__snapshots__/ts2swift.test.js.snap | 17 +++++++++++++++++ .../test/fixtures/TypeAliasObject.d.ts | 5 +++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js index 302058036..579bf1e57 100644 --- a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js @@ -56,6 +56,8 @@ export class TypeProcessor { this.processedTypes = new Map(); /** @type {Map} Seen position by type */ this.seenTypes = new Map(); + /** @type {Map} Preferred alias names by underlying type */ + this.aliasNameByType = new Map(); /** @type {string[]} Collected Swift code lines */ this.swiftLines = []; /** @type {Set} */ @@ -747,8 +749,8 @@ export class TypeProcessor { * @private */ visitStructuredType(type, diagnosticNode, members) { - const symbol = type.getSymbol() ?? type.aliasSymbol; - const name = symbol?.name ?? this.checker.typeToString(type); + const symbol = type.aliasSymbol ?? type.getSymbol(); + const name = type.aliasSymbol?.name ?? symbol?.name ?? this.checker.typeToString(type); if (!name) return; if (this.emittedStructuredTypeNames.has(name)) return; this.emittedStructuredTypeNames.add(name); @@ -760,7 +762,7 @@ export class TypeProcessor { if (jsNameArg) args.push(jsNameArg); const annotation = this.renderMacroAnnotation("JSClass", args); const typeName = this.renderIdentifier(swiftName); - const docNode = symbol?.getDeclarations()?.[0] ?? diagnosticNode; + const docNode = type.aliasSymbol?.getDeclarations()?.[0] ?? symbol?.getDeclarations()?.[0] ?? diagnosticNode; if (docNode) { this.emitDocComment(docNode, { indent: "" }); } diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap index 343f8795b..cfd9de98a 100644 --- a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap @@ -426,6 +426,23 @@ exports[`ts2swift > snapshots Swift output for TypeAlias.d.ts > TypeAlias 1`] = " `; +exports[`ts2swift > snapshots Swift output for TypeAliasObject.d.ts > TypeAliasObject 1`] = ` +"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit, +// DO NOT EDIT. +// +// To update this file, just rebuild your project or run +// \`swift package bridge-js\`. + +@_spi(Experimental) @_spi(BridgeJS) import JavaScriptKit + +@JSFunction func console() throws(JSException) -> Console + +@JSClass struct Console { + @JSFunction func log(_ message: String) throws(JSException) -> Void +} +" +`; + exports[`ts2swift > snapshots Swift output for TypeScriptClass.d.ts > TypeScriptClass 1`] = ` "// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit, // DO NOT EDIT. diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts new file mode 100644 index 000000000..e5bcadc74 --- /dev/null +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts @@ -0,0 +1,5 @@ +export type Console = { + log(message: string): void; +}; + +export function console(): Console; From df0d043b8827f031c21c35c049d329e05bb36fe1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Feb 2026 01:04:22 +0900 Subject: [PATCH 2/3] Remove unused aliasNameByType map --- Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js index 579bf1e57..c71ff6fe5 100644 --- a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js @@ -56,8 +56,6 @@ export class TypeProcessor { this.processedTypes = new Map(); /** @type {Map} Seen position by type */ this.seenTypes = new Map(); - /** @type {Map} Preferred alias names by underlying type */ - this.aliasNameByType = new Map(); /** @type {string[]} Collected Swift code lines */ this.swiftLines = []; /** @type {Set} */ From 04dffe7f343d66407cc635d2b89121e763523ef2 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Feb 2026 02:04:41 +0900 Subject: [PATCH 3/3] Cover type alias doc comments in ts2swift snapshots --- .../JavaScript/test/__snapshots__/ts2swift.test.js.snap | 2 ++ .../TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap index cfd9de98a..49814a996 100644 --- a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap @@ -437,7 +437,9 @@ exports[`ts2swift > snapshots Swift output for TypeAliasObject.d.ts > TypeAliasO @JSFunction func console() throws(JSException) -> Console +/// Console from the environment. @JSClass struct Console { + /// Log a message. @JSFunction func log(_ message: String) throws(JSException) -> Void } " diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts index e5bcadc74..55ffdfa3a 100644 --- a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/TypeAliasObject.d.ts @@ -1,4 +1,10 @@ +/** + * Console from the environment. + */ export type Console = { + /** + * Log a message. + */ log(message: string): void; };