-
Notifications
You must be signed in to change notification settings - Fork 108
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
Support enums with cases wrapped in #if #133
Support enums with cases wrapped in #if #133
Conversation
if let ifConfigDecl = $0.decl.as(IfConfigDeclSyntax.self) { | ||
let ifClauses = ifConfigDecl.clauses.flatMap { decl -> [DeclSyntax] in | ||
guard let elements = decl.elements?.as(MemberBlockItemListSyntax.self) else { | ||
return [] | ||
} | ||
let title = "\(decl.poundKeyword.text) \(decl.condition?.description ?? "")" | ||
return ["\(raw: title)"] + generateDeclSyntax(from: elements, with: access, enumName: enumName) | ||
} | ||
return ifClauses + ["#endif"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essentially, only this part is new. Here we parse the condition and then recursively look for EnumCaseDeclSyntax
which are part of this IfConfigDeclSyntax
as elements
property.
For example, this code 👇
#if os(macOS)
case macCase
Would be represented by this IfConfigDeclSyntax
type 👇
IfConfigDeclSyntax
├─clauses: IfConfigClauseListSyntax
│ ├─[0]: IfConfigClauseSyntax
│ │ ├─poundKeyword: poundIf
│ │ ├─condition: FunctionCallExprSyntax
│ │ │ ├─calledExpression: DeclReferenceExprSyntax
│ │ │ │ ╰─baseName: identifier("os")
│ │ │ ├─leftParen: leftParen
│ │ │ ├─arguments: LabeledExprListSyntax
│ │ │ │ ╰─[0]: LabeledExprSyntax
│ │ │ │ ╰─expression: DeclReferenceExprSyntax
│ │ │ │ ╰─baseName: identifier("macOS")
│ │ │ ├─rightParen: rightParen
│ │ │ ╰─additionalTrailingClosures: MultipleTrailingClosureElementListSyntax
│ │ ╰─elements: MemberBlockItemListSyntax
│ │ ├─[0]: MemberBlockItemSyntax
│ │ │ ╰─decl: EnumCaseDeclSyntax
│ │ │ ├─attributes: AttributeListSyntax
│ │ │ ├─modifiers: DeclModifierListSyntax
│ │ │ ├─caseKeyword: keyword(SwiftSyntax.Keyword.case)
│ │ │ ╰─elements: EnumCaseElementListSyntax
│ │ │ ╰─[0]: EnumCaseElementSyntax
│ │ │ ╰─name: identifier("macCase")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome addition! Thanks for taking the time to dive into the problem and write some great tests 😄
Happy to contribute 😉 🙌 |
…upport, Xcode 16 (pointfreeco#133) * Address build issues in Library Evolution mode Address build issues seen building for library evolution mode, Xcode 16 - i.e. `make build-for-library-evolution`. * Update ci.yml --------- Co-authored-by: Stephen Celis <stephen.celis@gmail.com>
Summary
This PR updates the macro code to allow it to generate case paths for cases which are conditionally compiled. For example:
Currently released implementation would generate the case path only for
bar
.Why
I'm working on a project where we use TCA. Because we share the code between iOS and macOS we have a few places where one platform has some additional actions (features) and they are wrapped with
#if os
.What do you think? Would it be useful? Appreciate any feedback 🙂