Skip to content

Commit 0c44eba

Browse files
Fix ts2swift type alias names and add console alias test
1 parent 318f3ae commit 0c44eba

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class TypeProcessor {
5656
this.processedTypes = new Map();
5757
/** @type {Map<ts.Type, ts.Node>} Seen position by type */
5858
this.seenTypes = new Map();
59+
/** @type {Map<ts.Type, string>} Preferred alias names by underlying type */
60+
this.aliasNameByType = new Map();
5961
/** @type {string[]} Collected Swift code lines */
6062
this.swiftLines = [];
6163
/** @type {Set<string>} */
@@ -747,8 +749,8 @@ export class TypeProcessor {
747749
* @private
748750
*/
749751
visitStructuredType(type, diagnosticNode, members) {
750-
const symbol = type.getSymbol() ?? type.aliasSymbol;
751-
const name = symbol?.name ?? this.checker.typeToString(type);
752+
const symbol = type.aliasSymbol ?? type.getSymbol();
753+
const name = type.aliasSymbol?.name ?? symbol?.name ?? this.checker.typeToString(type);
752754
if (!name) return;
753755
if (this.emittedStructuredTypeNames.has(name)) return;
754756
this.emittedStructuredTypeNames.add(name);
@@ -760,7 +762,7 @@ export class TypeProcessor {
760762
if (jsNameArg) args.push(jsNameArg);
761763
const annotation = this.renderMacroAnnotation("JSClass", args);
762764
const typeName = this.renderIdentifier(swiftName);
763-
const docNode = symbol?.getDeclarations()?.[0] ?? diagnosticNode;
765+
const docNode = type.aliasSymbol?.getDeclarations()?.[0] ?? symbol?.getDeclarations()?.[0] ?? diagnosticNode;
764766
if (docNode) {
765767
this.emitDocComment(docNode, { indent: "" });
766768
}

Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/__snapshots__/ts2swift.test.js.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,23 @@ exports[`ts2swift > snapshots Swift output for TypeAlias.d.ts > TypeAlias 1`] =
426426
"
427427
`;
428428
429+
exports[`ts2swift > snapshots Swift output for TypeAliasObject.d.ts > TypeAliasObject 1`] = `
430+
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
431+
// DO NOT EDIT.
432+
//
433+
// To update this file, just rebuild your project or run
434+
// \`swift package bridge-js\`.
435+
436+
@_spi(Experimental) @_spi(BridgeJS) import JavaScriptKit
437+
438+
@JSFunction func console() throws(JSException) -> Console
439+
440+
@JSClass struct Console {
441+
@JSFunction func log(_ message: String) throws(JSException) -> Void
442+
}
443+
"
444+
`;
445+
429446
exports[`ts2swift > snapshots Swift output for TypeScriptClass.d.ts > TypeScriptClass 1`] = `
430447
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
431448
// DO NOT EDIT.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type Console = {
2+
log(message: string): void;
3+
};
4+
5+
export function console(): Console;

0 commit comments

Comments
 (0)