Skip to content

Commit

Permalink
Run swift-format
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis authored and github-actions[bot] committed Feb 12, 2024
1 parent f75f938 commit cf967a2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import SwiftUI
import SwiftUINavigation

fileprivate enum PresentationTestCase {
private enum PresentationTestCase {
@Reducer
struct Feature {
struct State: Equatable {
Expand Down Expand Up @@ -86,8 +86,8 @@ fileprivate enum PresentationTestCase {
return .none

case .destination(.presented(.fullScreenCover(.parentSendDismissActionButtonTapped))),
.destination(.presented(.sheet(.parentSendDismissActionButtonTapped))),
.destination(.presented(.popover(.parentSendDismissActionButtonTapped))):
.destination(.presented(.sheet(.parentSendDismissActionButtonTapped))),
.destination(.presented(.popover(.parentSendDismissActionButtonTapped))):
return .send(.destination(.dismiss))

case let .destination(.presented(.alert(alertAction))):
Expand Down Expand Up @@ -141,9 +141,9 @@ fileprivate enum PresentationTestCase {
}

case .destination(.presented(.fullScreenCover(.dismissAndAlert))),
.destination(.presented(.popover(.dismissAndAlert))),
.destination(.presented(.navigationDestination(.dismissAndAlert))),
.destination(.presented(.sheet(.dismissAndAlert))):
.destination(.presented(.popover(.dismissAndAlert))),
.destination(.presented(.navigationDestination(.dismissAndAlert))),
.destination(.presented(.sheet(.dismissAndAlert))):
state.destination = .alert(
AlertState {
TextState("Hello!")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ComposableArchitecture
import SwiftUI

fileprivate enum PresentationItemTestCase {
private enum PresentationItemTestCase {
@Reducer
struct Feature {
@Reducer
Expand Down
2 changes: 1 addition & 1 deletion Sources/ComposableArchitecture/CaseReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
///
/// You should not conform to this protocol directly. Instead, the ``Reducer()`` macro will add a
/// conformance to enums.
public protocol CaseReducer<State,Action>: Reducer
public protocol CaseReducer<State, Action>: Reducer
where State: CaseReducerState, Body: Reducer, Body.State == State, Body.Action == Action {
associatedtype State = State
associatedtype Action = Action
Expand Down
63 changes: 31 additions & 32 deletions Sources/ComposableArchitecture/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
member,
names:
named(State),
named(Action),
named(init),
named(body),
named(CaseScope),
named(scope)
named(Action),
named(init),
named(body),
named(CaseScope),
named(scope)
)
@attached(memberAttribute)
@attached(extension, conformances: Reducer, CaseReducer)
Expand All @@ -32,11 +32,11 @@
member,
names:
named(State),
named(Action),
named(init),
named(body),
named(CaseScope),
named(scope)
named(Action),
named(init),
named(body),
named(CaseScope),
named(scope)
)
@attached(memberAttribute)
@attached(extension, conformances: Reducer, CaseReducer)
Expand All @@ -51,31 +51,31 @@
///
/// See <doc:Reducers#Synthesizing-protocol-conformances-on-State-and-Action> for more
/// information.
@_documentation(visibility: public)
@_documentation(visibility:public)
public struct _SynthesizedConformance {}
#else
public struct _SynthesizedConformance {}
#endif
extension _SynthesizedConformance {
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Codable`
/// protocol.
public static let codable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Decodable`
/// protocol.
public static let decodable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Encodable`
/// protocol.
public static let encodable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Equatable`
/// protocol.
public static let equatable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Hashable`
/// protocol.
public static let hashable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Sendable`
/// protocol.
public static let sendable = Self()
}
extension _SynthesizedConformance {
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Codable`
/// protocol.
public static let codable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Decodable`
/// protocol.
public static let decodable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Encodable`
/// protocol.
public static let encodable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Equatable`
/// protocol.
public static let equatable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Hashable`
/// protocol.
public static let hashable = Self()
/// Extends the `State` or `Action` types that ``Reducer()`` creates with the `Sendable`
/// protocol.
public static let sendable = Self()
}

/// Marks the case of an enum reducer as holding onto "ephemeral" state.
///
Expand Down Expand Up @@ -201,4 +201,3 @@
module: "ComposableArchitectureMacros", type: "ViewActionMacro"
) where R.Action: ViewAction
#endif

66 changes: 37 additions & 29 deletions Sources/ComposableArchitectureMacros/ReducerMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extension ReducerMacro: ExtensionMacro {
{
return []
}
let proto = declaration.isEnum
let proto =
declaration.isEnum
? "ComposableArchitecture.CaseReducer, ComposableArchitecture.Reducer"
: "ComposableArchitecture.Reducer"
let ext: DeclSyntax =
Expand Down Expand Up @@ -132,7 +133,8 @@ extension ReducerMacro: MemberAttributeMacro {
!attributeName.starts(with: "ComposableArchitecture.ReducerBuilder")
else { return [] }
}
let genericArguments = genericArguments.count == 1
let genericArguments =
genericArguments.count == 1
? "\(genericArguments.description).State, \(genericArguments.description).Action"
: "\(genericArguments)"
return [
Expand Down Expand Up @@ -176,8 +178,8 @@ extension ReducerMacro: MemberAttributeMacro {
}
}

private extension IdentifierTypeSyntax {
var isEphemeral: Bool {
extension IdentifierTypeSyntax {
fileprivate var isEphemeral: Bool {
self.name.text == "AlertState" || self.name.text == "ConfirmationDialogState"
}
}
Expand Down Expand Up @@ -215,20 +217,23 @@ extension ReducerMacro: MemberMacro {
}
return true
}
let hasExplicitReducerBody = bindings.contains {
guard $0.initializer == nil
else { return true }
guard let name = $0.typeAnnotation?.type.as(SomeOrAnyTypeSyntax.self)?.constraint
.as(IdentifierTypeSyntax.self)?.name.text
else {
return false
}
return ["Reducer", "ReducerOf"].withQualified.contains(name)
} || hasReduceMethod
let hasBody = bindings.contains {
$0.as(PatternBindingSyntax.self)?.pattern
.as(IdentifierPatternSyntax.self)?.identifier.text == "body"
} || hasReduceMethod
let hasExplicitReducerBody =
bindings.contains {
guard $0.initializer == nil
else { return true }
guard
let name = $0.typeAnnotation?.type.as(SomeOrAnyTypeSyntax.self)?.constraint
.as(IdentifierTypeSyntax.self)?.name.text
else {
return false
}
return ["Reducer", "ReducerOf"].withQualified.contains(name)
} || hasReduceMethod
let hasBody =
bindings.contains {
$0.as(PatternBindingSyntax.self)?.pattern
.as(IdentifierPatternSyntax.self)?.identifier.text == "body"
} || hasReduceMethod
var decls: [DeclSyntax] = []
if let enumDecl = declaration.as(EnumDeclSyntax.self) {
let enumCaseElements = enumDecl.memberBlock
Expand Down Expand Up @@ -257,7 +262,8 @@ extension ReducerMacro: MemberMacro {
parameter.type.is(IdentifierTypeSyntax.self) || parameter.type.is(MemberTypeSyntax.self)
{
let type = parameter.type
let stateCase = enumCaseElement.attribute == .ephemeral
let stateCase =
enumCaseElement.attribute == .ephemeral
? element
: element.suffixed("State")
stateCaseDecls.append("case \(stateCase.trimmedDescription)")
Expand Down Expand Up @@ -311,7 +317,8 @@ extension ReducerMacro: MemberMacro {
if case let .argumentList(arguments) = node.arguments,
let startIndex = arguments.firstIndex(where: { $0.label?.text == "state" })
{
let endIndex = arguments.firstIndex(where: { $0.label?.text == "action" })
let endIndex =
arguments.firstIndex(where: { $0.label?.text == "action" })
?? arguments.endIndex
conformances.append(
contentsOf: arguments[startIndex..<endIndex].compactMap {
Expand Down Expand Up @@ -400,7 +407,8 @@ extension ReducerMacro: MemberMacro {
fixIt: .replace(
message: MacroExpansionFixItMessage("Remove '(\(arguments))'"),
oldNode: node,
newNode: node
newNode:
node
.with(\.leftParen, nil)
.with(\.arguments, nil)
.with(\.rightParen, nil)
Expand Down Expand Up @@ -487,8 +495,8 @@ private final class ReduceVisitor: SyntaxVisitor {
}
}

private extension EnumCaseDeclSyntax {
var attribute: ReducerCase.Attribute? {
extension EnumCaseDeclSyntax {
fileprivate var attribute: ReducerCase.Attribute? {
if self.isIgnored {
return .ignored
} else if self.isEphemeral {
Expand All @@ -498,12 +506,12 @@ private extension EnumCaseDeclSyntax {
}
}

var isIgnored: Bool {
fileprivate var isIgnored: Bool {
self.attributes.contains("ReducerCaseIgnored")
|| self.elements.contains { $0.parameterClause?.parameters.count != 1 }
}

var isEphemeral: Bool {
fileprivate var isEphemeral: Bool {
self.attributes.contains("ReducerCaseEphemeral")
|| self.elements.contains {
guard
Expand All @@ -517,8 +525,8 @@ private extension EnumCaseDeclSyntax {
}
}

private extension EnumCaseElementSyntax {
func suffixed(_ suffix: TokenSyntax) -> Self {
extension EnumCaseElementSyntax {
fileprivate func suffixed(_ suffix: TokenSyntax) -> Self {
var element = self
if var parameterClause = element.parameterClause,
let type = parameterClause.parameters.first?.type
Expand All @@ -531,8 +539,8 @@ private extension EnumCaseElementSyntax {
}
}

private extension AttributeListSyntax {
func contains(_ name: TokenSyntax) -> Bool {
extension AttributeListSyntax {
fileprivate func contains(_ name: TokenSyntax) -> Bool {
self.contains {
guard
case let .attribute(attribute) = $0,
Expand Down

0 comments on commit cf967a2

Please sign in to comment.