Skip to content

Commit 1561681

Browse files
committed
BridgeJS: Introduce extended macro and namespace information extraction and storing
1 parent d62db09 commit 1561681

File tree

9 files changed

+868
-65
lines changed

9 files changed

+868
-65
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ class ExportSwift {
123123
}
124124

125125
private func visitFunction(node: FunctionDeclSyntax) -> ExportedFunction? {
126-
guard node.attributes.hasJSAttribute() else {
126+
guard let jsAttribute = node.attributes.firstJSAttribute else {
127127
return nil
128128
}
129+
129130
let name = node.name.text
131+
let namespace = extractNamespace(from: jsAttribute)
132+
130133
var parameters: [Parameter] = []
131134
for param in node.signature.parameterClause.parameters {
132135
guard let type = self.parent.lookupType(for: param.type) else {
@@ -165,7 +168,8 @@ class ExportSwift {
165168
abiName: abiName,
166169
parameters: parameters,
167170
returnType: returnType,
168-
effects: effects
171+
effects: effects,
172+
namespace: namespace
169173
)
170174
}
171175

@@ -192,6 +196,17 @@ class ExportSwift {
192196
}
193197
return Effects(isAsync: isAsync, isThrows: isThrows)
194198
}
199+
200+
private func extractNamespace(
201+
from jsAttribute: AttributeSyntax
202+
) -> [String]? {
203+
guard let arguments = jsAttribute.arguments?.as(LabeledExprListSyntax.self),
204+
let firstArg = arguments.first?.expression.as(StringLiteralExprSyntax.self),
205+
let namespaceString = firstArg.segments.first?.as(StringSegmentSyntax.self)?.content.text else {
206+
return nil
207+
}
208+
return namespaceString.split(separator: ".").map(String.init)
209+
}
195210

196211
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
197212
guard node.attributes.hasJSAttribute() else { return .skipChildren }
@@ -225,13 +240,17 @@ class ExportSwift {
225240

226241
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
227242
let name = node.name.text
243+
228244
stateStack.push(state: .classBody(name: name))
229245

230-
guard node.attributes.hasJSAttribute() else { return .skipChildren }
246+
guard let jsAttribute = node.attributes.firstJSAttribute else { return .skipChildren }
247+
248+
let namespace = extractNamespace(from: jsAttribute)
231249
exportedClassByName[name] = ExportedClass(
232250
name: name,
233251
constructor: nil,
234-
methods: []
252+
methods: [],
253+
namespace: namespace
235254
)
236255
exportedClassNames.append(name)
237256
return .visitChildren
@@ -635,9 +654,13 @@ class ExportSwift {
635654

636655
extension AttributeListSyntax {
637656
fileprivate func hasJSAttribute() -> Bool {
638-
return first(where: {
657+
firstJSAttribute != nil
658+
}
659+
660+
fileprivate var firstJSAttribute: AttributeSyntax? {
661+
first(where: {
639662
$0.as(AttributeSyntax.self)?.attributeName.trimmedDescription == "JS"
640-
}) != nil
663+
})?.as(AttributeSyntax.self)
641664
}
642665
}
643666

0 commit comments

Comments
 (0)