Skip to content

Commit 9c092f6

Browse files
TS2Swift: emit static properties as static members (swiftwasm#653)
1 parent 30cf39a commit 9c092f6

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ export class TypeProcessor {
654654
/**
655655
* Visit a property declaration and extract metadata
656656
* @param {ts.PropertyDeclaration | ts.PropertySignature} node
657-
* @returns {{ jsName: string, swiftName: string, type: string, isReadonly: boolean } | null}
657+
* @returns {{ jsName: string, swiftName: string, type: string, isReadonly: boolean, isStatic: boolean } | null}
658658
*/
659659
visitPropertyDecl(node) {
660660
if (!node.name) return null;
@@ -674,7 +674,8 @@ export class TypeProcessor {
674674
const type = this.checker.getTypeAtLocation(node)
675675
const swiftType = this.visitType(type, node);
676676
const isReadonly = node.modifiers?.some(m => m.kind === ts.SyntaxKind.ReadonlyKeyword) ?? false;
677-
return { jsName, swiftName, type: swiftType, isReadonly };
677+
const isStatic = node.modifiers?.some(m => m.kind === ts.SyntaxKind.StaticKeyword) ?? false;
678+
return { jsName, swiftName, type: swiftType, isReadonly, isStatic };
678679
}
679680

680681
/**
@@ -1011,6 +1012,7 @@ export class TypeProcessor {
10111012

10121013
const type = property.type;
10131014
const swiftName = this.renderIdentifier(property.swiftName);
1015+
const isStatic = property.isStatic;
10141016
const needsJSGetterName = property.jsName !== property.swiftName;
10151017
// Note: `from: .global` is only meaningful for top-level imports and constructors.
10161018
// Instance member access always comes from the JS object itself.
@@ -1020,10 +1022,11 @@ export class TypeProcessor {
10201022
if (needsJSGetterName) getterArgs.push(`jsName: "${this.escapeForSwiftStringLiteral(property.jsName)}"`);
10211023
if (fromArg) getterArgs.push(fromArg);
10221024
const getterAnnotation = this.renderMacroAnnotation("JSGetter", getterArgs);
1025+
const staticKeyword = isStatic ? "static " : "";
10231026

10241027
// Always render getter
10251028
this.emitDocComment(node, { indent: " " });
1026-
this.swiftLines.push(` ${getterAnnotation} var ${swiftName}: ${type}`);
1029+
this.swiftLines.push(` ${getterAnnotation} ${staticKeyword}var ${swiftName}: ${type}`);
10271030

10281031
// Render setter if not readonly
10291032
if (!property.isReadonly) {
@@ -1036,7 +1039,7 @@ export class TypeProcessor {
10361039
if (needsJSNameField) setterArgs.push(`jsName: "${this.escapeForSwiftStringLiteral(property.jsName)}"`);
10371040
if (fromArg) setterArgs.push(fromArg);
10381041
const annotation = this.renderMacroAnnotation("JSSetter", setterArgs);
1039-
this.swiftLines.push(` ${annotation} func ${this.renderIdentifier(setterName)}(_ value: ${type}) ${this.renderEffects({ isAsync: false })}`);
1042+
this.swiftLines.push(` ${annotation} ${staticKeyword}func ${this.renderIdentifier(setterName)}(_ value: ${type}) ${this.renderEffects({ isAsync: false })}`);
10401043
}
10411044
}
10421045

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,22 @@ exports[`ts2swift > snapshots Swift output for RecordDictionary.d.ts > RecordDic
354354
"
355355
`;
356356
357+
exports[`ts2swift > snapshots Swift output for StaticProperty.d.ts > StaticProperty 1`] = `
358+
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
359+
// DO NOT EDIT.
360+
//
361+
// To update this file, just rebuild your project or run
362+
// \`swift package bridge-js\`.
363+
364+
@_spi(BridgeJS) import JavaScriptKit
365+
366+
@JSClass struct Library {
367+
@JSGetter static var version: String
368+
@JSSetter static func setVersion(_ value: String) throws(JSException)
369+
}
370+
"
371+
`;
372+
357373
exports[`ts2swift > snapshots Swift output for StringEnum.d.ts > StringEnum 1`] = `
358374
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
359375
// DO NOT EDIT.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class Library {
2+
static version: string;
3+
}

0 commit comments

Comments
 (0)