Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't generate Equatable checking for computed properties #131

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/VexilMacros/FlagContainerMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ extension FlagContainerMacro: ExtensionMacro {
extendedType: type,
inheritanceClause: .init(inheritedTypes: [ .init(type: TypeSyntax(stringLiteral: "Equatable")) ])
) {
var variables = declaration.memberBlock.variables
var variables = declaration.memberBlock.storedVariables
if variables.isEmpty == false {
try FunctionDeclSyntax("func ==(lhs: \(type), rhs: \(type)) -> Bool") {
if let lastBinding = variables.removeLast().bindings.first?.pattern {
Expand Down
23 changes: 23 additions & 0 deletions Sources/VexilMacros/Utilities/SimpleVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ extension MemberBlockSyntax {
}
}

var storedVariables: [VariableDeclSyntax] {
variables.filter { variable in
// Only simple properties
guard variable.bindings.count == 1, let binding = variable.bindings.first else {
return false
}

// If it has no accessor block it's stored
guard let accessorBlock = binding.accessorBlock else {
return true
}

// If there is any kind of getter then its computed
switch accessorBlock.accessors {
case .getter:
return false

case let .accessors(accessors):
return accessors.allSatisfy { $0.accessorSpecifier.tokenKind != .keyword(.get) }
}
}
}

}

extension VariableDeclSyntax {
Expand Down
29 changes: 28 additions & 1 deletion Tests/VexilMacroTests/EquatableFlagContainerMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ final class EquatableFlagContainerMacroTests: XCTestCase {
struct TestFlags {
@Flag(default: false, description: "Some Flag")
var someFlag: Bool

var someComputedNotEquatableProperty: (any Error)? {
nil
}

var someComputedPropertyWithAGetter: (any Error)? {
get { fatalError() }
set { fatalError() }
}

var otherStoredProperty: Int {
didSet { fatalError() }
}
}
""",
expandedSource: """
Expand All @@ -86,6 +99,19 @@ final class EquatableFlagContainerMacroTests: XCTestCase {
)
}

var someComputedNotEquatableProperty: (any Error)? {
nil
}

var someComputedPropertyWithAGetter: (any Error)? {
get { fatalError() }
set { fatalError() }
}

var otherStoredProperty: Int {
didSet { fatalError() }
}

fileprivate let _flagKeyPath: FlagKeyPath

fileprivate let _flagLookup: any FlagLookup
Expand Down Expand Up @@ -120,7 +146,8 @@ final class EquatableFlagContainerMacroTests: XCTestCase {

extension TestFlags: Equatable {
static func ==(lhs: TestFlags, rhs: TestFlags) -> Bool {
lhs.someFlag == rhs.someFlag
lhs.someFlag == rhs.someFlag &&
lhs.otherStoredProperty == rhs.otherStoredProperty
}
}
""",
Expand Down
Loading