Skip to content

Commit 6649ced

Browse files
committed
Fix/update Swift-Mustache + add more tests + some fixes
1 parent 85fdbc7 commit 6649ced

File tree

5 files changed

+134
-31
lines changed

5 files changed

+134
-31
lines changed

Diff for: Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"originHash" : "ce25c9bdeb39120a6e58c3aa2fa205e436aca11542cc194f7b4520aecb86b27d",
2+
"originHash" : "db12584d48eea6de43652535e6f196bf16b860f1a22c890a09e0ccf979302a90",
33
"pins" : [
44
{
55
"identity" : "swift-mustache",
66
"kind" : "remoteSourceControl",
77
"location" : "https://github.com/mahdibm/swift-mustache",
88
"state" : {
99
"branch" : "mmbm-swift-6",
10-
"revision" : "e23122de36452ff04699b0fccc9a53866250c239"
10+
"revision" : "72f3279b228908c786d80289e7260439163654f2"
1111
}
1212
},
1313
{

Diff for: Sources/EnumeratorMacro/EnumCase.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ extension EnumCase.MustacheArray: MustacheTransformable {
9999
} else {
100100
let joined = self.underlying
101101
.map { String(describing: $0) }
102-
.joined(separator: "T ")
102+
.joined(separator: ", ")
103103
let string = EnumCase.MustacheString("(\(joined))")
104104
return string
105105
}
@@ -150,7 +150,7 @@ extension EnumCase.MustacheArrayOfOptionals: MustacheTransformable {
150150
let joined = self.underlying
151151
.enumerated()
152152
.map { $1.map { String(describing: $0) } ?? "_unnamed_\($0)" }
153-
.joined(separator: "T ")
153+
.joined(separator: ", ")
154154
let string = EnumCase.MustacheString("(\(joined))")
155155
return string
156156
}
@@ -197,7 +197,7 @@ extension EnumCase.Parameters: MustacheTransformable {
197197
let joined = names
198198
.enumerated()
199199
.map { $1?.underlying ?? "_unnamed_\($0)" }
200-
.joined(separator: "T ")
200+
.joined(separator: ", ")
201201
let string = EnumCase.MustacheString("(\(joined))")
202202
return string
203203
}

Diff for: Sources/EnumeratorMacro/EnumeratorMacroType.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SwiftDiagnostics
22
import SwiftSyntax
33
import SwiftSyntaxMacros
4+
import SwiftParser
45
import Mustache
56

67
public enum EnumeratorMacroType {}
@@ -14,7 +15,7 @@ extension EnumeratorMacroType: MemberMacro {
1415
if declaration.hasError { return [] }
1516

1617
guard let enumDecl = declaration.as(EnumDeclSyntax.self) else {
17-
fatalError("Not enum")
18+
throw MacroError.isNotEnum
1819
}
1920

2021
let members = enumDecl.memberBlock.members
@@ -23,7 +24,7 @@ extension EnumeratorMacroType: MemberMacro {
2324
let cases = try elements.map(EnumCase.init(from:))
2425

2526
guard let arguments = node.arguments else {
26-
fatalError("No arguments")
27+
throw MacroError.macroDeclarationHasNoArguments
2728
}
2829
let rendered = try arguments.as(LabeledExprListSyntax.self)!.compactMap {
2930
$0.expression
@@ -34,8 +35,11 @@ extension EnumeratorMacroType: MemberMacro {
3435
}.map { template in
3536
try MustacheTemplate(string: "{{%CONTENT_TYPE:TEXT}}\n" + template)
3637
.render(["cases": cases])
37-
}.map(DeclSyntax.init(stringLiteral:))
38-
38+
}.map {
39+
var parser = Parser($0)
40+
return DeclSyntax.parse(from: &parser)
41+
}
42+
3943
return rendered
4044
}
4145
}

Diff for: Sources/EnumeratorMacro/MacroError.swift

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import SwiftDiagnostics
2+
import SwiftSyntax
3+
4+
enum MacroError: Error, CustomStringConvertible {
5+
case isNotEnum
6+
case macroDeclarationHasNoArguments
7+
8+
var description: String {
9+
switch self {
10+
case .isNotEnum:
11+
"Only enums are supported."
12+
case .macroDeclarationHasNoArguments:
13+
"The macro declaration needs to have at least 1 StringLiteral argument."
14+
}
15+
}
16+
}
17+
18+
extension MacroError: DiagnosticMessage {
19+
var message: String {
20+
self.description
21+
}
22+
23+
var diagnosticID: MessageID {
24+
.init(domain: "EnumeratorMacro.MacroError", id: self.description)
25+
}
26+
27+
var severity: DiagnosticSeverity {
28+
.error
29+
}
30+
}

Diff for: Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift

+91-22
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import SwiftSyntaxMacrosTestSupport
44
import Testing
55

66
@Suite struct EnumeratorMacroTests {
7-
@Test func works() throws {
7+
@Test func createsCaseName() throws {
88
assertMacroExpansionWithSwiftTesting(
99
#"""
1010
@Enumerator(
@@ -42,6 +42,96 @@ import Testing
4242
macros: EnumeratorMacroEntryPoint.macros
4343
)
4444
}
45+
46+
@Test func createsACopyOfSelf() throws {
47+
assertMacroExpansionWithSwiftTesting(
48+
#"""
49+
@Enumerator("""
50+
enum CopyOfSelf {
51+
{{#cases}}
52+
case {{name}}{{joinedWithParenthesis(namesWithTypes(parameters))}}
53+
{{/cases}}
54+
}
55+
""")
56+
public enum TestEnum {
57+
case a(val1: String, val2: Int)
58+
case b
59+
case testCase(testValue: String)
60+
}
61+
"""#,
62+
expandedSource: #"""
63+
public enum TestEnum {
64+
case a(val1: String, val2: Int)
65+
case b
66+
case testCase(testValue: String)
67+
68+
enum CopyOfSelf {
69+
case a(val1: String, val2: Int)
70+
case b
71+
case testCase(testValue: String)
72+
}
73+
}
74+
"""#,
75+
macros: EnumeratorMacroEntryPoint.macros
76+
)
77+
}
78+
79+
@Test func createsDeclarationsForCaseChecking() throws {
80+
assertMacroExpansionWithSwiftTesting(
81+
#"""
82+
@Enumerator("""
83+
{{#cases}}
84+
var is{{capitalized(name)}}: Bool {
85+
switch self {
86+
case .{{name}}:
87+
return true
88+
default:
89+
return false
90+
}
91+
}
92+
{{/cases}}
93+
""")
94+
public enum TestEnum {
95+
case a(val1: String, val2: Int)
96+
case b
97+
case testCase(testValue: String)
98+
}
99+
"""#,
100+
expandedSource: #"""
101+
public enum TestEnum {
102+
case a(val1: String, val2: Int)
103+
case b
104+
case testCase(testValue: String)
105+
106+
var isA: Bool {
107+
switch self {
108+
case .a:
109+
return true
110+
default:
111+
return false
112+
}
113+
}
114+
var isB: Bool {
115+
switch self {
116+
case .b:
117+
return true
118+
default:
119+
return false
120+
}
121+
}
122+
var isTestcase: Bool {
123+
switch self {
124+
case .testCase:
125+
return true
126+
default:
127+
return false
128+
}
129+
}
130+
}
131+
"""#,
132+
macros: EnumeratorMacroEntryPoint.macros
133+
)
134+
}
45135
}
46136

47137
@attached(member, names: arbitrary)
@@ -50,27 +140,6 @@ macro Enumerator(_ templates: String...) = #externalMacro(
50140
type: "EnumeratorMacroType"
51141
)
52142

53-
54-
55-
56-
/// {{#namesWithTypes(parameters)}}{{joined(.)}}{{/namesWithTypes(parameters)}}
57-
58-
@Enumerator("""
59-
enum CopyOfSelf: String {
60-
{{#cases}}
61-
case {{name}}{{#namesWithTypes(parameters)}}{{joined(.)}}{{/namesWithTypes(parameters)}}
62-
{{/cases}}
63-
}
64-
""")
65-
public enum TestEnum2 {
66-
case a(val1: String, val2: Int)
67-
case b
68-
case testCase(testValue: String)
69-
}
70-
71-
72-
73-
74143
@Enumerator("""
75144
enum Subtype: String {
76145
{{#cases}}

0 commit comments

Comments
 (0)