Skip to content

Commit 7bad9cd

Browse files
TS2Swift: emit static properties as static members
1 parent b10bee6 commit 7bad9cd

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
@@ -636,7 +636,7 @@ export class TypeProcessor {
636636
/**
637637
* Visit a property declaration and extract metadata
638638
* @param {ts.PropertyDeclaration | ts.PropertySignature} node
639-
* @returns {{ jsName: string, swiftName: string, type: string, isReadonly: boolean } | null}
639+
* @returns {{ jsName: string, swiftName: string, type: string, isReadonly: boolean, isStatic: boolean } | null}
640640
*/
641641
visitPropertyDecl(node) {
642642
if (!node.name) return null;
@@ -656,7 +656,8 @@ export class TypeProcessor {
656656
const type = this.checker.getTypeAtLocation(node)
657657
const swiftType = this.visitType(type, node);
658658
const isReadonly = node.modifiers?.some(m => m.kind === ts.SyntaxKind.ReadonlyKeyword) ?? false;
659-
return { jsName, swiftName, type: swiftType, isReadonly };
659+
const isStatic = node.modifiers?.some(m => m.kind === ts.SyntaxKind.StaticKeyword) ?? false;
660+
return { jsName, swiftName, type: swiftType, isReadonly, isStatic };
660661
}
661662

662663
/**
@@ -993,6 +994,7 @@ export class TypeProcessor {
993994

994995
const type = property.type;
995996
const swiftName = this.renderIdentifier(property.swiftName);
997+
const isStatic = property.isStatic;
996998
const needsJSGetterName = property.jsName !== property.swiftName;
997999
// Note: `from: .global` is only meaningful for top-level imports and constructors.
9981000
// Instance member access always comes from the JS object itself.
@@ -1002,10 +1004,11 @@ export class TypeProcessor {
10021004
if (needsJSGetterName) getterArgs.push(`jsName: "${this.escapeForSwiftStringLiteral(property.jsName)}"`);
10031005
if (fromArg) getterArgs.push(fromArg);
10041006
const getterAnnotation = this.renderMacroAnnotation("JSGetter", getterArgs);
1007+
const staticKeyword = isStatic ? "static " : "";
10051008

10061009
// Always render getter
10071010
this.emitDocComment(node, { indent: " " });
1008-
this.swiftLines.push(` ${getterAnnotation} var ${swiftName}: ${type}`);
1011+
this.swiftLines.push(` ${getterAnnotation} ${staticKeyword}var ${swiftName}: ${type}`);
10091012

10101013
// Render setter if not readonly
10111014
if (!property.isReadonly) {
@@ -1018,7 +1021,7 @@ export class TypeProcessor {
10181021
if (needsJSNameField) setterArgs.push(`jsName: "${this.escapeForSwiftStringLiteral(property.jsName)}"`);
10191022
if (fromArg) setterArgs.push(fromArg);
10201023
const annotation = this.renderMacroAnnotation("JSSetter", setterArgs);
1021-
this.swiftLines.push(` ${annotation} func ${this.renderIdentifier(setterName)}(_ value: ${type}) ${this.renderEffects({ isAsync: false })}`);
1024+
this.swiftLines.push(` ${annotation} ${staticKeyword}func ${this.renderIdentifier(setterName)}(_ value: ${type}) ${this.renderEffects({ isAsync: false })}`);
10221025
}
10231026
}
10241027

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
@@ -338,6 +338,22 @@ exports[`ts2swift > snapshots Swift output for RecordDictionary.d.ts > RecordDic
338338
"
339339
`;
340340
341+
exports[`ts2swift > snapshots Swift output for StaticProperty.d.ts > StaticProperty 1`] = `
342+
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
343+
// DO NOT EDIT.
344+
//
345+
// To update this file, just rebuild your project or run
346+
// \`swift package bridge-js\`.
347+
348+
@_spi(BridgeJS) import JavaScriptKit
349+
350+
@JSClass struct Library {
351+
@JSGetter static var version: String
352+
@JSSetter static func setVersion(_ value: String) throws(JSException)
353+
}
354+
"
355+
`;
356+
341357
exports[`ts2swift > snapshots Swift output for StringEnum.d.ts > StringEnum 1`] = `
342358
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
343359
// 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)