11import SwiftDiagnostics
22import SwiftSyntax
3+ import SwiftSyntaxMacroExpansion
34import SwiftSyntaxMacros
45import SwiftSyntaxMacrosTestSupport
56import Testing
67import BridgeJSMacros
78
89@Suite struct JSClassMacroTests {
910 private let indentationWidth : Trivia = . spaces( 4 )
11+ private let macroSpecs : [ String : MacroSpec ] = [
12+ " JSClass " : MacroSpec ( type: JSClassMacro . self, conformances: [ " _JSBridgedClass " ] )
13+ ]
1014
1115 @Test func emptyStruct( ) {
1216 assertMacroExpansion (
@@ -23,8 +27,11 @@ import BridgeJSMacros
2327 self.jsObject = jsObject
2428 }
2529 }
30+
31+ extension MyClass: _JSBridgedClass {
32+ }
2633 """ ,
27- macros : [ " JSClass " : JSClassMacro . self ] ,
34+ macroSpecs : macroSpecs ,
2835 indentationWidth: indentationWidth
2936 )
3037 }
@@ -45,8 +52,11 @@ import BridgeJSMacros
4552 self.jsObject = jsObject
4653 }
4754 }
55+
56+ extension MyClass: _JSBridgedClass {
57+ }
4858 """ ,
49- macros : [ " JSClass " : JSClassMacro . self ] ,
59+ macroSpecs : macroSpecs ,
5060 indentationWidth: indentationWidth
5161 )
5262 }
@@ -69,8 +79,11 @@ import BridgeJSMacros
6979 self.jsObject = jsObject
7080 }
7181 }
82+
83+ extension MyClass: _JSBridgedClass {
84+ }
7285 """ ,
73- macros : [ " JSClass " : JSClassMacro . self ] ,
86+ macroSpecs : macroSpecs ,
7487 indentationWidth: indentationWidth
7588 )
7689 }
@@ -95,8 +108,11 @@ import BridgeJSMacros
95108 self.jsObject = jsObject
96109 }
97110 }
111+
112+ extension MyClass: _JSBridgedClass {
113+ }
98114 """ ,
99- macros : [ " JSClass " : JSClassMacro . self ] ,
115+ macroSpecs : macroSpecs ,
100116 indentationWidth: indentationWidth
101117 )
102118 }
@@ -119,8 +135,11 @@ import BridgeJSMacros
119135 self.jsObject = jsObject
120136 }
121137 }
138+
139+ extension MyClass: _JSBridgedClass {
140+ }
122141 """ ,
123- macros : [ " JSClass " : JSClassMacro . self ] ,
142+ macroSpecs : macroSpecs ,
124143 indentationWidth: indentationWidth
125144 )
126145 }
@@ -140,8 +159,11 @@ import BridgeJSMacros
140159 self.jsObject = jsObject
141160 }
142161 }
162+
163+ extension MyClass: _JSBridgedClass {
164+ }
143165 """ ,
144- macros : [ " JSClass " : JSClassMacro . self ] ,
166+ macroSpecs : macroSpecs ,
145167 indentationWidth: indentationWidth
146168 )
147169 }
@@ -161,8 +183,11 @@ import BridgeJSMacros
161183 self.jsObject = jsObject
162184 }
163185 }
186+
187+ extension MyEnum: _JSBridgedClass {
188+ }
164189 """ ,
165- macros : [ " JSClass " : JSClassMacro . self ] ,
190+ macroSpecs : macroSpecs ,
166191 indentationWidth: indentationWidth
167192 )
168193 }
@@ -182,8 +207,11 @@ import BridgeJSMacros
182207 self.jsObject = jsObject
183208 }
184209 }
210+
211+ extension MyActor: _JSBridgedClass {
212+ }
185213 """ ,
186- macros : [ " JSClass " : JSClassMacro . self ] ,
214+ macroSpecs : macroSpecs ,
187215 indentationWidth: indentationWidth
188216 )
189217 }
@@ -206,8 +234,11 @@ import BridgeJSMacros
206234 self.jsObject = jsObject
207235 }
208236 }
237+
238+ extension MyClass: _JSBridgedClass {
239+ }
209240 """ ,
210- macros : [ " JSClass " : JSClassMacro . self ] ,
241+ macroSpecs : macroSpecs ,
211242 indentationWidth: indentationWidth
212243 )
213244 }
@@ -232,8 +263,11 @@ import BridgeJSMacros
232263 self.jsObject = jsObject
233264 }
234265 }
266+
267+ extension MyClass: _JSBridgedClass {
268+ }
235269 """ ,
236- macros : [ " JSClass " : JSClassMacro . self ] ,
270+ macroSpecs : macroSpecs ,
237271 indentationWidth: indentationWidth
238272 )
239273 }
@@ -258,8 +292,11 @@ import BridgeJSMacros
258292 self.jsObject = jsObject
259293 }
260294 }
295+
296+ extension MyClass: _JSBridgedClass {
297+ }
261298 """ ,
262- macros : [ " JSClass " : JSClassMacro . self ] ,
299+ macroSpecs : macroSpecs ,
263300 indentationWidth: indentationWidth
264301 )
265302 }
@@ -281,8 +318,32 @@ import BridgeJSMacros
281318 self.jsObject = jsObject
282319 }
283320 }
321+
322+ extension MyClass: _JSBridgedClass {
323+ }
324+ """ ,
325+ macroSpecs: macroSpecs,
326+ indentationWidth: indentationWidth
327+ )
328+ }
329+
330+ @Test func structAlreadyConforms( ) {
331+ assertMacroExpansion (
332+ """
333+ @JSClass
334+ struct MyClass: _JSBridgedClass {
335+ }
336+ """ ,
337+ expandedSource: """
338+ struct MyClass: _JSBridgedClass {
339+ let jsObject: JSObject
340+
341+ init(unsafelyWrapping jsObject: JSObject) {
342+ self.jsObject = jsObject
343+ }
344+ }
284345 """ ,
285- macros : [ " JSClass " : JSClassMacro . self ] ,
346+ macroSpecs : macroSpecs ,
286347 indentationWidth: indentationWidth
287348 )
288349 }
0 commit comments