Skip to content

Commit 4ae9e79

Browse files
committed
BridgeJS: Fix static method and constructor codegen dropping default parameter values
1 parent c47beee commit 4ae9e79

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,7 @@ extension BridgeJSLink {
18341834
let returnExpr = try thunkBuilder.call(abiName: function.abiName, returnType: function.returnType)
18351835

18361836
let printer = CodeFragmentPrinter()
1837-
printer.write("\(function.name)(\(function.parameters.map { $0.name }.joined(separator: ", "))) {")
1837+
printer.write("\(function.name)(\(DefaultValueUtils.formatParameterList(function.parameters))) {")
18381838
printer.indent {
18391839
thunkBuilder.renderFunctionBody(into: printer, returnExpr: returnExpr)
18401840
}
@@ -1891,7 +1891,7 @@ extension BridgeJSLink {
18911891

18921892
let methodPrinter = CodeFragmentPrinter()
18931893
methodPrinter.write(
1894-
"\(method.name): function(\(method.parameters.map { $0.name }.joined(separator: ", "))) {"
1894+
"\(method.name): function(\(DefaultValueUtils.formatParameterList(method.parameters))) {"
18951895
)
18961896
methodPrinter.indent {
18971897
thunkBuilder.renderFunctionBody(into: methodPrinter, returnExpr: returnExpr)
@@ -2932,8 +2932,12 @@ extension BridgeJSLink {
29322932
printer.write("class \(klass.name) {")
29332933
printer.indent {
29342934
if let constructor = klass.constructor {
2935+
let paramSignatures = constructor.parameters.map { param in
2936+
let optional = param.hasDefault ? "?" : ""
2937+
return "\(param.name)\(optional): \(param.type.tsType)"
2938+
}
29352939
let constructorSignature =
2936-
"constructor(\(constructor.parameters.map { "\($0.name): \($0.type.tsType)" }.joined(separator: ", ")));"
2940+
"constructor(\(paramSignatures.joined(separator: ", ")));"
29372941
printer.write(constructorSignature)
29382942
}
29392943

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/DefaultParameters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ export async function createInstantiator(options, swift) {
612612
const structValue = structHelpers.MathOperations.lift();
613613
return structValue;
614614
},
615-
subtract: function(a, b) {
615+
subtract: function(a, b = 5.0) {
616616
const ret = instance.exports.bjs_MathOperations_static_subtract(a, b);
617617
return ret;
618618
},

0 commit comments

Comments
 (0)