Skip to content

Commit e887f7c

Browse files
committed
Organize Macro Examples into Type-Specific Directories
This commit restructures the macro examples into type-specific directories, streamlining the search process for newcomers and enhancing overall accessibility
1 parent 335d341 commit e887f7c

16 files changed

+31
-37
lines changed

Examples/Sources/MacroExamples/Implementation/FontLiteralMacro.swift renamed to Examples/Sources/MacroExamples/Implementation/Expression/FontLiteralMacro.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import SwiftSyntaxMacros
1616
/// Implementation of the `#fontLiteral` macro, which is similar in spirit
1717
/// to the built-in expressions `#colorLiteral`, `#imageLiteral`, etc., but in
1818
/// a small macro.
19-
public struct FontLiteralMacro: ExpressionMacro {
19+
public enum FontLiteralMacro: ExpressionMacro {
2020
public static func expansion(
21-
of macro: some FreestandingMacroExpansionSyntax,
21+
of node: some FreestandingMacroExpansionSyntax,
2222
in context: some MacroExpansionContext
23-
) -> ExprSyntax {
23+
) throws -> ExprSyntax {
2424
let argList = replaceFirstLabel(
25-
of: macro.arguments,
25+
of: node.arguments,
2626
with: "fontLiteralName"
2727
)
2828
return ".init(\(argList))"
@@ -40,6 +40,8 @@ private func replaceFirstLabel(
4040
}
4141

4242
var tuple = tuple
43-
tuple[tuple.startIndex] = firstElement.with(\.label, .identifier(newLabel))
43+
tuple[tuple.startIndex] = firstElement
44+
.with(\.label, .identifier(newLabel))
45+
.with(\.colon, .colonToken())
4446
return tuple
4547
}

Examples/Sources/MacroExamples/Implementation/StringifyMacro.swift renamed to Examples/Sources/MacroExamples/Implementation/Expression/StringifyMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import SwiftSyntaxMacros
2323
/// will expand to
2424
///
2525
/// (x + y, "x + y")
26-
public struct StringifyMacro: ExpressionMacro {
26+
public enum StringifyMacro: ExpressionMacro {
2727
public static func expansion(
2828
of node: some FreestandingMacroExpansionSyntax,
2929
in context: some MacroExpansionContext

Examples/Sources/MacroExamples/Implementation/URLMacro.swift renamed to Examples/Sources/MacroExamples/Implementation/Expression/URLMacro.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ import SwiftSyntaxMacros
1616

1717
/// Creates a non-optional URL from a static string. The string is checked to
1818
/// be valid during compile time.
19-
public struct URLMacro: ExpressionMacro {
19+
public enum URLMacro: ExpressionMacro {
2020
public static func expansion(
2121
of node: some FreestandingMacroExpansionSyntax,
2222
in context: some MacroExpansionContext
2323
) throws -> ExprSyntax {
24-
2524
guard let argument = node.arguments.first?.expression,
2625
let segments = argument.as(StringLiteralExprSyntax.self)?.segments,
2726
segments.count == 1,

Examples/Sources/MacroExamples/Implementation/WarningMacro.swift renamed to Examples/Sources/MacroExamples/Implementation/Expression/WarningMacro.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import SwiftSyntaxMacros
1616

1717
/// Implementation of the `myWarning` macro, which mimics the behavior of the
1818
/// built-in `#warning`.
19-
public struct WarningMacro: ExpressionMacro {
19+
public enum WarningMacro: ExpressionMacro {
2020
public static func expansion(
21-
of macro: some FreestandingMacroExpansionSyntax,
21+
of node: some FreestandingMacroExpansionSyntax,
2222
in context: some MacroExpansionContext
2323
) throws -> ExprSyntax {
24-
guard let firstElement = macro.arguments.first,
24+
guard let firstElement = node.arguments.first,
2525
let stringLiteral = firstElement.expression
2626
.as(StringLiteralExprSyntax.self),
2727
stringLiteral.segments.count == 1,
@@ -32,10 +32,10 @@ public struct WarningMacro: ExpressionMacro {
3232

3333
context.diagnose(
3434
Diagnostic(
35-
node: Syntax(macro),
35+
node: Syntax(node),
3636
message: SimpleDiagnosticMessage(
3737
message: messageString.content.description,
38-
diagnosticID: MessageID(domain: "test", id: "error"),
38+
diagnosticID: MessageID(domain: "test123", id: "error"),
3939
severity: .warning
4040
)
4141
)

Examples/Sources/MacroExamples/Implementation/CaseDetectionMacro.swift renamed to Examples/Sources/MacroExamples/Implementation/Member/CaseDetectionMacro.swift

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,11 @@
1313
import SwiftSyntax
1414
import SwiftSyntaxMacros
1515

16-
extension TokenSyntax {
17-
fileprivate var initialUppercased: String {
18-
let name = self.text
19-
guard let initial = name.first else {
20-
return name
21-
}
22-
23-
return "\(initial.uppercased())\(name.dropFirst())"
24-
}
25-
}
26-
27-
public struct CaseDetectionMacro: MemberMacro {
28-
public static func expansion<
29-
Declaration: DeclGroupSyntax,
30-
Context: MacroExpansionContext
31-
>(
16+
public enum CaseDetectionMacro: MemberMacro {
17+
public static func expansion(
3218
of node: AttributeSyntax,
33-
providingMembersOf declaration: Declaration,
34-
in context: Context
19+
providingMembersOf declaration: some DeclGroupSyntax,
20+
in context: some MacroExpansionContext
3521
) throws -> [DeclSyntax] {
3622
declaration.memberBlock.members
3723
.compactMap { $0.decl.as(EnumCaseDeclSyntax.self) }
@@ -50,3 +36,14 @@ public struct CaseDetectionMacro: MemberMacro {
5036
}
5137
}
5238
}
39+
40+
extension TokenSyntax {
41+
fileprivate var initialUppercased: String {
42+
let name = self.text
43+
guard let initial = name.first else {
44+
return name
45+
}
46+
47+
return "\(initial.uppercased())\(name.dropFirst())"
48+
}
49+
}

Examples/Sources/MacroExamples/Implementation/CustomCodable.swift renamed to Examples/Sources/MacroExamples/Implementation/Member/CustomCodable.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
import SwiftSyntax
1414
import SwiftSyntaxMacros
1515

16-
public struct CustomCodable: MemberMacro {
17-
16+
public enum CustomCodable: MemberMacro {
1817
public static func expansion(
1918
of node: AttributeSyntax,
2019
providingMembersOf declaration: some DeclGroupSyntax,
2120
in context: some MacroExpansionContext
2221
) throws -> [DeclSyntax] {
23-
2422
let memberList = declaration.memberBlock.members
2523

2624
let cases = memberList.compactMap({ member -> String? in
@@ -53,8 +51,6 @@ public struct CustomCodable: MemberMacro {
5351
5452
"""
5553

56-
return [
57-
codingKeys
58-
]
54+
return [codingKeys]
5955
}
6056
}

0 commit comments

Comments
 (0)