Skip to content

Commit

Permalink
Merge pull request #2 from futuredapp/fix/parameter-list-nonlast-item
Browse files Browse the repository at this point in the history
Fix: Parameter list only works if item is last
  • Loading branch information
mikolasstuchlik authored Aug 15, 2024
2 parents 7873be1 + b4ddf5a commit ef659df
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 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
71 changes: 62 additions & 9 deletions Tests/EnumIdentableTests/EnumIdentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class EnumIdentableTests: XCTestCase {
"""
,
expandedSource:
"""
#"""
enum TestEnum {
case one
case two
Expand Down Expand Up @@ -61,7 +61,7 @@ final class EnumIdentableTests: XCTestCase {
lhs.id == rhs.id
}
}
"""
"""#
,
macros: testMacros
)
Expand All @@ -81,7 +81,7 @@ final class EnumIdentableTests: XCTestCase {
"""
,
expandedSource:
"""
#"""
enum TestEnum {
case one(String)
Expand All @@ -108,7 +108,7 @@ final class EnumIdentableTests: XCTestCase {
lhs.id == rhs.id
}
}
"""
"""#
,
macros: testMacros
)
Expand All @@ -128,7 +128,7 @@ final class EnumIdentableTests: XCTestCase {
"""
,
expandedSource:
"""
#"""
enum TestEnum {
case one, two, three
Expand Down Expand Up @@ -161,7 +161,7 @@ final class EnumIdentableTests: XCTestCase {
lhs.id == rhs.id
}
}
"""
"""#
,
macros: testMacros
)
Expand All @@ -184,7 +184,7 @@ final class EnumIdentableTests: XCTestCase {
"""
,
expandedSource:
"""
#"""
enum TestEnum {
case one(id: String)
case two(model: String)
Expand All @@ -199,13 +199,13 @@ final class EnumIdentableTests: XCTestCase {
var rawValue: String {
switch self {
case let .one(id):
"one-\\(id)"
"one-\(id)"
case .two:
"two"
case .three:
"three"
case let .four(modelId):
"four-\\(modelId)"
"four-\(modelId)"
}
}
}
Expand Down Expand Up @@ -235,7 +235,60 @@ final class EnumIdentableTests: XCTestCase {
lhs.id == rhs.id
}
}
"""#
,
macros: testMacros
)
#else
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
)
Expand Down

0 comments on commit ef659df

Please sign in to comment.