Skip to content

Commit

Permalink
Handle comments on cases. (#160)
Browse files Browse the repository at this point in the history
* Handle comments on cases.

* Update MacroTests.swift

---------

Co-authored-by: Stephen Celis <[email protected]>
  • Loading branch information
mbrandonw and stephencelis authored Jun 6, 2024
1 parent f5de96e commit 597683c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/CasePathsMacros/CasePathableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ extension CasePathableMacro: MemberMacro {
let rewriter = SelfRewriter(selfEquivalent: enumName)
let memberBlock = rewriter.rewrite(enumDecl.memberBlock).cast(MemberBlockSyntax.self)
let rootSwitchCases = generateCases(from: memberBlock.members, enumName: enumName) {
"case .\($0.name): return \\.\($0.name)"
"case .\($0.name): return \\.\(raw: $0.name.text)"
}
let rootSwitch: DeclSyntax =
rootSwitchCases.isEmpty
Expand Down
98 changes: 98 additions & 0 deletions Tests/CasePathsMacrosTests/CasePathableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -968,4 +968,102 @@ final class CasePathableMacroTests: XCTestCase {
"""#
}
}

func testComments() {
assertMacro {
"""
@CasePathable enum Foo {
// Comment above case
case bar
/*Comment before case*/ case baz(Int)
case fizz(buzz: String) // Comment on case
case fizzier/*Comment in case*/(Int, buzzier: String)
}
"""
} expansion: {
#"""
enum Foo {
// Comment above case
case bar
/*Comment before case*/ case baz(Int)
case fizz(buzz: String) // Comment on case
case fizzier/*Comment in case*/(Int, buzzier: String)
public struct AllCasePaths: Sequence {
public subscript(root: Foo) -> PartialCaseKeyPath<Foo> {
switch root {
case .bar:
return \.bar
case .baz:
return \.baz
case .fizz:
return \.fizz
case .fizzier/*Comment in case*/:
return \.fizzier
}
}
// Comment above case
public var bar: CasePaths.AnyCasePath<Foo, Void> {
CasePaths.AnyCasePath<Foo, Void>(
embed: {
Foo.bar
},
extract: {
guard case .bar = $0 else {
return nil
}
return ()
}
)
}
/*Comment before case*/public var baz: CasePaths.AnyCasePath<Foo, Int> {
CasePaths.AnyCasePath<Foo, Int>(
embed: Foo.baz,
extract: {
guard case let .baz(v0) = $0 else {
return nil
}
return v0
}
)
}
public var fizz: CasePaths.AnyCasePath<Foo, String> {
CasePaths.AnyCasePath<Foo, String>(
embed: Foo.fizz,
extract: {
guard case let .fizz(v0) = $0 else {
return nil
}
return v0
}
)
}
public var fizzier: CasePaths.AnyCasePath<Foo, (Int, buzzier: String)> {
CasePaths.AnyCasePath<Foo, (Int, buzzier: String)>(
embed: Foo.fizzier,
extract: {
guard case let .fizzier(v0, v1) = $0 else {
return nil
}
return (v0, v1)
}
)
}
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*/)
return allCasePaths.makeIterator()
}
}
public static var allCasePaths: AllCasePaths { AllCasePaths() }
}
extension Foo: CasePaths.CasePathable {
}
"""#
}
}
}
12 changes: 12 additions & 0 deletions Tests/CasePathsTests/MacroTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if swift(>=5.9)
import CasePaths

@CasePathable
private enum Comments {
// Comment above case
case bar
/*Comment before case*/ case baz(Int)
case fizz(buzz: String) // Comment on case
case fizzier/*Comment in case*/(Int, buzzier: String)
}
#endif

0 comments on commit 597683c

Please sign in to comment.