Skip to content
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
4 changes: 4 additions & 0 deletions Sources/EquatableMacros/Closure.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import SwiftSyntax

func isClosure(type: TypeSyntax) -> Bool {
if let attributedType = type.as(AttributedTypeSyntax.self) {
return isClosure(type: attributedType.baseType)
}

if type.is(FunctionTypeSyntax.self) {
return true
}
Expand Down
164 changes: 164 additions & 0 deletions Tests/EquatableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,42 @@ struct EquatableMacroTests {
)
}

@Test
func equatableIgnoredCannotBeAppliedToMainActorClosures() async throws {
assertMacroExpansion(
"""
struct CustomView: View {
@EquatableIgnored var closure: @MainActor () -> Void
var name: String

var body: some View {
Text("CustomView")
}
}
""",
expandedSource:
"""
struct CustomView: View {
var closure: @MainActor () -> Void
var name: String

var body: some View {
Text("CustomView")
}
}
""",
diagnostics: [
DiagnosticSpec(
message: "@EquatableIgnored cannot be applied to closures",
line: 2,
column: 5
)
],
macroSpecs: macroSpecs,
failureHandler: failureHander
)
}

@Test
func equatableIgnoredCannotBeAppliedToBindings() async throws {
assertMacroExpansion(
Expand Down Expand Up @@ -658,6 +694,75 @@ struct EquatableMacroTests {
)
}

@Test(arguments: testArguments)
func arbitaryMainActorClosuresNotAllowed(isolation: Isolation) async throws {
let macro = EquatableMacroTests.equatableMacro(for: isolation)
let generatedConformance = switch isolation {
case .nonisolated:
"""
extension CustomView: Equatable {
nonisolated public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.name == rhs.name
}
}
"""
case .isolated:
"""
extension CustomView: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.name == rhs.name
}
}
"""
case .main:
"""
extension CustomView: @MainActor Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.name == rhs.name
}
}
"""
}
assertMacroExpansion(
"""
\(macro)
struct CustomView: View {
var name: String
let closure: (@MainActor () -> Void)?

var body: some View {
Text("CustomView")
}
}
""",
expandedSource:
"""
struct CustomView: View {
var name: String
let closure: (@MainActor () -> Void)?

var body: some View {
Text("CustomView")
}
}

\(generatedConformance)
""",
diagnostics: [
DiagnosticSpec(
message: "Arbitary closures are not supported in @Equatable",
line: 4,
column: 5,
fixIts: [
FixItSpec(message: "Consider marking the closure with@EquatableIgnoredUnsafeClosure if it doesn't effect the view's body output.")
]
)
],
macroSpecs: macroSpecs,
failureHandler: failureHander
)
}

@Test(arguments: testArguments)
func closuresMarkedWithEquatableIgnoredUnsafeClosure(isolation: Isolation) async throws {
let macro = EquatableMacroTests.equatableMacro(for: isolation)
Expand Down Expand Up @@ -717,6 +822,65 @@ struct EquatableMacroTests {
)
}

@Test(arguments: testArguments)
func mainActorClosuresMarkedWithEquatableIgnoredUnsafeClosure(isolation: Isolation) async throws {
let macro = EquatableMacroTests.equatableMacro(for: isolation)
let generatedConformance = switch isolation {
case .nonisolated:
"""
extension CustomView: Equatable {
nonisolated public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.name == rhs.name
}
}
"""
case .isolated:
"""
extension CustomView: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.name == rhs.name
}
}
"""
case .main:
"""
extension CustomView: @MainActor Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.name == rhs.name
}
}
"""
}
assertMacroExpansion(
"""
\(macro)
struct CustomView: View {
@EquatableIgnoredUnsafeClosure let closure: (@MainActor () -> Void)?
var name: String

var body: some View {
Text("CustomView")
}
}
""",
expandedSource:
"""
struct CustomView: View {
let closure: (@MainActor () -> Void)?
var name: String

var body: some View {
Text("CustomView")
}
}

\(generatedConformance)
""",
macroSpecs: macroSpecs,
failureHandler: failureHander
)
}

@Test(arguments: testArguments)
func noEquatableProperties(isolation: Isolation) async throws {
let macro = EquatableMacroTests.equatableMacro(for: isolation)
Expand Down
Loading