Skip to content

Fix CaseDetectionMacro to handle multiple elements and improve casing #3061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,28 @@ public enum CaseDetectionMacro: MemberMacro {
) throws -> [DeclSyntax] {
declaration.memberBlock.members
.compactMap { $0.decl.as(EnumCaseDeclSyntax.self) }
.map { $0.elements.first!.name }
.map { ($0, $0.initialUppercased) }
.map { original, uppercased in
.map { $0.elements }
.flatMap { $0 }
.map { $0.name }
.map { ($0, $0.pascalcased) }
.map { original, pascalcased in
"""
var is\(raw: uppercased): Bool {
if case .\(raw: original) = self {
return true
}

return false
var is\(raw: pascalcased): Bool {
self == .\(raw: original)
}
"""
}
}
}

extension TokenSyntax {
fileprivate var initialUppercased: String {
let name = self.text
guard let initial = name.first else {
return name
fileprivate var pascalcased: String {
self.text
.replacingOccurrences(of: "_", with: " ")
.replacingOccurrences(of: "(?<=.)([A-Z])", with: " $1", options: .regularExpression)
.lowercased()
.split(separator: " ")
.map { $0.capitalized }
.joined()
}

return "\(initial.uppercased())\(name.dropFirst())"
}
}