Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
}

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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 })}`);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Library {
static version: string;
}