diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js index 53216a78c..7d0d8cec1 100644 --- a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js @@ -636,7 +636,7 @@ export class TypeProcessor { /** * Visit a property declaration and extract metadata * @param {ts.PropertyDeclaration | ts.PropertySignature} node - * @returns {{ jsName: string, swiftName: string, type: string, isReadonly: boolean } | null} + * @returns {{ jsName: string, swiftName: string, type: string, isReadonly: boolean, isStatic: boolean } | null} */ visitPropertyDecl(node) { if (!node.name) return null; @@ -656,7 +656,8 @@ export class TypeProcessor { const type = this.checker.getTypeAtLocation(node) const swiftType = this.visitType(type, node); const isReadonly = node.modifiers?.some(m => m.kind === ts.SyntaxKind.ReadonlyKeyword) ?? false; - return { jsName, swiftName, type: swiftType, isReadonly }; + const isStatic = node.modifiers?.some(m => m.kind === ts.SyntaxKind.StaticKeyword) ?? false; + return { jsName, swiftName, type: swiftType, isReadonly, isStatic }; } /** @@ -993,6 +994,7 @@ export class TypeProcessor { const type = property.type; const swiftName = this.renderIdentifier(property.swiftName); + const isStatic = property.isStatic; const needsJSGetterName = property.jsName !== property.swiftName; // Note: `from: .global` is only meaningful for top-level imports and constructors. // Instance member access always comes from the JS object itself. @@ -1002,10 +1004,11 @@ export class TypeProcessor { if (needsJSGetterName) getterArgs.push(`jsName: "${this.escapeForSwiftStringLiteral(property.jsName)}"`); if (fromArg) getterArgs.push(fromArg); const getterAnnotation = this.renderMacroAnnotation("JSGetter", getterArgs); + const staticKeyword = isStatic ? "static " : ""; // Always render getter this.emitDocComment(node, { indent: " " }); - this.swiftLines.push(` ${getterAnnotation} var ${swiftName}: ${type}`); + this.swiftLines.push(` ${getterAnnotation} ${staticKeyword}var ${swiftName}: ${type}`); // Render setter if not readonly if (!property.isReadonly) { @@ -1018,7 +1021,7 @@ export class TypeProcessor { if (needsJSNameField) setterArgs.push(`jsName: "${this.escapeForSwiftStringLiteral(property.jsName)}"`); if (fromArg) setterArgs.push(fromArg); const annotation = this.renderMacroAnnotation("JSSetter", setterArgs); - this.swiftLines.push(` ${annotation} func ${this.renderIdentifier(setterName)}(_ value: ${type}) ${this.renderEffects({ isAsync: false })}`); + this.swiftLines.push(` ${annotation} ${staticKeyword}func ${this.renderIdentifier(setterName)}(_ value: ${type}) ${this.renderEffects({ isAsync: false })}`); } } 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 85d1da0de..da51d6c50 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 @@ -338,6 +338,22 @@ exports[`ts2swift > snapshots Swift output for RecordDictionary.d.ts > RecordDic " `; +exports[`ts2swift > snapshots Swift output for StaticProperty.d.ts > StaticProperty 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(BridgeJS) import JavaScriptKit + +@JSClass struct Library { + @JSGetter static var version: String + @JSSetter static func setVersion(_ value: String) throws(JSException) +} +" +`; + exports[`ts2swift > snapshots Swift output for StringEnum.d.ts > StringEnum 1`] = ` "// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit, // DO NOT EDIT. diff --git a/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/StaticProperty.d.ts b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/StaticProperty.d.ts new file mode 100644 index 000000000..afef26b5c --- /dev/null +++ b/Plugins/BridgeJS/Sources/TS2Swift/JavaScript/test/fixtures/StaticProperty.d.ts @@ -0,0 +1,3 @@ +export class Library { + static version: string; +}