Skip to content

Commit 987b088

Browse files
Restrict @jsclass to structs (#523)
- Updated `@JSClass` macro to emit an error unless it’s attached to a `struct` (`Plugins/BridgeJS/Sources/BridgeJSMacros/JSClassMacro.swift:13`). - Added new diagnostic message `@JSClass can only be applied to structs.` (`Plugins/BridgeJS/Sources/BridgeJSMacros/JSMacroSupport.swift:8`). - Updated macro expansion tests so `class`/`enum`/`actor` usages now produce diagnostics and no generated members/extensions (`Plugins/BridgeJS/Tests/BridgeJSMacrosTests/JSClassMacroTests.swift:147`). - Verified with `swift test --package-path ./Plugins/BridgeJS --filter BridgeJSMacrosTests`.
1 parent 38a937c commit 987b088

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

Plugins/BridgeJS/Sources/BridgeJSMacros/JSClassMacro.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ extension JSClassMacro: MemberMacro {
1212
conformingTo protocols: [TypeSyntax],
1313
in context: some MacroExpansionContext
1414
) throws -> [DeclSyntax] {
15+
guard declaration.is(StructDeclSyntax.self) else {
16+
context.diagnose(
17+
Diagnostic(node: Syntax(declaration), message: JSMacroMessage.unsupportedJSClassDeclaration)
18+
)
19+
return []
20+
}
21+
1522
var members: [DeclSyntax] = []
1623

1724
let existingMembers = declaration.memberBlock.members
@@ -59,6 +66,7 @@ extension JSClassMacro: ExtensionMacro {
5966
conformingTo protocols: [TypeSyntax],
6067
in context: some MacroExpansionContext
6168
) throws -> [ExtensionDeclSyntax] {
69+
guard declaration.is(StructDeclSyntax.self) else { return [] }
6270
guard !protocols.isEmpty else { return [] }
6371

6472
let conformanceList = protocols.map { $0.trimmed.description }.joined(separator: ", ")

Plugins/BridgeJS/Sources/BridgeJSMacros/JSMacroSupport.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import SwiftDiagnostics
55

66
enum JSMacroMessage: String, DiagnosticMessage {
77
case unsupportedDeclaration = "@JSFunction can only be applied to functions or initializers."
8+
case unsupportedJSClassDeclaration = "@JSClass can only be applied to structs."
89
case unsupportedVariable = "@JSGetter can only be applied to single-variable declarations."
910
case unsupportedSetterDeclaration = "@JSSetter can only be applied to functions."
1011
case invalidSetterName =

Plugins/BridgeJS/Tests/BridgeJSMacrosTests/JSClassMacroTests.swift

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,15 @@ import BridgeJSMacros
153153
""",
154154
expandedSource: """
155155
class MyClass {
156-
let jsObject: JSObject
157-
158-
init(unsafelyWrapping jsObject: JSObject) {
159-
self.jsObject = jsObject
160-
}
161-
}
162-
163-
extension MyClass: _JSBridgedClass {
164156
}
165157
""",
158+
diagnostics: [
159+
DiagnosticSpec(
160+
message: "@JSClass can only be applied to structs.",
161+
line: 1,
162+
column: 1
163+
)
164+
],
166165
macroSpecs: macroSpecs,
167166
indentationWidth: indentationWidth
168167
)
@@ -177,16 +176,15 @@ import BridgeJSMacros
177176
""",
178177
expandedSource: """
179178
enum MyEnum {
180-
let jsObject: JSObject
181-
182-
init(unsafelyWrapping jsObject: JSObject) {
183-
self.jsObject = jsObject
184-
}
185-
}
186-
187-
extension MyEnum: _JSBridgedClass {
188179
}
189180
""",
181+
diagnostics: [
182+
DiagnosticSpec(
183+
message: "@JSClass can only be applied to structs.",
184+
line: 1,
185+
column: 1
186+
)
187+
],
190188
macroSpecs: macroSpecs,
191189
indentationWidth: indentationWidth
192190
)
@@ -201,16 +199,15 @@ import BridgeJSMacros
201199
""",
202200
expandedSource: """
203201
actor MyActor {
204-
let jsObject: JSObject
205-
206-
init(unsafelyWrapping jsObject: JSObject) {
207-
self.jsObject = jsObject
208-
}
209-
}
210-
211-
extension MyActor: _JSBridgedClass {
212202
}
213203
""",
204+
diagnostics: [
205+
DiagnosticSpec(
206+
message: "@JSClass can only be applied to structs.",
207+
line: 1,
208+
column: 1
209+
)
210+
],
214211
macroSpecs: macroSpecs,
215212
indentationWidth: indentationWidth
216213
)

0 commit comments

Comments
 (0)