Skip to content

Commit 597683c

Browse files
Handle comments on cases. (#160)
* Handle comments on cases. * Update MacroTests.swift --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent f5de96e commit 597683c

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

Sources/CasePathsMacros/CasePathableMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ extension CasePathableMacro: MemberMacro {
8484
let rewriter = SelfRewriter(selfEquivalent: enumName)
8585
let memberBlock = rewriter.rewrite(enumDecl.memberBlock).cast(MemberBlockSyntax.self)
8686
let rootSwitchCases = generateCases(from: memberBlock.members, enumName: enumName) {
87-
"case .\($0.name): return \\.\($0.name)"
87+
"case .\($0.name): return \\.\(raw: $0.name.text)"
8888
}
8989
let rootSwitch: DeclSyntax =
9090
rootSwitchCases.isEmpty

Tests/CasePathsMacrosTests/CasePathableMacroTests.swift

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,4 +968,102 @@ final class CasePathableMacroTests: XCTestCase {
968968
"""#
969969
}
970970
}
971+
972+
func testComments() {
973+
assertMacro {
974+
"""
975+
@CasePathable enum Foo {
976+
// Comment above case
977+
case bar
978+
/*Comment before case*/ case baz(Int)
979+
case fizz(buzz: String) // Comment on case
980+
case fizzier/*Comment in case*/(Int, buzzier: String)
981+
}
982+
"""
983+
} expansion: {
984+
#"""
985+
enum Foo {
986+
// Comment above case
987+
case bar
988+
/*Comment before case*/ case baz(Int)
989+
case fizz(buzz: String) // Comment on case
990+
case fizzier/*Comment in case*/(Int, buzzier: String)
991+
992+
public struct AllCasePaths: Sequence {
993+
public subscript(root: Foo) -> PartialCaseKeyPath<Foo> {
994+
switch root {
995+
case .bar:
996+
return \.bar
997+
case .baz:
998+
return \.baz
999+
case .fizz:
1000+
return \.fizz
1001+
case .fizzier/*Comment in case*/:
1002+
return \.fizzier
1003+
}
1004+
}
1005+
// Comment above case
1006+
public var bar: CasePaths.AnyCasePath<Foo, Void> {
1007+
CasePaths.AnyCasePath<Foo, Void>(
1008+
embed: {
1009+
Foo.bar
1010+
},
1011+
extract: {
1012+
guard case .bar = $0 else {
1013+
return nil
1014+
}
1015+
return ()
1016+
}
1017+
)
1018+
}
1019+
/*Comment before case*/public var baz: CasePaths.AnyCasePath<Foo, Int> {
1020+
CasePaths.AnyCasePath<Foo, Int>(
1021+
embed: Foo.baz,
1022+
extract: {
1023+
guard case let .baz(v0) = $0 else {
1024+
return nil
1025+
}
1026+
return v0
1027+
}
1028+
)
1029+
}
1030+
public var fizz: CasePaths.AnyCasePath<Foo, String> {
1031+
CasePaths.AnyCasePath<Foo, String>(
1032+
embed: Foo.fizz,
1033+
extract: {
1034+
guard case let .fizz(v0) = $0 else {
1035+
return nil
1036+
}
1037+
return v0
1038+
}
1039+
)
1040+
}
1041+
public var fizzier: CasePaths.AnyCasePath<Foo, (Int, buzzier: String)> {
1042+
CasePaths.AnyCasePath<Foo, (Int, buzzier: String)>(
1043+
embed: Foo.fizzier,
1044+
extract: {
1045+
guard case let .fizzier(v0, v1) = $0 else {
1046+
return nil
1047+
}
1048+
return (v0, v1)
1049+
}
1050+
)
1051+
}
1052+
public func makeIterator() -> IndexingIterator<[PartialCaseKeyPath<Foo>]> {
1053+
var allCasePaths: [PartialCaseKeyPath<Foo>] = []
1054+
allCasePaths.append(\.bar)
1055+
allCasePaths.append(\.baz)
1056+
allCasePaths.append(\.fizz)
1057+
allCasePaths.append(\.fizzier/*Comment in case*/)
1058+
return allCasePaths.makeIterator()
1059+
}
1060+
}
1061+
public static var allCasePaths: AllCasePaths { AllCasePaths() }
1062+
}
1063+
1064+
extension Foo: CasePaths.CasePathable {
1065+
}
1066+
"""#
1067+
}
1068+
}
9711069
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#if swift(>=5.9)
2+
import CasePaths
3+
4+
@CasePathable
5+
private enum Comments {
6+
// Comment above case
7+
case bar
8+
/*Comment before case*/ case baz(Int)
9+
case fizz(buzz: String) // Comment on case
10+
case fizzier/*Comment in case*/(Int, buzzier: String)
11+
}
12+
#endif

0 commit comments

Comments
 (0)