Skip to content

Added support of ViewAction for preprocessor macros #3630

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Sources/ComposableArchitectureMacros/ViewActionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,24 @@ extension SyntaxProtocol {
}
}

extension DeclGroupSyntax {
fileprivate var hasStoreVariable: Bool {
self.memberBlock.members.contains(where: { member in
if let variableDecl = member.decl.as(VariableDeclSyntax.self),
private extension DeclGroupSyntax {
var hasStoreVariable: Bool {
let members = memberBlock.members
return members.contains(where: { member in

let variableDecl: VariableDeclSyntax? =
if
let ifConfigDecl = member.decl.as(IfConfigDeclSyntax.self),
let firstClause = ifConfigDecl.clauses.first,
let member = firstClause.elements?.as(MemberBlockItemListSyntax.self)?.first
{
member.decl.as(VariableDeclSyntax.self)
} else {
member.decl.as(VariableDeclSyntax.self)
}

if
let variableDecl,
let firstBinding = variableDecl.bindings.first,
let identifierPattern = firstBinding.pattern.as(IdentifierPatternSyntax.self),
identifierPattern.identifier.text == "store"
Expand Down
34 changes: 34 additions & 0 deletions Tests/ComposableArchitectureMacrosTests/ViewActionMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@
"""
}
}

func testBindableStore_WithPreprocessorMacro() {
assertMacro {
"""
@ViewAction(for: Feature.self)
struct FeatureView: View {
#if canImport(AppKit)
@Perception.Bindable var store: StoreOf<Feature>
#else
@Bindable var store: StoreOf<Feature>
#endif
var body: some View {
EmptyView()
}
}
"""
} expansion: {
"""
struct FeatureView: View {
#if canImport(AppKit)
@Perception.Bindable var store: StoreOf<Feature>
#else
@Bindable var store: StoreOf<Feature>
#endif
var body: some View {
EmptyView()
}
}

extension FeatureView: ComposableArchitecture.ViewActionSending {
}
"""
}
}

func testNoStore() {
assertMacro {
Expand Down
Loading