Skip to content

Commit

Permalink
Public modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ay42 committed May 29, 2024
1 parent 06a9e89 commit 1fe16a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Sources/EquatableMacros/EquatableExtensionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ public enum EquatableExtensionMacro: ExtensionMacro {
throw EquatableExtensionError.onlyApplicableToFinalClassOrActor
}

let isPublic = declaration.modifiers.contains(where: { $0.name.tokenKind == .keyword(.public) })
let addModifiers = isPublic ? "public " : ""

return try [
ExtensionDeclSyntax("extension \(type.trimmed): Equatable") {
try FunctionDeclSyntax("static func == (lhs: \(type.trimmed), rhs: \(type.trimmed)) -> Bool") {
try FunctionDeclSyntax("\(raw: addModifiers)static func == (lhs: \(type.trimmed), rhs: \(type.trimmed)) -> Bool") {
let properties = declaration.memberBlock.members
.compactMap { $0.decl.as(VariableDeclSyntax.self) }
.compactMap { $0.bindings.first?.as(PatternBindingSyntax.self) }
Expand Down
43 changes: 43 additions & 0 deletions Tests/EquatableTests/EquatableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,49 @@ final class EquatableTests: XCTestCase {
#endif
}

func testEquatableMacroOnPublicFinalClasses() throws {
#if canImport(EquatableMacros)

assertMacroExpansion(
"""
@Equatable
public final class Planet {
let name: String
let mass: Mass
var this: String { "1" }
init(name: String) {
self.name = name
}
}
""",
expandedSource: """
public final class Planet {
let name: String
let mass: Mass
var this: String { "1" }
init(name: String) {
self.name = name
}
}
extension Planet: Equatable {
public static func == (lhs: Planet, rhs: Planet) -> Bool {
lhs.name == rhs.name &&
lhs.mass == rhs.mass
}
}
""",
macros: testMacros
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testEquatableMacroOnActors() throws {
#if canImport(EquatableMacros)

Expand Down

0 comments on commit 1fe16a2

Please sign in to comment.