|
| 1 | +import SwiftCompilerPlugin |
| 2 | +import SwiftSyntax |
| 3 | +import SwiftSyntaxBuilder |
| 4 | +import SwiftSyntaxMacros |
| 5 | + |
| 6 | +/// A macro which generate `mutating func` for all variables inside struct. |
| 7 | +public struct MutableMacro: MemberMacro { |
| 8 | + |
| 9 | + public static func expansion<Declaration, Context>(of node: AttributeSyntax, |
| 10 | + providingMembersOf declaration: Declaration, |
| 11 | + in context: Context) throws -> [DeclSyntax] |
| 12 | + where Declaration: DeclGroupSyntax, |
| 13 | + Context: MacroExpansionContext { |
| 14 | + guard let structDecl = declaration.as(StructDeclSyntax.self) else { |
| 15 | + throw MacroError.onlyApplicableToStruct |
| 16 | + } |
| 17 | + |
| 18 | + let variables = structDecl.memberBlock |
| 19 | + .members |
| 20 | + |
| 21 | + .compactMap { $0.decl.as(VariableDeclSyntax.self) } |
| 22 | + .filter { $0.bindingKeyword.text == "var" } |
| 23 | + |
| 24 | + let functions = try variables.compactMap { variableDecl -> FunctionDeclSyntax? in |
| 25 | + guard let variableBinding = variableDecl.bindings.first, |
| 26 | + let variableType = variableBinding.typeAnnotation?.type else { |
| 27 | + throw MacroError.typeAnnotationRequiredFor(variableName: variableDecl.bindings.first?.pattern.description ?? "unknown") |
| 28 | + } |
| 29 | + |
| 30 | + let variableName = TokenSyntax(stringLiteral: variableBinding.pattern.description) |
| 31 | + |
| 32 | + let parameter = FunctionParameterSyntax(firstName: variableName, type: variableType) |
| 33 | + let parameterList = FunctionParameterListSyntax(arrayLiteral: parameter) |
| 34 | + let modifiers = ModifierListSyntax(arrayLiteral: .init(name: .keyword(.mutating))) |
| 35 | + let bodyItem = CodeBlockItemSyntax.Item.expr(.init(stringLiteral: "self.\(variableName.text)=\(variableName.text)")) |
| 36 | + let body = CodeBlockSyntax(statements: .init(arrayLiteral: .init(item: bodyItem))) |
| 37 | + return FunctionDeclSyntax(modifiers: modifiers, |
| 38 | + identifier: .identifier("set"), |
| 39 | + signature: .init(input: .init(parameterList: parameterList)), |
| 40 | + body: body |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + return functions.compactMap { $0.as(DeclSyntax.self) } |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +@main |
| 50 | +struct MacroPlugin: CompilerPlugin { |
| 51 | + let providingMacros: [Macro.Type] = [ |
| 52 | + MutableMacro.self, |
| 53 | + ] |
| 54 | +} |
0 commit comments