Skip to content

Commit

Permalink
Handle comments on cases without associated values (#163)
Browse files Browse the repository at this point in the history
* Support comments on cases w/o associated vals

* Add enum case w/o value and regen snapshot

* Update MacroTests.swift

* Regen snapshot
  • Loading branch information
twocentstudios authored Jun 7, 2024
1 parent c58e88b commit fe3bfeb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/CasePathsMacros/CasePathableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension CasePathableMacro: MemberMacro {
}
let casePaths = generateDeclSyntax(from: memberBlock.members, enumName: enumName)
let allCases = generateCases(from: memberBlock.members, enumName: enumName) {
"allCasePaths.append(\\.\($0.name))"
"allCasePaths.append(\\.\(raw: $0.name.text))"
}

return [
Expand Down
21 changes: 20 additions & 1 deletion Tests/CasePathsMacrosTests/CasePathableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ final class CasePathableMacroTests: XCTestCase {
/*Comment before case*/ case baz(Int)
case fizz(buzz: String) // Comment on case
case fizzier/*Comment in case*/(Int, buzzier: String)
case fizziest // Comment without associated value
}
"""
} expansion: {
Expand All @@ -1002,6 +1003,7 @@ final class CasePathableMacroTests: XCTestCase {
/*Comment before case*/ case baz(Int)
case fizz(buzz: String) // Comment on case
case fizzier/*Comment in case*/(Int, buzzier: String)
case fizziest // Comment without associated value
public struct AllCasePaths: Sequence {
public subscript(root: Foo) -> PartialCaseKeyPath<Foo> {
Expand All @@ -1017,6 +1019,9 @@ final class CasePathableMacroTests: XCTestCase {
if root.is(\.fizzier) {
return \.fizzier
}
if root.is(\.fizziest) {
return \.fizziest
}
return \.never
}
// Comment above case
Expand Down Expand Up @@ -1066,12 +1071,26 @@ final class CasePathableMacroTests: XCTestCase {
}
)
}
public var fizziest: CasePaths.AnyCasePath<Foo, Void> {
CasePaths.AnyCasePath<Foo, Void>(
embed: {
Foo.fizziest
},
extract: {
guard case .fizziest = $0 else {
return nil
}
return ()
}
)
}
public func makeIterator() -> IndexingIterator<[PartialCaseKeyPath<Foo>]> {
var allCasePaths: [PartialCaseKeyPath<Foo>] = []
allCasePaths.append(\.bar)
allCasePaths.append(\.baz)
allCasePaths.append(\.fizz)
allCasePaths.append(\.fizzier/*Comment in case*/)
allCasePaths.append(\.fizzier)
allCasePaths.append(\.fizziest)
return allCasePaths.makeIterator()
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/CasePathsTests/MacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/*Comment before case*/ case baz(Int)
case fizz(buzz: String) // Comment on case
case fizzier /*Comment in case*/(Int, buzzier: String)
case fizziest
}

@CasePathable
Expand Down

0 comments on commit fe3bfeb

Please sign in to comment.