Skip to content

Commit 9a56b28

Browse files
committed
BridgeJS: Consolidate namespaced and non-namespaced class DTS entry rendering
The `renderClassEntry` closure passed to `buildHierarchicalExportsType` manually rebuilt the same `ClassName: { new(...); staticMethod(); ... }` block that `renderExportedClass` already produces as its `dtsExportEntry` return value. The two paths had to be kept in sync by hand. Replace the inline closure with a direct call to `renderExportedClass` that discards the JS and DTS-type outputs and returns only the `dtsExportEntry` slice. This makes the class namespace entry for namespaced and non-namespaced classes identical by construction. Thread `throws` through `buildHierarchicalExportsType`, `populateTypeScriptExportLines`, and `generateTypeScript` to accommodate the fact that `renderExportedClass` is throwing. Hoist the `buildHierarchicalExportsType` call out of the `printer.indent` closure so the `try` expression is in a throwing context.
1 parent 6d2c070 commit 9a56b28

1 file changed

Lines changed: 20 additions & 43 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ public struct BridgeJSLink {
796796
return printer.lines
797797
}
798798

799-
private func generateTypeScript(data: LinkData) -> String {
799+
private func generateTypeScript(data: LinkData) throws -> String {
800800
let header = """
801801
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
802802
// DO NOT EDIT.
@@ -884,45 +884,22 @@ public struct BridgeJSLink {
884884
printer.write(lines: generateImportedTypeDefinitions())
885885

886886
// Exports type
887+
let hierarchicalExportLines = try namespaceBuilder.buildHierarchicalExportsType(
888+
exportedSkeletons: exportedSkeletons,
889+
renderClassEntry: { klass in
890+
let (_, _, dtsExportEntry) = try self.renderExportedClass(klass)
891+
return dtsExportEntry
892+
},
893+
renderFunctionSignature: { function in
894+
"\(function.name)\(self.renderTSSignature(parameters: function.parameters, returnType: function.returnType, effects: function.effects));"
895+
}
896+
)
887897
printer.write("export type Exports = {")
888898
printer.indent {
889899
// Add non-namespaced items
890900
printer.write(lines: data.dtsExportLines)
891-
892901
// Add hierarchical namespaced items
893-
let hierarchicalLines = namespaceBuilder.buildHierarchicalExportsType(
894-
exportedSkeletons: exportedSkeletons,
895-
renderClassEntry: { klass in
896-
let printer = CodeFragmentPrinter()
897-
printer.write("\(klass.name): {")
898-
printer.indent {
899-
if let constructor = klass.constructor {
900-
printer.write(
901-
"new\(self.renderTSSignature(parameters: constructor.parameters, returnType: .swiftHeapObject(klass.name), effects: constructor.effects));"
902-
)
903-
}
904-
// Static methods and static properties belong on the namespace
905-
// entry alongside the constructor (same shape that
906-
// `renderExportedClass` produces for non-namespaced classes via
907-
// `dtsExportEntryPrinter`).
908-
for method in klass.methods where method.effects.isStatic {
909-
printer.write(
910-
"\(method.name)\(self.renderTSSignature(parameters: method.parameters, returnType: method.returnType, effects: method.effects));"
911-
)
912-
}
913-
for property in klass.properties where property.isStatic {
914-
let readonly = property.isReadonly ? "readonly " : ""
915-
printer.write("\(readonly)\(property.name): \(property.type.tsType);")
916-
}
917-
}
918-
printer.write("}")
919-
return printer.lines
920-
},
921-
renderFunctionSignature: { function in
922-
"\(function.name)\(self.renderTSSignature(parameters: function.parameters, returnType: function.returnType, effects: function.effects));"
923-
}
924-
)
925-
printer.write(lines: hierarchicalLines)
902+
printer.write(lines: hierarchicalExportLines)
926903
}
927904
printer.write("}")
928905

@@ -1118,7 +1095,7 @@ public struct BridgeJSLink {
11181095
}
11191096
let data = try collectLinkData()
11201097
let outputJs = try generateJavaScript(data: data)
1121-
let outputDts = generateTypeScript(data: data)
1098+
let outputDts = try generateTypeScript(data: data)
11221099
return (outputJs, outputDts)
11231100
}
11241101

@@ -2670,16 +2647,16 @@ extension BridgeJSLink {
26702647

26712648
fileprivate func buildHierarchicalExportsType(
26722649
exportedSkeletons: [ExportedSkeleton],
2673-
renderClassEntry: (ExportedClass) -> [String],
2650+
renderClassEntry: (ExportedClass) throws -> [String],
26742651
renderFunctionSignature: (ExportedFunction) -> String
2675-
) -> [String] {
2652+
) throws -> [String] {
26762653
let printer = CodeFragmentPrinter()
26772654
let rootNode = NamespaceNode(name: "")
26782655

26792656
buildExportsTree(rootNode: rootNode, exportedSkeletons: exportedSkeletons)
26802657

26812658
for (_, node) in rootNode.children {
2682-
populateTypeScriptExportLines(
2659+
try populateTypeScriptExportLines(
26832660
node: node,
26842661
renderClassEntry: renderClassEntry,
26852662
renderFunctionSignature: renderFunctionSignature
@@ -2693,16 +2670,16 @@ extension BridgeJSLink {
26932670

26942671
private func populateTypeScriptExportLines(
26952672
node: NamespaceNode,
2696-
renderClassEntry: (ExportedClass) -> [String],
2673+
renderClassEntry: (ExportedClass) throws -> [String],
26972674
renderFunctionSignature: (ExportedFunction) -> String
2698-
) {
2675+
) throws {
26992676
for function in node.content.functions {
27002677
let signature = renderFunctionSignature(function)
27012678
node.content.functionDtsLines.append((function.name, [signature]))
27022679
}
27032680

27042681
for klass in node.content.classes {
2705-
let entry = renderClassEntry(klass)
2682+
let entry = try renderClassEntry(klass)
27062683
node.content.classDtsLines.append((klass.name, entry))
27072684
}
27082685

@@ -2711,7 +2688,7 @@ extension BridgeJSLink {
27112688
}
27122689

27132690
for (_, childNode) in node.children {
2714-
populateTypeScriptExportLines(
2691+
try populateTypeScriptExportLines(
27152692
node: childNode,
27162693
renderClassEntry: renderClassEntry,
27172694
renderFunctionSignature: renderFunctionSignature

0 commit comments

Comments
 (0)