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

Conversation

jasin755
Copy link

Fix: Ensure @ViewAction(for:) Macro Compatibility with Preprocessor Directives

Summary

This PR updates the @ViewAction(for:) macro to be compatible with preprocessor directives, such as #if canImport(AppKit). Previously, the macro did not handle conditional compilation correctly, which could lead to issues when using platform-specific attributes.

Changes

  • Improved handling of #if canImport(AppKit) to support platform-dependent attributes.
  • Ensured that attributes like @Perception.Bindable and @Bindable can be conditionally applied based on platform availability.
  • Refactored macro expansion logic to correctly resolve conditional code paths.

Example Usage

#if canImport(AppKit)  
@Perception.Bindable  
var store: StoreOf<MyReducer>  
#else  
@Bindable  
var store: StoreOf<MyReducer>  
#endif  

Impact

This change improves compatibility when using @ViewAction(for:) in cross-platform projects, ensuring that platform-specific attributes are correctly recognized and applied.

Testing

  • Verified compilation on different platforms (iOS/macOS).
  • Ensured that the correct attribute is applied depending on platform availability.

@stephencelis
Copy link
Member

@jasin755 Thanks for taking the time to look into this and implement! I think one thing that still needs to be accounted for is nested #ifs, so some kind of recursion is required.

There is a SwiftIfConfig module in SwiftSyntax that provides helpers for this so that you don't need to write manual recursive code: https://github.com/swiftlang/swift-syntax/blob/main/Sources/SwiftIfConfig/SwiftIfConfig.docc/SwiftIfConfig.md

We haven't explored it yet, but do you think you can take a crack at support as well as another test with more complex nested #if statements?

@jasin755
Copy link
Author

Thanks for pointing this out! Yes, recursion will indeed be necessary—I actually realized this myself the day after submitting the PR. I just haven't had the chance to look into it yet, and I'm still getting familiar with Swift Macros.
I'll definitely explore the SwiftIfConfig module you mentioned to handle nested #ifs, and I'll prepare an additional test case with more complex nested conditions soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants