@@ -88,147 +88,152 @@ private enum JSImportMacroHelper {
8888}
8989
9090extension JSImportFunctionMacro : BodyMacro {
91- package static func expansion(
92- of node: AttributeSyntax ,
93- providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax ,
94- in context: some MacroExpansionContext
95- ) throws -> [ CodeBlockItemSyntax ] {
96- if let functionDecl = declaration. as ( FunctionDeclSyntax . self) {
97- let enclosingTypeName = JSImportMacroHelper . enclosingTypeName ( from: context)
98- let isStatic = JSImportMacroHelper . isStatic ( functionDecl. modifiers)
99- let isInstanceMember = enclosingTypeName != nil && !isStatic
91+ package static func expansion(
92+ of node: AttributeSyntax ,
93+ providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax ,
94+ in context: some MacroExpansionContext
95+ ) throws -> [ CodeBlockItemSyntax ] {
96+ if let functionDecl = declaration. as ( FunctionDeclSyntax . self) {
97+ let enclosingTypeName = JSImportMacroHelper . enclosingTypeName ( from: context)
98+ let isStatic = JSImportMacroHelper . isStatic ( functionDecl. modifiers)
99+ let isInstanceMember = enclosingTypeName != nil && !isStatic
100+
101+ let name = functionDecl. name. text
102+ let glueName = JSImportMacroHelper . glueName ( baseName: name, enclosingTypeName: enclosingTypeName)
103+
104+ var arguments : [ String ] = [ ]
105+ if isInstanceMember {
106+ arguments. append ( " self.jsObject " )
107+ }
108+ arguments. append (
109+ contentsOf: JSImportMacroHelper . parameterNames ( functionDecl. signature. parameterClause. parameters)
110+ )
100111
101- let name = functionDecl . name . text
102- let glueName = JSImportMacroHelper . glueName ( baseName : name , enclosingTypeName : enclosingTypeName )
112+ let argsJoined = arguments . joined ( separator : " , " )
113+ let call = " \( glueName) ( \( argsJoined ) ) "
103114
104- var arguments : [ String ] = [ ]
105- if isInstanceMember {
106- arguments. append ( " self.jsObject " )
115+ let effects = functionDecl. signature. effectSpecifiers
116+ let isAsync = effects? . asyncSpecifier != nil
117+ let isThrows = effects? . throwsClause != nil
118+ let prefix = JSImportMacroHelper . tryAwaitPrefix ( isAsync: isAsync, isThrows: isThrows)
119+
120+ let isVoid = JSImportMacroHelper . isVoidReturn ( functionDecl. signature. returnClause? . type)
121+ let line = isVoid ? " \( prefix) \( call) " : " return \( prefix) \( call) "
122+ return [ CodeBlockItemSyntax ( stringLiteral: line) ]
107123 }
108- arguments. append ( contentsOf: JSImportMacroHelper . parameterNames ( functionDecl. signature. parameterClause. parameters) )
109124
110- let argsJoined = arguments. joined ( separator: " , " )
111- let call = " \( glueName) ( \( argsJoined) ) "
125+ if let initializerDecl = declaration. as ( InitializerDeclSyntax . self) {
126+ guard let enclosingTypeName = JSImportMacroHelper . enclosingTypeName ( from: context) else {
127+ context. diagnose (
128+ Diagnostic ( node: Syntax ( declaration) , message: JSImportMacroMessage . unsupportedDeclaration)
129+ )
130+ return [ ]
131+ }
112132
113- let effects = functionDecl . signature . effectSpecifiers
114- let isAsync = effects ? . asyncSpecifier != nil
115- let isThrows = effects ? . throwsClause != nil
116- let prefix = JSImportMacroHelper . tryAwaitPrefix ( isAsync : isAsync , isThrows : isThrows )
133+ let glueName = JSImportMacroHelper . glueName ( baseName : " init " , enclosingTypeName : enclosingTypeName )
134+ let parameters = initializerDecl . signature . parameterClause . parameters
135+ let arguments = JSImportMacroHelper . parameterNames ( parameters )
136+ let call = " \( glueName ) ( \( arguments . joined ( separator : " , " ) ) ) "
117137
118- let isVoid = JSImportMacroHelper . isVoidReturn ( functionDecl . signature. returnClause ? . type )
119- let line = isVoid ? " \( prefix ) \( call ) " : " return \( prefix ) \( call ) "
120- return [ CodeBlockItemSyntax ( stringLiteral : line ) ]
121- }
138+ let effects = initializerDecl . signature. effectSpecifiers
139+ let isAsync = effects ? . asyncSpecifier != nil
140+ let isThrows = effects ? . throwsClause != nil
141+ let prefix = JSImportMacroHelper . tryAwaitPrefix ( isAsync : isAsync , isThrows : isThrows )
122142
123- if let initializerDecl = declaration . as ( InitializerDeclSyntax . self ) {
124- guard let enclosingTypeName = JSImportMacroHelper . enclosingTypeName ( from : context ) else {
125- context . diagnose ( Diagnostic ( node : Syntax ( declaration ) , message : JSImportMacroMessage . unsupportedDeclaration ) )
126- return [ ]
143+ return [
144+ CodeBlockItemSyntax ( stringLiteral : " let jsObject = \( prefix ) \( call ) " ) ,
145+ CodeBlockItemSyntax ( stringLiteral : " self.init(unsafelyWrapping: jsObject) " ) ,
146+ ]
127147 }
128148
129- let glueName = JSImportMacroHelper . glueName ( baseName: " init " , enclosingTypeName: enclosingTypeName)
130- let parameters = initializerDecl. signature. parameterClause. parameters
131- let arguments = JSImportMacroHelper . parameterNames ( parameters)
132- let call = " \( glueName) ( \( arguments. joined ( separator: " , " ) ) ) "
133-
134- let effects = initializerDecl. signature. effectSpecifiers
135- let isAsync = effects? . asyncSpecifier != nil
136- let isThrows = effects? . throwsClause != nil
137- let prefix = JSImportMacroHelper . tryAwaitPrefix ( isAsync: isAsync, isThrows: isThrows)
138-
139- return [
140- CodeBlockItemSyntax ( stringLiteral: " let jsObject = \( prefix) \( call) " ) ,
141- CodeBlockItemSyntax ( stringLiteral: " self.init(unsafelyWrapping: jsObject) " ) ,
142- ]
149+ context. diagnose ( Diagnostic ( node: Syntax ( declaration) , message: JSImportMacroMessage . unsupportedDeclaration) )
150+ return [ ]
143151 }
144-
145- context. diagnose ( Diagnostic ( node: Syntax ( declaration) , message: JSImportMacroMessage . unsupportedDeclaration) )
146- return [ ]
147- }
148152}
149153
150154enum JSImportVariableMacro { }
151155
152156extension JSImportVariableMacro : AccessorMacro {
153- package static func expansion(
154- of node: AttributeSyntax ,
155- providingAccessorsOf declaration: some DeclSyntaxProtocol ,
156- in context: some MacroExpansionContext
157- ) throws -> [ AccessorDeclSyntax ] {
158- guard let variableDecl = declaration. as ( VariableDeclSyntax . self) ,
159- let binding = variableDecl. bindings. first,
160- let identifier = binding. pattern. as ( IdentifierPatternSyntax . self)
161- else {
162- context. diagnose ( Diagnostic ( node: Syntax ( declaration) , message: JSImportMacroMessage . unsupportedVariable) )
163- return [ ]
164- }
157+ package static func expansion(
158+ of node: AttributeSyntax ,
159+ providingAccessorsOf declaration: some DeclSyntaxProtocol ,
160+ in context: some MacroExpansionContext
161+ ) throws -> [ AccessorDeclSyntax ] {
162+ guard let variableDecl = declaration. as ( VariableDeclSyntax . self) ,
163+ let binding = variableDecl. bindings. first,
164+ let identifier = binding. pattern. as ( IdentifierPatternSyntax . self)
165+ else {
166+ context. diagnose ( Diagnostic ( node: Syntax ( declaration) , message: JSImportMacroMessage . unsupportedVariable) )
167+ return [ ]
168+ }
165169
166- let enclosingTypeName = JSImportMacroHelper . enclosingTypeName ( from: context)
167- let isStatic = JSImportMacroHelper . isStatic ( variableDecl. modifiers)
168- let isInstanceMember = enclosingTypeName != nil && !isStatic
169-
170- let propertyName = identifier. identifier. text
171- let getterName = JSImportMacroHelper . glueName (
172- baseName: propertyName,
173- enclosingTypeName: enclosingTypeName,
174- operation: " get "
175- )
176- let setterName = JSImportMacroHelper . glueName (
177- baseName: propertyName,
178- enclosingTypeName: enclosingTypeName,
179- operation: " set "
180- )
181-
182- var getterArgs : [ String ] = [ ]
183- if isInstanceMember {
184- getterArgs. append ( " self.jsObject " )
185- }
186- let getterCall : CodeBlockItemSyntax = " return try \( raw: getterName) ( \( raw: getterArgs. joined ( separator: " , " ) ) ) "
187-
188- let throwsClause = ThrowsClauseSyntax (
189- throwsSpecifier: . keyword( . throws) ,
190- leftParen: . leftParenToken( ) ,
191- type: IdentifierTypeSyntax ( name: . identifier( " JSException " ) ) ,
192- rightParen: . rightParenToken( )
193- )
194-
195- var accessors : [ AccessorDeclSyntax ] = [
196- AccessorDeclSyntax (
197- accessorSpecifier: . keyword( . get) ,
198- effectSpecifiers: AccessorEffectSpecifiersSyntax (
199- asyncSpecifier: nil ,
200- throwsClause: throwsClause
201- ) ,
202- body: CodeBlockSyntax {
203- getterCall
204- }
170+ let enclosingTypeName = JSImportMacroHelper . enclosingTypeName ( from: context)
171+ let isStatic = JSImportMacroHelper . isStatic ( variableDecl. modifiers)
172+ let isInstanceMember = enclosingTypeName != nil && !isStatic
173+
174+ let propertyName = identifier. identifier. text
175+ let getterName = JSImportMacroHelper . glueName (
176+ baseName: propertyName,
177+ enclosingTypeName: enclosingTypeName,
178+ operation: " get "
179+ )
180+ let setterName = JSImportMacroHelper . glueName (
181+ baseName: propertyName,
182+ enclosingTypeName: enclosingTypeName,
183+ operation: " set "
205184 )
206- ]
207185
208- let isLet = variableDecl. bindingSpecifier. tokenKind == . keyword( . let)
209- if !isLet {
210- var setterArgs : [ String ] = [ ]
186+ var getterArgs : [ String ] = [ ]
211187 if isInstanceMember {
212- setterArgs . append ( " self.jsObject " )
188+ getterArgs . append ( " self.jsObject " )
213189 }
214- setterArgs. append ( " newValue " )
215- let setterCall : CodeBlockItemSyntax = " try \( raw: setterName) ( \( raw: setterArgs. joined ( separator: " , " ) ) ) "
216- accessors. append (
190+ let getterCall : CodeBlockItemSyntax =
191+ " return try \( raw: getterName) ( \( raw: getterArgs. joined ( separator: " , " ) ) ) "
192+
193+ let throwsClause = ThrowsClauseSyntax (
194+ throwsSpecifier: . keyword( . throws) ,
195+ leftParen: . leftParenToken( ) ,
196+ type: IdentifierTypeSyntax ( name: . identifier( " JSException " ) ) ,
197+ rightParen: . rightParenToken( )
198+ )
199+
200+ var accessors : [ AccessorDeclSyntax ] = [
217201 AccessorDeclSyntax (
218- accessorSpecifier: . keyword( . set ) ,
202+ accessorSpecifier: . keyword( . get ) ,
219203 effectSpecifiers: AccessorEffectSpecifiersSyntax (
220204 asyncSpecifier: nil ,
221205 throwsClause: throwsClause
222206 ) ,
223207 body: CodeBlockSyntax {
224- setterCall
208+ getterCall
225209 }
226210 )
227- )
228- }
211+ ]
212+
213+ let isLet = variableDecl. bindingSpecifier. tokenKind == . keyword( . let)
214+ if !isLet {
215+ var setterArgs : [ String ] = [ ]
216+ if isInstanceMember {
217+ setterArgs. append ( " self.jsObject " )
218+ }
219+ setterArgs. append ( " newValue " )
220+ let setterCall : CodeBlockItemSyntax = " try \( raw: setterName) ( \( raw: setterArgs. joined ( separator: " , " ) ) ) "
221+ accessors. append (
222+ AccessorDeclSyntax (
223+ accessorSpecifier: . keyword( . set) ,
224+ effectSpecifiers: AccessorEffectSpecifiersSyntax (
225+ asyncSpecifier: nil ,
226+ throwsClause: throwsClause
227+ ) ,
228+ body: CodeBlockSyntax {
229+ setterCall
230+ }
231+ )
232+ )
233+ }
229234
230- return accessors
231- }
235+ return accessors
236+ }
232237}
233238
234239enum JSImportClassMacro { }
0 commit comments