Skip to content

Commit e701702

Browse files
committed
BridgeJS: fix: adding name for namespace parameter
BridgeJS: fix: run swiftformat BridgeJS: chore: extend docc documentaion with namespace example
1 parent 5ca3335 commit e701702

File tree

6 files changed

+257
-149
lines changed

6 files changed

+257
-149
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,18 @@ class ExportSwift {
126126
guard let jsAttribute = node.attributes.firstJSAttribute else {
127127
return nil
128128
}
129-
129+
130130
let name = node.name.text
131131
let namespace = extractNamespace(from: jsAttribute)
132-
132+
133133
if namespace != nil, case .classBody = state {
134134
diagnose(
135135
node: jsAttribute,
136136
message: "Namespace is only needed in top-level declaration",
137137
hint: "Remove the namespace from @JS attribute or move this function to top-level"
138138
)
139139
}
140-
140+
141141
var parameters: [Parameter] = []
142142
for param in node.signature.parameterClause.parameters {
143143
guard let type = self.parent.lookupType(for: param.type) else {
@@ -204,15 +204,21 @@ class ExportSwift {
204204
}
205205
return Effects(isAsync: isAsync, isThrows: isThrows)
206206
}
207-
207+
208208
private func extractNamespace(
209209
from jsAttribute: AttributeSyntax
210210
) -> [String]? {
211-
guard let arguments = jsAttribute.arguments?.as(LabeledExprListSyntax.self),
212-
let firstArg = arguments.first?.expression.as(StringLiteralExprSyntax.self),
213-
let namespaceString = firstArg.segments.first?.as(StringSegmentSyntax.self)?.content.text else {
214-
return nil
211+
guard let arguments = jsAttribute.arguments?.as(LabeledExprListSyntax.self) else {
212+
return nil
215213
}
214+
215+
guard let namespaceArg = arguments.first(where: { $0.label?.text == "namespace" }),
216+
let stringLiteral = namespaceArg.expression.as(StringLiteralExprSyntax.self),
217+
let namespaceString = stringLiteral.segments.first?.as(StringSegmentSyntax.self)?.content.text
218+
else {
219+
return nil
220+
}
221+
216222
return namespaceString.split(separator: ".").map(String.init)
217223
}
218224

@@ -222,17 +228,17 @@ class ExportSwift {
222228
diagnose(node: node, message: "@JS init must be inside a @JS class")
223229
return .skipChildren
224230
}
225-
231+
226232
if let jsAttribute = node.attributes.firstJSAttribute,
227-
let namespace = extractNamespace(from: jsAttribute),
228-
namespace != nil {
233+
extractNamespace(from: jsAttribute) != nil
234+
{
229235
diagnose(
230236
node: jsAttribute,
231237
message: "Namespace is not supported for initializer declarations",
232238
hint: "Remove the namespace from @JS attribute"
233239
)
234240
}
235-
241+
236242
var parameters: [Parameter] = []
237243
for param in node.signature.parameterClause.parameters {
238244
guard let type = self.parent.lookupType(for: param.type) else {
@@ -259,7 +265,7 @@ class ExportSwift {
259265

260266
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
261267
let name = node.name.text
262-
268+
263269
stateStack.push(state: .classBody(name: name))
264270

265271
guard let jsAttribute = node.attributes.firstJSAttribute else { return .skipChildren }
@@ -675,7 +681,7 @@ extension AttributeListSyntax {
675681
fileprivate func hasJSAttribute() -> Bool {
676682
firstJSAttribute != nil
677683
}
678-
684+
679685
fileprivate var firstJSAttribute: AttributeSyntax? {
680686
first(where: {
681687
$0.as(AttributeSyntax.self)?.attributeName.trimmedDescription == "JS"

0 commit comments

Comments
 (0)