Skip to content

Commit

Permalink
Fix/update Swift-Mustache + add more tests + some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 10, 2024
1 parent 85fdbc7 commit 6649ced
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "ce25c9bdeb39120a6e58c3aa2fa205e436aca11542cc194f7b4520aecb86b27d",
"originHash" : "db12584d48eea6de43652535e6f196bf16b860f1a22c890a09e0ccf979302a90",
"pins" : [
{
"identity" : "swift-mustache",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mahdibm/swift-mustache",
"state" : {
"branch" : "mmbm-swift-6",
"revision" : "e23122de36452ff04699b0fccc9a53866250c239"
"revision" : "72f3279b228908c786d80289e7260439163654f2"
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions Sources/EnumeratorMacro/EnumCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension EnumCase.MustacheArray: MustacheTransformable {
} else {
let joined = self.underlying
.map { String(describing: $0) }
.joined(separator: "T ")
.joined(separator: ", ")
let string = EnumCase.MustacheString("(\(joined))")
return string
}
Expand Down Expand Up @@ -150,7 +150,7 @@ extension EnumCase.MustacheArrayOfOptionals: MustacheTransformable {
let joined = self.underlying
.enumerated()
.map { $1.map { String(describing: $0) } ?? "_unnamed_\($0)" }
.joined(separator: "T ")
.joined(separator: ", ")
let string = EnumCase.MustacheString("(\(joined))")
return string
}
Expand Down Expand Up @@ -197,7 +197,7 @@ extension EnumCase.Parameters: MustacheTransformable {
let joined = names
.enumerated()
.map { $1?.underlying ?? "_unnamed_\($0)" }
.joined(separator: "T ")
.joined(separator: ", ")
let string = EnumCase.MustacheString("(\(joined))")
return string
}
Expand Down
12 changes: 8 additions & 4 deletions Sources/EnumeratorMacro/EnumeratorMacroType.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftParser
import Mustache

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

guard let enumDecl = declaration.as(EnumDeclSyntax.self) else {
fatalError("Not enum")
throw MacroError.isNotEnum
}

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

guard let arguments = node.arguments else {
fatalError("No arguments")
throw MacroError.macroDeclarationHasNoArguments
}
let rendered = try arguments.as(LabeledExprListSyntax.self)!.compactMap {
$0.expression
Expand All @@ -34,8 +35,11 @@ extension EnumeratorMacroType: MemberMacro {
}.map { template in
try MustacheTemplate(string: "{{%CONTENT_TYPE:TEXT}}\n" + template)
.render(["cases": cases])
}.map(DeclSyntax.init(stringLiteral:))

}.map {
var parser = Parser($0)
return DeclSyntax.parse(from: &parser)
}

return rendered
}
}
30 changes: 30 additions & 0 deletions Sources/EnumeratorMacro/MacroError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import SwiftDiagnostics
import SwiftSyntax

enum MacroError: Error, CustomStringConvertible {
case isNotEnum
case macroDeclarationHasNoArguments

var description: String {
switch self {
case .isNotEnum:
"Only enums are supported."
case .macroDeclarationHasNoArguments:
"The macro declaration needs to have at least 1 StringLiteral argument."
}
}
}

extension MacroError: DiagnosticMessage {
var message: String {
self.description
}

var diagnosticID: MessageID {
.init(domain: "EnumeratorMacro.MacroError", id: self.description)
}

var severity: DiagnosticSeverity {
.error
}
}
113 changes: 91 additions & 22 deletions Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntaxMacrosTestSupport
import Testing

@Suite struct EnumeratorMacroTests {
@Test func works() throws {
@Test func createsCaseName() throws {
assertMacroExpansionWithSwiftTesting(
#"""
@Enumerator(
Expand Down Expand Up @@ -42,6 +42,96 @@ import Testing
macros: EnumeratorMacroEntryPoint.macros
)
}

@Test func createsACopyOfSelf() throws {
assertMacroExpansionWithSwiftTesting(
#"""
@Enumerator("""
enum CopyOfSelf {
{{#cases}}
case {{name}}{{joinedWithParenthesis(namesWithTypes(parameters))}}
{{/cases}}
}
""")
public enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}
"""#,
expandedSource: #"""
public enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
enum CopyOfSelf {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}
}
"""#,
macros: EnumeratorMacroEntryPoint.macros
)
}

@Test func createsDeclarationsForCaseChecking() throws {
assertMacroExpansionWithSwiftTesting(
#"""
@Enumerator("""
{{#cases}}
var is{{capitalized(name)}}: Bool {
switch self {
case .{{name}}:
return true
default:
return false
}
}
{{/cases}}
""")
public enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}
"""#,
expandedSource: #"""
public enum TestEnum {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
var isA: Bool {
switch self {
case .a:
return true
default:
return false
}
}
var isB: Bool {
switch self {
case .b:
return true
default:
return false
}
}
var isTestcase: Bool {
switch self {
case .testCase:
return true
default:
return false
}
}
}
"""#,
macros: EnumeratorMacroEntryPoint.macros
)
}
}

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




/// {{#namesWithTypes(parameters)}}{{joined(.)}}{{/namesWithTypes(parameters)}}

@Enumerator("""
enum CopyOfSelf: String {
{{#cases}}
case {{name}}{{#namesWithTypes(parameters)}}{{joined(.)}}{{/namesWithTypes(parameters)}}
{{/cases}}
}
""")
public enum TestEnum2 {
case a(val1: String, val2: Int)
case b
case testCase(testValue: String)
}




@Enumerator("""
enum Subtype: String {
{{#cases}}
Expand Down

0 comments on commit 6649ced

Please sign in to comment.