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

More fixes for @Reducer macro. #2834

Merged
merged 2 commits into from
Feb 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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