Skip to content

Commit d7d7a09

Browse files
committed
Address review comments in #1654
1 parent 2c857ae commit d7d7a09

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

Package.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,19 @@ let package = Package(
179179
exclude: ["CMakeLists.txt"]
180180
),
181181

182+
.testTarget(
183+
name: "SwiftSyntaxMacrosTest",
184+
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntaxBuilder", "SwiftSyntaxMacros", "SwiftSyntaxMacrosTestSupport"]
185+
),
186+
187+
// MARK: SwiftSyntaxMacroExpansion
188+
182189
.target(
183190
name: "SwiftSyntaxMacroExpansion",
184191
dependencies: ["SwiftSyntax", "SwiftSyntaxMacros"],
185192
exclude: ["CMakeLists.txt"]
186193
),
187194

188-
.testTarget(
189-
name: "SwiftSyntaxMacrosTest",
190-
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntaxBuilder", "SwiftSyntaxMacros", "SwiftSyntaxMacrosTestSupport"]
191-
),
192-
193195
// MARK: SwiftSyntaxMacrosTestSupport
194196

195197
.target(

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ public enum MacroRole {
1313
}
1414

1515
/// Simple diagnostic message
16-
private struct MacroExpansionError: Error, CustomStringConvertible {
17-
let description: String
16+
private enum MacroExpansionError: String, Error, CustomStringConvertible {
17+
case unmathedMacroRole = "macro doesn't conform to required macro role"
18+
case parentDeclGroupNil = "parent decl group is nil"
19+
case declarationNotDeclGroup = "declaration is not a decl group syntax"
20+
case declarationNotIdentified = "declaration is not a 'Identified' syntax"
21+
var description: String { self.rawValue }
1822
}
1923

2024
/// Expand `@freestanding(XXX)` macros.
25+
///
26+
/// - Parameters:
27+
/// - definition: a type conforms to one of freestanding `Macro` protocol.
28+
/// - node: macro expansion syntax node (e.g. `#macroName(argument)`).
29+
/// - in: context of the expansion.
30+
/// - Returns: expanded source text. Upon failure (i.e. `defintion.expansion()`
31+
/// throws) returns `nil`, and the diagnostics representing the `Error` are
32+
/// guaranteed to be added to context.
2133
public func expandFreestandingMacro(
2234
definition: Macro.Type,
2335
node: FreestandingMacroExpansionSyntax,
@@ -51,7 +63,7 @@ public func expandFreestandingMacro(
5163
expandedSyntax = Syntax(CodeBlockItemListSyntax(rewritten))
5264

5365
default:
54-
throw MacroExpansionError(description: "macro doesn't conform to required macro role")
66+
throw MacroExpansionError.unmathedMacroRole
5567
}
5668
return expandedSyntax.formattedExpansion(definition.formatMode)
5769
}
@@ -63,6 +75,18 @@ public func expandFreestandingMacro(
6375
}
6476

6577
/// Expand `@attached(XXX)` macros.
78+
///
79+
/// - Parameters:
80+
/// - definition: a type that conforms to one or more attached `Macro` protocols.
81+
/// - macroRole: indicates which `Macro` protocol expansion should be performed
82+
/// - attributeNode: attribute syntax node (e.g. `@macroName(argument)`).
83+
/// - declarationNode: target declaration syntax node to apply the expansion.
84+
/// - parentDeclNode: Only used for `MacroRole.memberAttribute`. The parent
85+
/// context node of `declarationNode`.
86+
/// - in: context of the expansion.
87+
/// - Returns: A list of expanded source text. Upon failure (i.e.
88+
/// `defintion.expansion()` throws) returns `nil`, and the diagnostics
89+
/// representing the `Error` are guaranteed to be added to context.
6690
public func expandAttachedMacro<Context: MacroExpansionContext>(
6791
definition: Macro.Type,
6892
macroRole: MacroRole,
@@ -88,7 +112,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
88112
let parentDeclGroup = parentDeclNode?.asProtocol(DeclGroupSyntax.self)
89113
else {
90114
// Compiler error: 'parentDecl' is mandatory for MemberAttributeMacro.
91-
throw MacroExpansionError(description: "parent decl group is nil")
115+
throw MacroExpansionError.parentDeclGroupNil
92116
}
93117

94118
// Local function to expand a member attribute macro once we've opened up
@@ -118,7 +142,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
118142
guard let declGroup = declarationNode.asProtocol(DeclGroupSyntax.self)
119143
else {
120144
// Compiler error: declNode for member macro must be DeclGroupSyntax.
121-
throw MacroExpansionError(description: "declaration is not a decl group syntax")
145+
throw MacroExpansionError.declarationNotDeclGroup
122146
}
123147

124148
// Local function to expand a member macro once we've opened up
@@ -151,12 +175,14 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
151175
}
152176

153177
case (let attachedMacro as ConformanceMacro.Type, .conformance):
154-
guard
155-
let declGroup = declarationNode.asProtocol(DeclGroupSyntax.self),
156-
let identified = declarationNode.asProtocol(IdentifiedDeclSyntax.self)
178+
guard let declGroup = declarationNode.asProtocol(DeclGroupSyntax.self) else {
179+
// Compiler error: type mismatch.
180+
throw MacroExpansionError.declarationNotDeclGroup
181+
}
182+
guard let identified = declarationNode.asProtocol(IdentifiedDeclSyntax.self)
157183
else {
158184
// Compiler error: type mismatch.
159-
throw MacroExpansionError(description: "declaration is not a identified decl group")
185+
throw MacroExpansionError.declarationNotIdentified
160186
}
161187

162188
// Local function to expand a conformance macro once we've opened up
@@ -185,7 +211,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
185211
}
186212

187213
default:
188-
throw MacroExpansionError(description: "macro doesn't conform to required macro role")
214+
throw MacroExpansionError.unmathedMacroRole
189215
}
190216
} catch {
191217
context.addDiagnostics(from: error, node: attributeNode)

0 commit comments

Comments
 (0)