Skip to content

Commit

Permalink
Fix case for ids not being last
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolasstuchlik committed Aug 15, 2024
1 parent 7873be1 commit a52eca4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Sources/EnumIdentableMacros/EnumIdentableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,22 @@ public struct EnumIdentableMacro: MemberMacro {

let enumCaseParameterClause = enumCase.children(viewMode: .fixedUp).filter{ $0.kind == .enumCaseParameterClause }
let enumCaseParameterList = enumCaseParameterClause.flatMap { $0.children(viewMode: .fixedUp).filter { $0.kind == .enumCaseParameterList }}
let enumCaseParameter = enumCaseParameterList.flatMap { $0.children(viewMode: .fixedUp).filter { $0.kind == .enumCaseParameter }}
let parametersTokens = enumCaseParameter.compactMap {
let parameterName = $0.firstToken(viewMode: .fixedUp)
let parameterType = $0.lastToken(viewMode: .fixedUp)
return (parameterName, parameterType)
let enumCaseParameter: [SyntaxChildren.Element] = enumCaseParameterList.flatMap { $0.children(viewMode: .fixedUp).filter { $0.kind == .enumCaseParameter }}
let parametersTokens = enumCaseParameter.compactMap { item -> (TokenSyntax?, TypeSyntax)? in
guard
let item = item.as(EnumCaseParameterSyntax.self)
else {
return nil
}
return (item.firstName, item.type)
}
// Check if the case contains an parameter that contains "id"
let parameters: [(name: String, type: String)] = parametersTokens.compactMap { name, type in
if case let .identifier(idName) = name?.tokenKind,
idName.lowercased().contains("id"),
case let .identifier(typeName) = type?.tokenKind {
return (name: idName, type: typeName)
if
case let .identifier(idName) = name?.tokenKind,
idName.lowercased().contains("id")
{
return (name: idName, type: "\(type)")
}
return (name: "_", type: "_")
}
Expand Down
53 changes: 53 additions & 0 deletions Tests/EnumIdentableTests/EnumIdentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,57 @@ final class EnumIdentableTests: XCTestCase {
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}

func testEnumWithAssociatedValuesMarkedAsIdProject() throws {
#if canImport(EnumIdentableMacros)
assertMacroExpansion(
"""
@EnumIdentable
enum Destination: Hashable, Identifiable {
case destination(id: Int, a: String)
}
"""
,
expandedSource:
"""
enum Destination: Hashable, Identifiable {
case destination(id: Int, a: String)
enum CaseID {
case destination(id: Int)
var rawValue: String {
switch self {
case let .destination(id):
"destination-\\(id)"
}
}
}
var caseId: CaseID {
switch self {
case let .destination(id, _):
.destination(id: id)
}
}
var id: String {
self.caseId.rawValue
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id
}
}
"""
,
macros: testMacros
)
#else
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}
}

0 comments on commit a52eca4

Please sign in to comment.