Skip to content

Commit 30cf39a

Browse files
Merge pull request #652 from swiftwasm/katei/32f9-ts2swift-treat-c
TS2Swift: treat callable exported consts as functions
1 parent 306c1d7 commit 30cf39a

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ export class TypeProcessor {
261261
if (!isExported) return;
262262

263263
const fromArg = this.renderDefaultJSImportFromArgument();
264+
const isConst = (node.declarationList.flags & ts.NodeFlags.Const) !== 0;
264265

265266
for (const decl of node.declarationList.declarations) {
266267
if (!ts.isIdentifier(decl.name)) continue;
@@ -270,13 +271,30 @@ export class TypeProcessor {
270271
const swiftVarName = this.renderIdentifier(swiftName);
271272

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

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

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

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
@@ -48,6 +48,22 @@ 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) -> Response
61+
62+
@JSClass struct Response {
63+
}
64+
"
65+
`;
66+
5167
exports[`ts2swift > snapshots Swift output for Documentation.d.ts > Documentation 1`] = `
5268
"// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
5369
// 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) => Response;

0 commit comments

Comments
 (0)