-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCaseDetectionMacro.swift
More file actions
32 lines (29 loc) · 1.07 KB
/
CaseDetectionMacro.swift
File metadata and controls
32 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import SwiftSyntax
import SwiftSyntaxMacros
import MacroToolkit
// Modified from: https://github.com/DougGregor/swift-macro-examples/blob/f61ac7cdca8dc3557e53f86e7e03df1353908d3e/MacroExamplesPlugin/CaseDetectionMacro.swift
public struct CaseDetectionMacro: MemberMacro {
public static func expansion<
Declaration: DeclGroupSyntax, Context: MacroExpansionContext
>(
of node: AttributeSyntax,
providingMembersOf declaration: Declaration,
in context: Context
) throws -> [DeclSyntax] {
guard let enum_ = Enum(declaration) else {
throw MacroError("@CaseDetectionMacro can only be attached to enum declarations")
}
return enum_.cases
.map { ($0.identifier, $0.identifier.initialUppercased) }
.map { original, uppercased in
"""
var is\(raw: uppercased): Bool {
if case .\(raw: original) = self {
return true
}
return false
}
"""
}
}
}