Skip to content

Commit 27272e9

Browse files
committed
BridgeJS: Simplify exported type naming and its fixtures
1 parent 7a6d643 commit 27272e9

10 files changed

Lines changed: 21 additions & 2917 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,19 +2208,11 @@ private final class ExportSwiftAPICollector: SyntaxAnyVisitor {
22082208
message: "Enum visibility must be at least internal"
22092209
)
22102210

2211-
let tsFullPath: String
2212-
if let namespace = effectiveNamespace, !namespace.isEmpty {
2213-
tsFullPath = namespace.joined(separator: ".") + "." + name
2214-
} else {
2215-
tsFullPath = name
2216-
}
2217-
22182211
// Create enum directly in dictionary
22192212
let exportedEnum = ExportedEnum(
22202213
name: name,
22212214
jsName: jsName,
22222215
swiftCallName: swiftCallName,
2223-
tsFullPath: tsFullPath,
22242216
explicitAccessControl: explicitAccessControl,
22252217
cases: [], // Will be populated in visit(EnumCaseDeclSyntax)
22262218
rawType: SwiftEnumRawType(rawType),

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ public struct BridgeJSLink {
405405
scope: JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry),
406406
printer: printer,
407407
hasDirectAccessToSwiftClass: false,
408-
classNamespaces: intrinsicRegistry.classNamespaces
409408
)
410409
}
411410

@@ -1226,7 +1225,6 @@ public struct BridgeJSLink {
12261225
scope: structScope,
12271226
printer: structPrinter,
12281227
hasDirectAccessToSwiftClass: false,
1229-
classNamespaces: intrinsicRegistry.classNamespaces
12301228
)
12311229
)
12321230
bodyPrinter.write(lines: structPrinter.lines)
@@ -1248,7 +1246,6 @@ public struct BridgeJSLink {
12481246
scope: enumScope,
12491247
printer: enumPrinter,
12501248
hasDirectAccessToSwiftClass: false,
1251-
classNamespaces: intrinsicRegistry.classNamespaces
12521249
)
12531250
)
12541251
bodyPrinter.write(lines: enumPrinter.lines)
@@ -1348,14 +1345,6 @@ public struct BridgeJSLink {
13481345
public func link(sharedMemory: Bool = false) throws -> (outputJs: String, outputDts: String) {
13491346
intrinsicRegistry.reset()
13501347
importedModuleRegistry.configure(skeletons: skeletons)
1351-
intrinsicRegistry.classNamespaces = skeletons.reduce(into: [:]) { result, unified in
1352-
guard let skeleton = unified.exported else { return }
1353-
for klass in skeleton.classes {
1354-
if let namespace = klass.resolvedJSNamespace {
1355-
result[klass.name] = namespace
1356-
}
1357-
}
1358-
}
13591348
intrinsicRegistry.classPaths = skeletons.reduce(into: [:]) { result, unified in
13601349
for klass in unified.exported?.classes ?? [] {
13611350
result[klass.swiftCallName] = klass.tsPathComponents
@@ -1581,7 +1570,6 @@ public struct BridgeJSLink {
15811570
scope: scope,
15821571
printer: body,
15831572
hasDirectAccessToSwiftClass: hasDirectAccessToSwiftClass,
1584-
classNamespaces: intrinsicRegistry.classNamespaces
15851573
)
15861574
}
15871575

@@ -2617,7 +2605,6 @@ extension BridgeJSLink {
26172605
scope: scope,
26182606
printer: body,
26192607
hasDirectAccessToSwiftClass: false,
2620-
classNamespaces: intrinsicRegistry.classNamespaces
26212608
)
26222609
}
26232610

@@ -4158,14 +4145,15 @@ extension BridgeJSLink {
41584145
fileprivate func renderJSDoc(documentation: String?, parameters: [Parameter]) -> [String] {
41594146
let parsed = documentation.map(DocCComment.init(parsing:)) ?? DocCComment()
41604147

4148+
let resolveTypeName = makeCodecPrintContext(printer: CodeFragmentPrinter()).defaultValueTypeName
41614149
var tagLines: [String] = []
41624150
for parameter in parameters {
41634151
let docText = parsed.parameter(named: parameter.name)
41644152
let defaultValue = parameter.defaultValue.map {
41654153
DefaultValueUtils.format(
41664154
$0,
41674155
as: .typescript,
4168-
resolveTypeName: makeCodecPrintContext(printer: CodeFragmentPrinter()).defaultValueTypeName
4156+
resolveTypeName: resolveTypeName
41694157
)
41704158
}
41714159
switch (docText, defaultValue) {

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,6 @@ struct IntrinsicJSFragment: Sendable {
540540
/// Whether the fragment has direct access to the SwiftHeapObject classes.
541541
/// If false, the fragment needs to use `_exports` to access the class.
542542
var hasDirectAccessToSwiftClass: Bool = true
543-
/// Maps class names to their namespace path components for resolving `_exports` access.
544-
var classNamespaces: [String: [String]] = [:]
545543

546544
func with<T>(_ keyPath: WritableKeyPath<PrintCodeContext, T>, _ value: T) -> PrintCodeContext {
547545
var new = self
@@ -553,14 +551,6 @@ struct IntrinsicJSFragment: Sendable {
553551
qualifiedName.split(separator: ".").last.map(String.init) ?? qualifiedName
554552
}
555553

556-
private func exportsAccess(forClass name: String) -> String {
557-
if let namespace = classNamespaces[name], !namespace.isEmpty {
558-
let path = namespace.map { ".\($0)" }.joined()
559-
return "_exports\(path).\(name)"
560-
}
561-
return "_exports['\(name)']"
562-
}
563-
564554
func classReference(forQualifiedName qualifiedName: String) -> String {
565555
if let path = scope.exportedClassPath(forSwiftName: qualifiedName), let name = path.last {
566556
if hasDirectAccessToSwiftClass {
@@ -571,8 +561,7 @@ struct IntrinsicJSFragment: Sendable {
571561
if hasDirectAccessToSwiftClass {
572562
return unqualifiedClassName(for: qualifiedName)
573563
}
574-
let unqualified = unqualifiedClassName(for: qualifiedName)
575-
return exportsAccess(forClass: unqualified)
564+
return "_exports['\(unqualifiedClassName(for: qualifiedName))']"
576565
}
577566

578567
func defaultValueTypeName(_ type: BridgeType, _ format: DefaultValueUtils.OutputFormat) -> String? {

Plugins/BridgeJS/Sources/BridgeJSLink/JSIntrinsicRegistry.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import BridgeJSUtilities
55
/// Registry for JS helper intrinsics used during code generation.
66
final class JSIntrinsicRegistry {
77
private var entries: [String: [String]] = [:]
8-
var classNamespaces: [String: [String]] = [:]
98
var classPaths: [String: [String]] = [:]
109
var renamedEnumNames: [String: (value: String, type: String)] = [:]
1110

@@ -44,7 +43,6 @@ final class JSIntrinsicRegistry {
4443

4544
func reset() {
4645
entries.removeAll()
47-
classNamespaces.removeAll()
4846
classPaths.removeAll()
4947
renamedEnumNames.removeAll()
5048
typeOwnerModules.removeAll()

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,6 @@ public struct ExportedEnum: Codable, Equatable, Sendable, NamespacedExportedType
911911
name: String,
912912
jsName: String? = nil,
913913
swiftCallName: String,
914-
tsFullPath: String,
915914
explicitAccessControl: String?,
916915
cases: [EnumCase],
917916
rawType: SwiftEnumRawType?,
@@ -925,9 +924,7 @@ public struct ExportedEnum: Codable, Equatable, Sendable, NamespacedExportedType
925924
self.name = name
926925
self.jsName = jsName
927926
self.swiftCallName = swiftCallName
928-
self.tsFullPath =
929-
jsName != nil || jsNamespace != nil
930-
? ((jsNamespace ?? namespace ?? []) + [jsName ?? name]).joined(separator: ".") : tsFullPath
927+
self.tsFullPath = ((jsNamespace ?? namespace ?? []) + [jsName ?? name]).joined(separator: ".")
931928
self.explicitAccessControl = explicitAccessControl
932929
self.cases = cases
933930
self.rawType = rawType
Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,11 @@
1-
@JS func makeRenamedBox() -> SwiftBox
2-
@JS func transformRenamedRecords(
3-
_ values: [SwiftRecord?],
4-
transform: (SwiftBox?) -> SwiftRecord
5-
) -> [SwiftRecord]
6-
@JS func useRenamedDelegate(_ delegate: SwiftDelegate) -> String
7-
@JS func renamedChoice(_ value: SwiftChoice) -> SwiftChoice
8-
@JS func renamedMode(_ value: SwiftMode) -> SwiftMode
9-
@JS func renamedCode(_ value: SwiftCode) -> SwiftCode
10-
@JS func renamedPayload(_ value: SwiftPayload) -> SwiftPayload
11-
@JS func renamedNested(_ value: SwiftNamespace.SwiftChild) -> SwiftNamespace.SwiftChild
12-
@JS func renamedExtensionNested(_ value: SwiftNamespace.SwiftExtensionChild) -> SwiftNamespace.SwiftExtensionChild
13-
141
@JS("PublicBox") final class SwiftBox {
152
@JS init()
16-
@JS var record: SwiftRecord
173
@JS func copy() -> SwiftBox
18-
@JS static func make() -> SwiftBox
19-
@JS static var current: SwiftBox
20-
}
21-
22-
@JS("PublicRecord") struct SwiftRecord {
23-
var value: Int
24-
var box: SwiftBox?
25-
26-
@JS init(value: Int, box: SwiftBox?)
27-
@JS func copy() -> SwiftRecord
28-
@JS static func make() -> SwiftRecord
29-
@JS static var current: SwiftRecord
30-
}
31-
32-
@JS("PublicDelegate", namespace: "API") protocol SwiftDelegate {
33-
var record: SwiftRecord { get }
34-
func box() -> SwiftBox
354
}
365

376
@JS("PublicChoice") enum SwiftChoice {
387
case first
398
case second
40-
41-
@JS static func defaultValue() -> SwiftChoice
42-
}
43-
44-
@JS("PublicMode", namespace: "API") enum SwiftMode: String {
45-
case active = "active"
46-
case inactive = "inactive"
47-
48-
@JS static var current: SwiftMode
49-
}
50-
51-
@JS("PublicCode", enumStyle: .tsEnum) enum SwiftCode: Int {
52-
case success = 0
53-
case failure = 1
54-
}
55-
56-
@JS("PublicPayload") enum SwiftPayload {
57-
case record(SwiftRecord)
58-
case box(SwiftBox?)
59-
case choice(SwiftChoice)
60-
61-
@JS static func make() -> SwiftPayload
629
}
6310

64-
@JS("PublicNamespace", namespace: "API") enum SwiftNamespace {
65-
@JS static func child() -> SwiftChild
66-
67-
@JS("PublicChild") struct SwiftChild {
68-
var value: Int
69-
@JS static func make() -> SwiftChild
70-
}
71-
72-
@JS("PublicNestedNamespace") enum SwiftNestedNamespace {
73-
@JS static func box() -> SwiftBox
74-
}
75-
}
76-
77-
extension SwiftNamespace {
78-
@JS("PublicExtensionChild") struct SwiftExtensionChild {
79-
var child: SwiftChild
80-
@JS static func make() -> SwiftExtensionChild
81-
}
82-
}
83-
84-
@JS("PublicParent", namespace: "API") final class SwiftParent {
85-
@JS init()
86-
87-
@JS("PublicNestedRecord") struct SwiftNestedRecord {
88-
var value: Int
89-
}
90-
}
91-
92-
extension SwiftParent {
93-
@JS("PublicNestedBox") final class SwiftNestedBox {
94-
@JS init()
95-
@JS static func make() -> SwiftNestedBox
96-
}
97-
}
98-
99-
@JS("PublicContainer") struct SwiftContainer {
100-
var value: Int
101-
102-
@JS("PublicContainedRecord") struct SwiftContainedRecord {
103-
var value: Int
104-
}
105-
}
11+
@JS func renamedChoice(_ value: SwiftChoice) -> SwiftChoice

0 commit comments

Comments
 (0)