Skip to content

Commit 146218a

Browse files
authored
Enum reducers: generate direct action cases for ephemeral state (#3240)
Currently, the following reducer enum: ```swift @Reducer enum Destination { case alert(AlertState<Never>) } ``` Generates the following action: ```swift @CasePathable enum Action { case alert(AlertState<Never>.Action) } ``` And this produces a warning in Case Paths 1.5 due to the nested `Never` not being referenced directly. This PR plucks the action type out and embeds it directly, instead: ```diff -case alert(AlertState<Never>.Action) +case alert(Never) ``` Which will allow us to better suppress warnings Swift emits for uninhabited types.
1 parent a792049 commit 146218a

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

ComposableArchitecture.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/ComposableArchitectureMacros/ReducerMacro.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,17 @@ private enum ReducerCase {
498498
let parameter = parameterClause.parameters.first,
499499
parameter.type.is(IdentifierTypeSyntax.self) || parameter.type.is(MemberTypeSyntax.self)
500500
{
501-
return "case \(element.suffixed("Action").type.trimmedDescription)"
501+
if
502+
let type = parameter.type.as(IdentifierTypeSyntax.self),
503+
type.isEphemeral,
504+
let generics = type.genericArgumentClause?.arguments,
505+
generics.count == 1,
506+
let generic = generics.first?.argument.trimmedDescription
507+
{
508+
return "case \(element.name)(\(generic))"
509+
} else {
510+
return "case \(element.suffixed("Action").type.trimmedDescription)"
511+
}
502512
} else {
503513
return "case \(element.name)(Swift.Never)"
504514
}

Tests/ComposableArchitectureMacrosTests/ReducerMacroTests.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@
246246
switch store.state {
247247
248248
}
249-
}}
249+
}
250+
}
250251
251252
extension Destination: ComposableArchitecture.CaseReducer, ComposableArchitecture.Reducer {
252253
}
@@ -298,11 +299,11 @@
298299
case activity(Activity.Action)
299300
case timeline(Timeline.Action)
300301
case tweet(Tweet.Action)
301-
case alert(AlertState<Alert> .Action)
302+
case alert(Alert)
302303
}
303304
304305
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
305-
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+
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>> {
306307
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
307308
Activity()
308309
}
@@ -372,7 +373,7 @@
372373
}
373374
374375
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
375-
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action> ._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Timeline>, ComposableArchitecture.Scope<Self.State, Self.Action, Meeting>> {
376+
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Timeline>, ComposableArchitecture.Scope<Self.State, Self.Action, Meeting>> {
376377
ComposableArchitecture.Scope(state: \Self.State.Cases.timeline, action: \Self.Action.Cases.timeline) {
377378
Timeline()
378379
}
@@ -572,7 +573,7 @@
572573
573574
@CasePathable
574575
enum Action {
575-
case alert(AlertState<Never> .Action)
576+
case alert(Never)
576577
}
577578
578579
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
@@ -629,7 +630,7 @@
629630
}
630631
631632
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
632-
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action> ._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Activity>, ComposableArchitecture.Scope<Self.State, Self.Action, Timeline>> {
633+
static var body: ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>._Sequence<ComposableArchitecture.Scope<Self.State, Self.Action, Activity>, ComposableArchitecture.Scope<Self.State, Self.Action, Timeline>> {
633634
ComposableArchitecture.Scope(state: \Self.State.Cases.activity, action: \Self.Action.Cases.activity) {
634635
Activity()
635636
}
@@ -751,8 +752,8 @@
751752
752753
@CasePathable
753754
enum Action {
754-
case alert(AlertState<Alert> .Action)
755-
case dialog(ConfirmationDialogState<Dialog> .Action)
755+
case alert(Alert)
756+
case dialog(Dialog)
756757
case meeting(Swift.Never)
757758
}
758759
@@ -820,7 +821,7 @@
820821
}
821822
822823
@ComposableArchitecture.ReducerBuilder<Self.State, Self.Action>
823-
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>> {
824+
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>> {
824825
ComposableArchitecture.Scope(state: \Self.State.Cases.drillDown, action: \Self.Action.Cases.drillDown) {
825826
Counter()
826827
}
@@ -1098,7 +1099,8 @@
10981099
enum Action: Equatable, Hashable, Sendable {
10991100
}
11001101
1101-
let body = ComposableArchitecture.EmptyReducer<State, Action>()}
1102+
let body = ComposableArchitecture.EmptyReducer<State, Action>()
1103+
}
11021104
11031105
@available(iOS, unavailable) extension Feature: ComposableArchitecture.Reducer {
11041106
}
@@ -1183,7 +1185,7 @@
11831185
case child(ChildFeature.Action)
11841186
#if os(macOS)
11851187
case mac(MacFeature.Action)
1186-
case macAlert(AlertState<MacAlert> .Action)
1188+
case macAlert(MacAlert)
11871189
#elseif os(iOS)
11881190
case phone(PhoneFeature.Action)
11891191
#else
@@ -1194,7 +1196,7 @@
11941196
#if DEBUG
11951197
#if INNER
11961198
case inner(InnerFeature.Action)
1197-
case innerDialog(ConfirmationDialogState<InnerDialog> .Action)
1199+
case innerDialog(InnerDialog)
11981200
#endif
11991201
#endif
12001202

0 commit comments

Comments
 (0)