Skip to content

Commit

Permalink
Patch
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Aug 31, 2023
1 parent 9093eec commit 7270607
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
40 changes: 20 additions & 20 deletions Sources/Verge/macros.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
@freestanding(expression)
public macro ifChanged<State, U0>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, onChanged: (U0) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, onChanged: (U0) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, onChanged: (U0, U1) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, onChanged: (U0, U1) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, onChanged: (U0, U1, U2) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, onChanged: (U0, U1, U2) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2, U3>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, onChanged: (U0, U1, U2, U3) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2, U3>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, onChanged: (U0, U1, U2, U3) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2, U3, U4>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, onChanged: (U0, U1, U2, U3, U4) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2, U3, U4>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, onChanged: (U0, U1, U2, U3, U4) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2, U3, U4, U5>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, onChanged: (U0, U1, U2, U3, U4, U5) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2, U3, U4, U5>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, onChanged: (U0, U1, U2, U3, U4, U5) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2, U3, U4, U5, U6>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, onChanged: (U0, U1, U2, U3, U4, U5, U6) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2, U3, U4, U5, U6>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, onChanged: (U0, U1, U2, U3, U4, U5, U6) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2, U3, U4, U5, U6, U7>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, _ keyPath_7: KeyPath<State, U7>, onChanged: (U0, U1, U2, U3, U4, U5, U6, U7) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2, U3, U4, U5, U6, U7>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, _ keyPath_7: KeyPath<State, U7>, onChanged: (U0, U1, U2, U3, U4, U5, U6, U7) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2, U3, U4, U5, U6, U7, U8>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, _ keyPath_7: KeyPath<State, U7>, _ keyPath_8: KeyPath<State, U8>, onChanged: (U0, U1, U2, U3, U4, U5, U6, U7, U8) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2, U3, U4, U5, U6, U7, U8>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, _ keyPath_7: KeyPath<State, U7>, _ keyPath_8: KeyPath<State, U8>, onChanged: (U0, U1, U2, U3, U4, U5, U6, U7, U8) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")

@freestanding(expression)
public macro ifChanged<State, U0, U1, U2, U3, U4, U5, U6, U7, U8, U9>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, _ keyPath_7: KeyPath<State, U7>, _ keyPath_8: KeyPath<State, U8>, _ keyPath_9: KeyPath<State, U9>, onChanged: (U0, U1, U2, U3, U4, U5, U6, U7, U8, U9) -> Void) -> Void = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
@freestanding(declaration)
public macro IfChanged<State, U0, U1, U2, U3, U4, U5, U6, U7, U8, U9>(_ state: Changes<State>, _ keyPath_0: KeyPath<State, U0>, _ keyPath_1: KeyPath<State, U1>, _ keyPath_2: KeyPath<State, U2>, _ keyPath_3: KeyPath<State, U3>, _ keyPath_4: KeyPath<State, U4>, _ keyPath_5: KeyPath<State, U5>, _ keyPath_6: KeyPath<State, U6>, _ keyPath_7: KeyPath<State, U7>, _ keyPath_8: KeyPath<State, U8>, _ keyPath_9: KeyPath<State, U9>, onChanged: (U0, U1, U2, U3, U4, U5, U6, U7, U8, U9) -> Void) = #externalMacro(module: "VergeMacrosPlugin", type: "IfChangedMacro")
36 changes: 19 additions & 17 deletions Sources/VergeMacrosPlugin/IfChangedMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct IfChangedMacro: ExpressionMacro {
public struct IfChangedMacro: Macro {

public enum Error: Swift.Error {
case foundNotKeyPathLiteral
case faildToExpand

}

}

extension IfChangedMacro: DeclarationMacro {

public static func expansion(
of node: some SwiftSyntax.FreestandingMacroExpansionSyntax,
in context: some SwiftSyntaxMacros.MacroExpansionContext
) throws -> SwiftSyntax.ExprSyntax {
) throws -> [SwiftSyntax.DeclSyntax] {

let onChangedClosure: ClosureExprSyntax

Expand Down Expand Up @@ -42,7 +45,7 @@ public struct IfChangedMacro: ExpressionMacro {
).components

let name =
components
components
.map {
$0.cast(KeyPathComponentSyntax.self).component.cast(
KeyPathPropertyComponentSyntax.self
Expand All @@ -56,21 +59,20 @@ public struct IfChangedMacro: ExpressionMacro {

let conditions = names.map { arg in

return
(
condition: """
return (
condition: """
primitiveState\(arg.1) != previousState?\(arg.1)
""",
property: """
property: """
let \(arg.0) = primitiveState\(arg.1)
""",
accessor: "primitiveState\(arg.1)"
)
accessor: "primitiveState\(arg.1)"
)
}

return """
{ () -> Void in
return [
("""
do {
let primitiveState = \(stateExpr).primitive
let previousState = \(stateExpr).previous?.primitive
Expand All @@ -79,10 +81,10 @@ public struct IfChangedMacro: ExpressionMacro {
return
}
let _: Void = \(onChangedClosure)(\(raw: conditions.map { $0.accessor }.joined(separator: ",")))
}()
"""

let _: Void = \(onChangedClosure)(\(raw: conditions.map { $0.accessor }.joined(separator: ", ")))
}
""" as DeclSyntax)
]
}

}
7 changes: 4 additions & 3 deletions Tests/VergeTests/MacroTests.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import Verge
import XCTest
import SwiftUI

final class MacroTests: XCTestCase {

func testChanges() {

let state: Changes<DemoState> = .init(old: nil, new: .init(name: "hello"))

#ifChanged(state, \.name) { name in
#IfChanged(state, \.name) { name in
print(name)
}

#ifChanged(state, \.name, \.count) { name, count in
#IfChanged(state, \.name, \.count) { name, count in
print(name, count)
}

#ifChanged(state, \.name, \.count, onChanged: { name, count in
#IfChanged(state, \.name, \.count, onChanged: { name, count in
print(name, count)
})

Expand Down

0 comments on commit 7270607

Please sign in to comment.