|
| 1 | +import SwiftSyntax |
| 2 | +import SwiftSyntaxMacros |
| 3 | + |
| 4 | +@_implementationOnly import SwiftDiagnostics |
| 5 | +@_implementationOnly import SwiftOperators |
| 6 | +@_implementationOnly import SwiftSyntaxBuilder |
| 7 | + |
| 8 | +private extension DeclSyntaxProtocol { |
| 9 | + var isObservableStoredProperty: Bool { |
| 10 | + guard let property = self.as(VariableDeclSyntax.self), |
| 11 | + let binding = property.bindings.first |
| 12 | + else { |
| 13 | + return false |
| 14 | + } |
| 15 | + |
| 16 | + return binding.accessor == nil |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +public struct ObservableMacro: MemberMacro, MemberAttributeMacro, ConformanceMacro { |
| 21 | + // MARK: - ConformanceMacro |
| 22 | + public static func expansion< |
| 23 | + Declaration: DeclGroupSyntax, |
| 24 | + Context: MacroExpansionContext |
| 25 | + >( |
| 26 | + of node: AttributeSyntax, |
| 27 | + providingConformancesOf declaration: Declaration, |
| 28 | + in context: Context |
| 29 | + ) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] { |
| 30 | + let protocolName: TypeSyntax = "Observable" |
| 31 | + return [(protocolName, nil)] |
| 32 | + } |
| 33 | + |
| 34 | + // MARK: - MemberMacro |
| 35 | + public static func expansion< |
| 36 | + Declaration: DeclGroupSyntax, |
| 37 | + Context: MacroExpansionContext |
| 38 | + >( |
| 39 | + of node: AttributeSyntax, |
| 40 | + providingMembersOf declaration: Declaration, |
| 41 | + in context: Context |
| 42 | + ) throws -> [DeclSyntax] { |
| 43 | + guard let identified = declaration.asProtocol(IdentifiedDeclSyntax.self) else { |
| 44 | + return [] |
| 45 | + } |
| 46 | + |
| 47 | + let parentName = identified.identifier |
| 48 | + |
| 49 | + let registrar: DeclSyntax = |
| 50 | + """ |
| 51 | + let _registrar = ObservationRegistrar<\(parentName)>() |
| 52 | + """ |
| 53 | + |
| 54 | + let transactions: DeclSyntax = |
| 55 | + """ |
| 56 | + public nonisolated func transactions<Delivery>(for keyPaths: KeyPaths<\(parentName)>, isolation: Delivery) -> ObservedTransactions<\(parentName), Delivery> where Delivery: Actor { |
| 57 | + _registrar.transactions(for: keyPaths, isolation: isolation) |
| 58 | + } |
| 59 | + """ |
| 60 | + |
| 61 | + let changes: DeclSyntax = |
| 62 | + """ |
| 63 | + public nonisolated func changes<Member>(for keyPath: KeyPath<\(parentName), Member>) -> ObservedChanges<\(parentName), Member> where Member: Sendable { |
| 64 | + _registrar.changes(for: keyPath) |
| 65 | + } |
| 66 | + """ |
| 67 | + |
| 68 | + let memberList = MemberDeclListSyntax( |
| 69 | + declaration.members.members.filter { |
| 70 | + $0.decl.isObservableStoredProperty |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + let storageStruct: DeclSyntax = |
| 75 | + """ |
| 76 | + private struct _Storage { |
| 77 | + \(memberList) |
| 78 | + } |
| 79 | + """ |
| 80 | + |
| 81 | + let storage: DeclSyntax = |
| 82 | + """ |
| 83 | + private var _storage = _Storage() |
| 84 | + """ |
| 85 | + |
| 86 | + return [ |
| 87 | + registrar, |
| 88 | + transactions, |
| 89 | + changes, |
| 90 | + storageStruct, |
| 91 | + storage, |
| 92 | + ] |
| 93 | + } |
| 94 | + |
| 95 | + // MARK: - MemberAttributeMacro |
| 96 | + |
| 97 | + public static func expansion< |
| 98 | + Declaration: DeclGroupSyntax, |
| 99 | + MemberDeclaration: DeclSyntaxProtocol, |
| 100 | + Context: MacroExpansionContext |
| 101 | + >( |
| 102 | + of node: AttributeSyntax, |
| 103 | + attachedTo declaration: Declaration, |
| 104 | + providingAttributesFor member: MemberDeclaration, |
| 105 | + in context: Context |
| 106 | + ) throws -> [AttributeSyntax] { |
| 107 | + guard member.isObservableStoredProperty else { |
| 108 | + return [] |
| 109 | + } |
| 110 | + |
| 111 | + return [ |
| 112 | + AttributeSyntax( |
| 113 | + attributeName: SimpleTypeIdentifierSyntax( |
| 114 | + name: .identifier("ObservableProperty") |
| 115 | + ) |
| 116 | + ) |
| 117 | + ] |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +public struct ObservablePropertyMacro: AccessorMacro { |
| 122 | + public static func expansion< |
| 123 | + Context: MacroExpansionContext, |
| 124 | + Declaration: DeclSyntaxProtocol |
| 125 | + >( |
| 126 | + of node: AttributeSyntax, |
| 127 | + providingAccessorsOf declaration: Declaration, |
| 128 | + in context: Context |
| 129 | + ) throws -> [AccessorDeclSyntax] { |
| 130 | + guard let property = declaration.as(VariableDeclSyntax.self), |
| 131 | + let binding = property.bindings.first, |
| 132 | + let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier, |
| 133 | + binding.accessor == nil |
| 134 | + else { |
| 135 | + return [] |
| 136 | + } |
| 137 | + |
| 138 | + if identifier.text == "_registrar" || identifier.text == "_storage" { return [] } |
| 139 | + |
| 140 | + let getAccessor: AccessorDeclSyntax = |
| 141 | + """ |
| 142 | + get { |
| 143 | + _registrar.access(self, keyPath: \\.\(identifier)) |
| 144 | + return _storage.\(identifier) |
| 145 | + } |
| 146 | + """ |
| 147 | + |
| 148 | + let setAccessor: AccessorDeclSyntax = |
| 149 | + """ |
| 150 | + set { |
| 151 | + _registrar.withMutation(of: self, keyPath: \\.\(identifier)) { |
| 152 | + _storage.\(identifier) = newValue |
| 153 | + } |
| 154 | + } |
| 155 | + """ |
| 156 | + |
| 157 | + return [getAccessor, setAccessor] |
| 158 | + } |
| 159 | +} |
0 commit comments