Skip to content

Commit 5a99c24

Browse files
authored
Add support for Swift 6.1 trailing commas (#224)
* Add support for Swift 6.1 trailing commas * Add trailing commas test case
1 parent 41b89b8 commit 5a99c24

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

Sources/CasePathsMacros/CasePathableMacro.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ extension EnumCaseElementListSyntax.Element {
435435
associatedValue.parameters[index].secondName = nil
436436
}
437437
}
438+
439+
// Remove trailing comma from the last parameter for tuple type generation
440+
if let lastIndex = associatedValue.parameters.indices.last {
441+
associatedValue.parameters[lastIndex] = associatedValue.parameters[lastIndex]
442+
.with(\.trailingComma, nil)
443+
}
444+
438445
return "(\(associatedValue.parameters.trimmed))"
439446
}
440447
} else {

Tests/CasePathsMacrosTests/CasePathableMacroTests.swift

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,5 +1294,100 @@
12941294
"""#
12951295
}
12961296
}
1297+
1298+
func testTrailingCommas() {
1299+
assertMacro {
1300+
"""
1301+
@CasePathable enum Action {
1302+
case exampleAction(
1303+
param1: String,
1304+
param2: String,
1305+
param3: String,
1306+
)
1307+
case singleParam(
1308+
value: Int,
1309+
)
1310+
case multipleWithLabels(
1311+
first: String,
1312+
second: Bool,
1313+
third: Double,
1314+
)
1315+
}
1316+
"""
1317+
} expansion: {
1318+
#"""
1319+
enum Action {
1320+
case exampleAction(
1321+
param1: String,
1322+
param2: String,
1323+
param3: String,
1324+
)
1325+
case singleParam(
1326+
value: Int,
1327+
)
1328+
case multipleWithLabels(
1329+
first: String,
1330+
second: Bool,
1331+
third: Double,
1332+
)
1333+
1334+
public struct AllCasePaths: CasePaths.CasePathReflectable, Swift.Sendable, Swift.Sequence {
1335+
public subscript(root: Action) -> CasePaths.PartialCaseKeyPath<Action> {
1336+
if root.is(\.exampleAction) {
1337+
return \.exampleAction
1338+
}
1339+
if root.is(\.singleParam) {
1340+
return \.singleParam
1341+
}
1342+
if root.is(\.multipleWithLabels) {
1343+
return \.multipleWithLabels
1344+
}
1345+
return \.never
1346+
}
1347+
public var exampleAction: CasePaths.AnyCasePath<Action, (param1: String,
1348+
param2: String,
1349+
param3: String)> {
1350+
._$embed(Action.exampleAction) {
1351+
guard case let .exampleAction(v0, v1, v2) = $0 else {
1352+
return nil
1353+
}
1354+
return (v0, v1, v2)
1355+
}
1356+
}
1357+
public var singleParam: CasePaths.AnyCasePath<Action, Int> {
1358+
._$embed(Action.singleParam) {
1359+
guard case let .singleParam(v0) = $0 else {
1360+
return nil
1361+
}
1362+
return v0
1363+
}
1364+
}
1365+
public var multipleWithLabels: CasePaths.AnyCasePath<Action, (first: String,
1366+
second: Bool,
1367+
third: Double)> {
1368+
._$embed(Action.multipleWithLabels) {
1369+
guard case let .multipleWithLabels(v0, v1, v2) = $0 else {
1370+
return nil
1371+
}
1372+
return (v0, v1, v2)
1373+
}
1374+
}
1375+
public func makeIterator() -> Swift.IndexingIterator<[CasePaths.PartialCaseKeyPath<Action>]> {
1376+
var allCasePaths: [CasePaths.PartialCaseKeyPath<Action>] = []
1377+
allCasePaths.append(\.exampleAction)
1378+
allCasePaths.append(\.singleParam)
1379+
allCasePaths.append(\.multipleWithLabels)
1380+
return allCasePaths.makeIterator()
1381+
}
1382+
}
1383+
public static var allCasePaths: AllCasePaths { AllCasePaths() }
1384+
}
1385+
1386+
extension Action: CasePaths.CasePathable, CasePaths.CasePathIterable {
1387+
}
1388+
"""#
1389+
}
1390+
}
1391+
12971392
}
12981393
#endif

0 commit comments

Comments
 (0)