Skip to content
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

Merged

Conversation

AndrewVebster
Copy link
Contributor

Summary

This PR updates the macro code to allow it to generate case paths for cases which are conditionally compiled. For example:

@CasePathable enum Foo {
  case bar
  #if DEBUG
  case debugCase
  #endif
}

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 🙂

Comment on lines +111 to +120
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"]
}
Copy link
Contributor Author

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")

Copy link
Member

@stephencelis stephencelis left a 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 😄

@stephencelis stephencelis merged commit dafa669 into pointfreeco:main Dec 5, 2023
5 checks passed
@AndrewVebster
Copy link
Contributor Author

Happy to contribute 😉 🙌

johankool pushed a commit to johankool/swift-case-paths that referenced this pull request Nov 2, 2024
…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>
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