Skip to content

Commit 1758695

Browse files
TS2Swift: handle callable exported consts
1 parent b10bee6 commit 1758695

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,30 @@ export class TypeProcessor {
270270
const swiftVarName = this.renderIdentifier(swiftName);
271271

272272
const type = this.checker.getTypeAtLocation(decl);
273-
const swiftType = this.visitType(type, decl);
274273

275274
/** @type {string[]} */
276275
const args = [];
277276
const jsNameArg = this.renderOptionalJSNameArg(jsName, swiftName);
278277
if (jsNameArg) args.push(jsNameArg);
279278
if (fromArg) args.push(fromArg);
279+
const callSignatures = type.getCallSignatures();
280+
281+
if (callSignatures.length > 0) {
282+
const signature = callSignatures[0];
283+
const parameters = signature.getParameters();
284+
const parameterNameMap = this.buildParameterNameMap(parameters);
285+
const params = this.renderParameters(parameters, decl);
286+
const returnType = this.visitType(signature.getReturnType(), decl);
287+
const effects = this.renderEffects({ isAsync: false });
288+
const annotation = this.renderMacroAnnotation("JSFunction", args);
289+
290+
this.emitDocComment(decl, { indent: "", parameterNameMap });
291+
this.swiftLines.push(`${annotation} func ${swiftVarName}(${params}) ${effects} -> ${returnType}`);
292+
this.swiftLines.push("");
293+
continue;
294+
}
295+
296+
const swiftType = this.visitType(type, decl);
280297
const annotation = this.renderMacroAnnotation("JSGetter", args);
281298

282299
this.emitDocComment(decl, { indent: "" });

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ exports[`ts2swift > snapshots Swift output for Async.d.ts > Async 1`] = `
4848
"
4949
`;
5050

51+
exports[`ts2swift > snapshots Swift output for CallableConst.d.ts > CallableConst 1`] = `
52+
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
53+
// DO NOT EDIT.
54+
//
55+
// To update this file, just rebuild your project or run
56+
// \`swift package bridge-js\`.
57+
58+
@_spi(BridgeJS) import JavaScriptKit
59+
60+
@JSFunction func fetch(_ url: String) throws(JSException) -> JSPromise
61+
"
62+
`;
63+
5164
exports[`ts2swift > snapshots Swift output for Documentation.d.ts > Documentation 1`] = `
5265
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
5366
// DO NOT EDIT.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export interface Response {}
2+
export const fetch: (url: string) => Promise<Response>;

0 commit comments

Comments
 (0)