Skip to content

Commit 856f9b8

Browse files
authored
More fixes for @Reducer macro. (#2834)
* More fixes for @Reducer macro. * wip
1 parent ad7223a commit 856f9b8

File tree

3 files changed

+161
-32
lines changed

3 files changed

+161
-32
lines changed

Sources/ComposableArchitectureMacros/ReducerMacro.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,19 @@ extension ReducerMacro: MemberMacro {
391391
"""
392392
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
393393
\(access)static var body: \(raw: staticVarBody) {
394-
\(raw: reducerScopes.joined(separator: "\n"))
395-
}
396394
"""
397395
)
396+
if reducerScopes.isEmpty {
397+
decls.append("""
398+
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
399+
""")
400+
} else {
401+
decls.append("""
402+
\(raw: reducerScopes.joined(separator: "\n"))
403+
404+
""")
405+
}
406+
decls.append("}")
398407
}
399408
if !typeNames.contains("CaseScope") {
400409
decls.append(

Tests/ComposableArchitectureMacrosTests/ReducerMacroTests.swift

Lines changed: 144 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@
236236
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
237237
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
238238
239+
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
240+
239241
}
240242
241243
enum CaseScope {
@@ -303,15 +305,17 @@
303305
304306
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
305307
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>> {
306-
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
307-
Activity()
308-
}
309-
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
310-
Timeline()
311-
}
312-
ComposableArchitecture.Scope(state: \Self.State.Cases.tweet, action: \Self.Action.Cases.tweet) {
313-
Tweet()
314-
}
308+
309+
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
310+
Activity()
311+
}
312+
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
313+
Timeline()
314+
}
315+
ComposableArchitecture.Scope(state: \Self.State.Cases.tweet, action: \Self.Action.Cases.tweet) {
316+
Tweet()
317+
}
318+
315319
}
316320
317321
enum CaseScope {
@@ -341,6 +345,106 @@
341345
}
342346
}
343347

348+
func testEnum_Empty() {
349+
assertMacro {
350+
"""
351+
@Reducer
352+
enum Destination {
353+
}
354+
"""
355+
} expansion: {
356+
"""
357+
enum Destination {
358+
359+
@CasePathable
360+
@dynamicMemberLookup
361+
@ObservableState
362+
enum State: ComposableArchitecture.CaseReducerState {
363+
typealias StateReducer = Destination
364+
365+
}
366+
367+
@CasePathable
368+
enum Action {
369+
370+
}
371+
372+
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
373+
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
374+
375+
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
376+
377+
}
378+
379+
enum CaseScope {
380+
381+
}
382+
383+
static func scope(_ store: ComposableArchitecture.Store<Self.State, Self.Action>) -> CaseScope {
384+
switch store.state {
385+
386+
}
387+
}
388+
}
389+
390+
extension Destination: ComposableArchitecture.CaseReducer, ComposableArchitecture.Reducer {
391+
}
392+
"""
393+
}
394+
}
395+
396+
func testEnum_OneAlertCase() {
397+
assertMacro {
398+
"""
399+
@Reducer
400+
enum Destination {
401+
case alert(AlertState<Never>)
402+
}
403+
"""
404+
} expansion: {
405+
"""
406+
enum Destination {
407+
@ReducerCaseEphemeral
408+
case alert(AlertState<Never>)
409+
410+
@CasePathable
411+
@dynamicMemberLookup
412+
@ObservableState
413+
enum State: ComposableArchitecture.CaseReducerState {
414+
typealias StateReducer = Destination
415+
case alert(AlertState<Never>)
416+
}
417+
418+
@CasePathable
419+
enum Action {
420+
case alert(AlertState<Never>.Action)
421+
}
422+
423+
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
424+
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
425+
426+
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
427+
428+
}
429+
430+
enum CaseScope {
431+
case alert(AlertState<Never>)
432+
}
433+
434+
static func scope(_ store: ComposableArchitecture.Store<Self.State, Self.Action>) -> CaseScope {
435+
switch store.state {
436+
case let .alert(v0):
437+
return .alert(v0)
438+
}
439+
}
440+
}
441+
442+
extension Destination: ComposableArchitecture.CaseReducer, ComposableArchitecture.Reducer {
443+
}
444+
"""
445+
}
446+
}
447+
344448
func testEnum_TwoCases() {
345449
assertMacro {
346450
"""
@@ -373,12 +477,14 @@
373477
374478
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
375479
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Activity>, ComposableArchitecture.Scope<Self.State, Self.Action, Timeline>> {
376-
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
377-
Activity()
378-
}
379-
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
380-
Timeline()
381-
}
480+
481+
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
482+
Activity()
483+
}
484+
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
485+
Timeline()
486+
}
487+
382488
}
383489
384490
enum CaseScope {
@@ -435,9 +541,11 @@
435541
436542
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
437543
static var body: ComposableArchitecture.Scope<Self.State, Self.Action, Timeline> {
438-
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
439-
Timeline()
440-
}
544+
545+
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
546+
Timeline()
547+
}
548+
441549
}
442550
443551
enum CaseScope {
@@ -500,6 +608,8 @@
500608
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
501609
static var body: ComposableArchitecture.EmptyReducer<Self.State, Self.Action> {
502610
611+
ComposableArchitecture.EmptyReducer<Self.State, Self.Action>()
612+
503613
}
504614
505615
enum CaseScope {
@@ -562,15 +672,17 @@
562672
563673
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
564674
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>> {
565-
ComposableArchitecture.Scope(state: \Self.State.Cases.drillDown, action: \Self.Action.Cases.drillDown) {
566-
Counter()
567-
}
568-
ComposableArchitecture.Scope(state: \Self.State.Cases.popover, action: \Self.Action.Cases.popover) {
569-
Counter()
570-
}
571-
ComposableArchitecture.Scope(state: \Self.State.Cases.sheet, action: \Self.Action.Cases.sheet) {
572-
Counter()
573-
}
675+
676+
ComposableArchitecture.Scope(state: \Self.State.Cases.drillDown, action: \Self.Action.Cases.drillDown) {
677+
Counter()
678+
}
679+
ComposableArchitecture.Scope(state: \Self.State.Cases.popover, action: \Self.Action.Cases.popover) {
680+
Counter()
681+
}
682+
ComposableArchitecture.Scope(state: \Self.State.Cases.sheet, action: \Self.Action.Cases.sheet) {
683+
Counter()
684+
}
685+
574686
}
575687
576688
enum CaseScope {
@@ -625,9 +737,11 @@
625737
626738
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
627739
static var body: ComposableArchitecture.Scope<Self.State, Self.Action, Nested.Feature> {
628-
ComposableArchitecture.Scope(state: \Self.State.Cases.feature, action: \Self.Action.Cases.feature) {
629-
Nested.Feature()
630-
}
740+
741+
ComposableArchitecture.Scope(state: \Self.State.Cases.feature, action: \Self.Action.Cases.feature) {
742+
Nested.Feature()
743+
}
744+
631745
}
632746
633747
enum CaseScope {

Tests/ComposableArchitectureTests/MacroTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979
case feature3(Feature)
8080
case feature4(Feature)
8181
}
82+
@Reducer
83+
public enum Destination5 {
84+
case alert(AlertState<Never>)
85+
}
86+
@Reducer
87+
public enum Destination6 {}
8288
}
8389

8490
enum TestEnumReducer_SynthesizedConformances {

0 commit comments

Comments
 (0)