File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -25,9 +25,12 @@ public enum EquatableExtensionMacro: ExtensionMacro {
25
25
throw EquatableExtensionError . onlyApplicableToFinalClassOrActor
26
26
}
27
27
28
+ let isPublic = declaration. modifiers. contains ( where: { $0. name. tokenKind == . keyword( . public) } )
29
+ let addModifiers = isPublic ? " public " : " "
30
+
28
31
return try [
29
32
ExtensionDeclSyntax ( " extension \( type. trimmed) : Equatable " ) {
30
- try FunctionDeclSyntax ( " static func == (lhs: \( type. trimmed) , rhs: \( type. trimmed) ) -> Bool " ) {
33
+ try FunctionDeclSyntax ( " \( raw : addModifiers ) static func == (lhs: \( type. trimmed) , rhs: \( type. trimmed) ) -> Bool " ) {
31
34
let properties = declaration. memberBlock. members
32
35
. compactMap { $0. decl. as ( VariableDeclSyntax . self) }
33
36
. compactMap { $0. bindings. first? . as ( PatternBindingSyntax . self) }
Original file line number Diff line number Diff line change @@ -57,6 +57,49 @@ final class EquatableTests: XCTestCase {
57
57
#endif
58
58
}
59
59
60
+ func testEquatableMacroOnPublicFinalClasses( ) throws {
61
+ #if canImport(EquatableMacros)
62
+
63
+ assertMacroExpansion (
64
+ """
65
+ @Equatable
66
+ public final class Planet {
67
+ let name: String
68
+ let mass: Mass
69
+
70
+ var this: String { " 1 " }
71
+
72
+ init(name: String) {
73
+ self.name = name
74
+ }
75
+ }
76
+ """ ,
77
+ expandedSource: """
78
+ public final class Planet {
79
+ let name: String
80
+ let mass: Mass
81
+
82
+ var this: String { " 1 " }
83
+
84
+ init(name: String) {
85
+ self.name = name
86
+ }
87
+ }
88
+
89
+ extension Planet: Equatable {
90
+ public static func == (lhs: Planet, rhs: Planet) -> Bool {
91
+ lhs.name == rhs.name &&
92
+ lhs.mass == rhs.mass
93
+ }
94
+ }
95
+ """ ,
96
+ macros: testMacros
97
+ )
98
+ #else
99
+ throw XCTSkip ( " macros are only supported when running tests for the host platform " )
100
+ #endif
101
+ }
102
+
60
103
func testEquatableMacroOnActors( ) throws {
61
104
#if canImport(EquatableMacros)
62
105
You can’t perform that action at this time.
0 commit comments