Skip to content

Commit

Permalink
More fixes for @Reducer macro. (#2834)
Browse files Browse the repository at this point in the history
* More fixes for @Reducer macro.

* wip
  • Loading branch information
mbrandonw committed Feb 19, 2024
1 parent ad7223a commit 856f9b8
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 32 deletions.
13 changes: 11 additions & 2 deletions Sources/ComposableArchitectureMacros/ReducerMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,19 @@ extension ReducerMacro: MemberMacro {
"""
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
\(access)static var body: \(raw: staticVarBody) {
\(raw: reducerScopes.joined(separator: "\n"))
}
"""
)
if reducerScopes.isEmpty {
decls.append("""
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
""")
} else {
decls.append("""
\(raw: reducerScopes.joined(separator: "\n"))
""")
}
decls.append("}")
}
if !typeNames.contains("CaseScope") {
decls.append(
Expand Down
174 changes: 144 additions & 30 deletions Tests/ComposableArchitectureMacrosTests/ReducerMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
}
enum CaseScope {
Expand Down Expand Up @@ -303,15 +305,17 @@
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Activity>, ComposableArchitecture.Scope<Self.State, Self.Action, Timeline>>, ComposableArchitecture.Scope<Self.State, Self.Action, Tweet>> {
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
Activity()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
Timeline()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.tweet, action: \Self.Action.Cases.tweet) {
Tweet()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
Activity()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
Timeline()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.tweet, action: \Self.Action.Cases.tweet) {
Tweet()
}
}
enum CaseScope {
Expand Down Expand Up @@ -341,6 +345,106 @@
}
}

func testEnum_Empty() {
assertMacro {
"""
@Reducer
enum Destination {
}
"""
} expansion: {
"""
enum Destination {
@CasePathable
@dynamicMemberLookup
@ObservableState
enum State: ComposableArchitecture.CaseReducerState {
typealias StateReducer = Destination
}
@CasePathable
enum Action {
}
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
}
enum CaseScope {
}
static func scope(_ store: ComposableArchitecture.Store<Self.State, Self.Action>) -> CaseScope {
switch store.state {
}
}
}
extension Destination: ComposableArchitecture.CaseReducer, ComposableArchitecture.Reducer {
}
"""
}
}

func testEnum_OneAlertCase() {
assertMacro {
"""
@Reducer
enum Destination {
case alert(AlertState<Never>)
}
"""
} expansion: {
"""
enum Destination {
@ReducerCaseEphemeral
case alert(AlertState<Never>)
@CasePathable
@dynamicMemberLookup
@ObservableState
enum State: ComposableArchitecture.CaseReducerState {
typealias StateReducer = Destination
case alert(AlertState<Never>)
}
@CasePathable
enum Action {
case alert(AlertState<Never>.Action)
}
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
}
enum CaseScope {
case alert(AlertState<Never>)
}
static func scope(_ store: ComposableArchitecture.Store<Self.State, Self.Action>) -> CaseScope {
switch store.state {
case let .alert(v0):
return .alert(v0)
}
}
}
extension Destination: ComposableArchitecture.CaseReducer, ComposableArchitecture.Reducer {
}
"""
}
}

func testEnum_TwoCases() {
assertMacro {
"""
Expand Down Expand Up @@ -373,12 +477,14 @@
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Activity>, ComposableArchitecture.Scope<Self.State, Self.Action, Timeline>> {
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
Activity()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
Timeline()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
Activity()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
Timeline()
}
}
enum CaseScope {
Expand Down Expand Up @@ -435,9 +541,11 @@
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.Scope<Self.State, Self.Action, Timeline> {
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
Timeline()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
Timeline()
}
}
enum CaseScope {
Expand Down Expand Up @@ -500,6 +608,8 @@
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
}
enum CaseScope {
Expand Down Expand Up @@ -562,15 +672,17 @@
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Counter>, ComposableArchitecture.Scope<Self.State, Self.Action, Counter>>, ComposableArchitecture.Scope<Self.State, Self.Action, Counter>> {
ComposableArchitecture.Scope(state: \Self.State.Cases.drillDown, action: \Self.Action.Cases.drillDown) {
Counter()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.popover, action: \Self.Action.Cases.popover) {
Counter()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.sheet, action: \Self.Action.Cases.sheet) {
Counter()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.drillDown, action: \Self.Action.Cases.drillDown) {
Counter()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.popover, action: \Self.Action.Cases.popover) {
Counter()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.sheet, action: \Self.Action.Cases.sheet) {
Counter()
}
}
enum CaseScope {
Expand Down Expand Up @@ -625,9 +737,11 @@
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
static var body: ComposableArchitecture.Scope<Self.State, Self.Action, Nested.Feature> {
ComposableArchitecture.Scope(state: \Self.State.Cases.feature, action: \Self.Action.Cases.feature) {
Nested.Feature()
}
ComposableArchitecture.Scope(state: \Self.State.Cases.feature, action: \Self.Action.Cases.feature) {
Nested.Feature()
}
}
enum CaseScope {
Expand Down
6 changes: 6 additions & 0 deletions Tests/ComposableArchitectureTests/MacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
case feature3(Feature)
case feature4(Feature)
}
@Reducer
public enum Destination5 {
case alert(AlertState<Never>)
}
@Reducer
public enum Destination6 {}
}

enum TestEnumReducer_SynthesizedConformances {
Expand Down

0 comments on commit 856f9b8

Please sign in to comment.