Skip to content

Commit b7e4b01

Browse files
committed
[Macros] Rename the declarationNode argument label to node in the
macro expansion APIs.
1 parent a834159 commit b7e4b01

File tree

3 files changed

+105
-23
lines changed

3 files changed

+105
-23
lines changed

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ extension PluginProviderMessageHandler {
156156
definition: macroDefinition,
157157
macroRole: role,
158158
attributeNode: attributeNode,
159-
declarationNode: declarationNode,
159+
node: declarationNode,
160160
parentDeclNode: parentDeclNode,
161161
extendedType: extendedType,
162162
conformanceList: conformanceList,

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,52 @@ public func expandFreestandingMacro(
211211
/// - Returns: A list of expanded source text. Upon failure (i.e.
212212
/// `definition.expansion()` throws) returns `nil`, and the diagnostics
213213
/// representing the `Error` are guaranteed to be added to context.
214+
@available(*, deprecated, message: "Change the 'declarationNode' argument label to 'node'")
214215
public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>(
215216
definition: Macro.Type,
216217
macroRole: MacroRole,
217218
attributeNode: AttributeSyntax,
218-
declarationNode: some SyntaxProtocol,
219+
declarationNode: DeclSyntax,
220+
parentDeclNode: DeclSyntax?,
221+
extendedType: TypeSyntax?,
222+
conformanceList: InheritedTypeListSyntax?,
223+
in context: Context,
224+
indentationWidth: Trivia? = nil
225+
) -> [String]? {
226+
expandAttachedMacroWithoutCollapsing(
227+
definition: definition,
228+
macroRole: macroRole,
229+
attributeNode: attributeNode,
230+
node: declarationNode,
231+
parentDeclNode: parentDeclNode,
232+
extendedType: extendedType,
233+
conformanceList: conformanceList,
234+
in: context,
235+
indentationWidth: indentationWidth
236+
)
237+
}
238+
239+
/// Expand `@attached(XXX)` macros.
240+
///
241+
/// - Parameters:
242+
/// - definition: a type that conforms to one or more attached `Macro` protocols.
243+
/// - macroRole: indicates which `Macro` protocol expansion should be performed
244+
/// - attributeNode: attribute syntax node (e.g. `@macroName(argument)`).
245+
/// - node: target syntax node to apply the expansion. This is either a declaration
246+
/// or a closure syntax node.
247+
/// - parentDeclNode: Only used for `MacroRole.memberAttribute`. The parent
248+
/// context node of `declarationNode`.
249+
/// - context: context of the expansion.
250+
/// - indentationWidth: The indentation that should be added for each additional
251+
/// nesting level
252+
/// - Returns: A list of expanded source text. Upon failure (i.e.
253+
/// `definition.expansion()` throws) returns `nil`, and the diagnostics
254+
/// representing the `Error` are guaranteed to be added to context.
255+
public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>(
256+
definition: Macro.Type,
257+
macroRole: MacroRole,
258+
attributeNode: AttributeSyntax,
259+
node: some SyntaxProtocol,
219260
parentDeclNode: DeclSyntax?,
220261
extendedType: TypeSyntax?,
221262
conformanceList: InheritedTypeListSyntax?,
@@ -225,7 +266,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
225266
do {
226267
switch (definition, macroRole) {
227268
case (let attachedMacro as AccessorMacro.Type, .accessor):
228-
let declarationNode = declarationNode.cast(DeclSyntax.self)
269+
let declarationNode = node.cast(DeclSyntax.self)
229270
let accessors = try attachedMacro.expansion(
230271
of: attributeNode,
231272
providingAccessorsOf: declarationNode,
@@ -236,7 +277,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
236277
}
237278

238279
case (let attachedMacro as MemberAttributeMacro.Type, .memberAttribute):
239-
let declarationNode = declarationNode.cast(DeclSyntax.self)
280+
let declarationNode = node.cast(DeclSyntax.self)
240281
guard
241282
let parentDeclGroup = parentDeclNode?.asProtocol(DeclGroupSyntax.self)
242283
else {
@@ -257,7 +298,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
257298
}
258299

259300
case (let attachedMacro as MemberMacro.Type, .member):
260-
guard let declGroup = declarationNode.asProtocol(DeclGroupSyntax.self)
301+
guard let declGroup = node.asProtocol(DeclGroupSyntax.self)
261302
else {
262303
// Compiler error: declNode for member macro must be DeclGroupSyntax.
263304
throw MacroExpansionError.declarationNotDeclGroup
@@ -276,7 +317,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
276317
}
277318

278319
case (let attachedMacro as PeerMacro.Type, .peer):
279-
let declarationNode = declarationNode.cast(DeclSyntax.self)
320+
let declarationNode = node.cast(DeclSyntax.self)
280321
let peers = try attachedMacro.expansion(
281322
of: attributeNode,
282323
providingPeersOf: declarationNode,
@@ -289,15 +330,15 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
289330
}
290331

291332
case (let attachedMacro as ExtensionMacro.Type, .extension):
292-
guard let declGroup = declarationNode.asProtocol(DeclGroupSyntax.self) else {
333+
guard let declGroup = node.asProtocol(DeclGroupSyntax.self) else {
293334
// Compiler error: type mismatch.
294335
throw MacroExpansionError.declarationNotDeclGroup
295336
}
296337

297338
let extensionOf: TypeSyntax
298339
if let extendedType {
299340
extensionOf = extendedType
300-
} else if let identified = declarationNode.asProtocol(NamedDeclSyntax.self) {
341+
} else if let identified = node.asProtocol(NamedDeclSyntax.self) {
301342
// Fallback for old compilers with a new plugin, where
302343
extensionOf = TypeSyntax(IdentifierTypeSyntax(name: identified.name))
303344
} else {
@@ -321,7 +362,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
321362

322363
case (let attachedMacro as PreambleMacro.Type, .preamble):
323364
guard
324-
let declToPass = Syntax(declarationNode).asProtocol(SyntaxProtocol.self)
365+
let declToPass = Syntax(node).asProtocol(SyntaxProtocol.self)
325366
as? (DeclSyntaxProtocol & WithOptionalCodeBlockSyntax)
326367
else {
327368
// Compiler error: declaration must have a body.
@@ -339,13 +380,13 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
339380

340381
case (let attachedMacro as BodyMacro.Type, .body):
341382
let body: [CodeBlockItemSyntax]
342-
if let closureSyntax = declarationNode.as(ClosureExprSyntax.self) {
383+
if let closureSyntax = node.as(ClosureExprSyntax.self) {
343384
body = try attachedMacro.expansion(
344385
of: attributeNode,
345386
providingBodyFor: closureSyntax,
346387
in: context
347388
)
348-
} else if let declToPass = Syntax(declarationNode).asProtocol(SyntaxProtocol.self)
389+
} else if let declToPass = Syntax(node).asProtocol(SyntaxProtocol.self)
349390
as? (DeclSyntaxProtocol & WithOptionalCodeBlockSyntax)
350391
{
351392
body = try attachedMacro.expansion(
@@ -386,11 +427,52 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
386427
/// - Returns: expanded source text. Upon failure (i.e. `defintion.expansion()`
387428
/// throws) returns `nil`, and the diagnostics representing the `Error` are
388429
/// guaranteed to be added to context.
430+
@available(*, deprecated, message: "Change the 'declarationNode' argument label to 'node'")
431+
public func expandAttachedMacro<Context: MacroExpansionContext>(
432+
definition: Macro.Type,
433+
macroRole: MacroRole,
434+
attributeNode: AttributeSyntax,
435+
declarationNode: DeclSyntax,
436+
parentDeclNode: DeclSyntax?,
437+
extendedType: TypeSyntax?,
438+
conformanceList: InheritedTypeListSyntax?,
439+
in context: Context,
440+
indentationWidth: Trivia? = nil
441+
) -> String? {
442+
expandAttachedMacro(
443+
definition: definition,
444+
macroRole: macroRole,
445+
attributeNode: attributeNode,
446+
node: declarationNode,
447+
parentDeclNode: parentDeclNode,
448+
extendedType: extendedType,
449+
conformanceList: conformanceList,
450+
in: context,
451+
indentationWidth: indentationWidth
452+
)
453+
}
454+
455+
/// Expand `@attached(XXX)` macros.
456+
///
457+
/// - Parameters:
458+
/// - definition: a type that conforms to one or more attached `Macro` protocols.
459+
/// - macroRole: indicates which `Macro` protocol expansion should be performed
460+
/// - attributeNode: attribute syntax node (e.g. `@macroName(argument)`).
461+
/// - node: target declaration syntax node to apply the expansion. This is either
462+
/// a declaration or a closure syntax node.
463+
/// - parentDeclNode: Only used for `MacroRole.memberAttribute`. The parent
464+
/// context node of `declarationNode`.
465+
/// - context: context of the expansion.
466+
/// - indentationWidth: The indentation that should be added for each additional
467+
/// nesting level
468+
/// - Returns: expanded source text. Upon failure (i.e. `defintion.expansion()`
469+
/// throws) returns `nil`, and the diagnostics representing the `Error` are
470+
/// guaranteed to be added to context.
389471
public func expandAttachedMacro<Context: MacroExpansionContext>(
390472
definition: Macro.Type,
391473
macroRole: MacroRole,
392474
attributeNode: AttributeSyntax,
393-
declarationNode: some SyntaxProtocol,
475+
node: some SyntaxProtocol,
394476
parentDeclNode: DeclSyntax?,
395477
extendedType: TypeSyntax?,
396478
conformanceList: InheritedTypeListSyntax?,
@@ -401,7 +483,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
401483
definition: definition,
402484
macroRole: macroRole,
403485
attributeNode: attributeNode,
404-
declarationNode: declarationNode,
486+
node: node,
405487
parentDeclNode: parentDeclNode,
406488
extendedType: extendedType,
407489
conformanceList: conformanceList,
@@ -421,7 +503,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
421503
return collapse(
422504
expansions: expandedSources,
423505
for: macroRole,
424-
attachedTo: declarationNode,
506+
attachedTo: node,
425507
indentationWidth: collapseIndentationWidth
426508
)
427509
}

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private func expandMemberMacro(
194194
definition: definition,
195195
macroRole: .member,
196196
attributeNode: attributeNode.detach(in: context, foldingWith: .standardOperators),
197-
declarationNode: attachedTo.detach(in: context),
197+
node: attachedTo.detach(in: context),
198198
parentDeclNode: nil,
199199
extendedType: nil,
200200
conformanceList: conformanceList,
@@ -223,7 +223,7 @@ private func expandMemberAttributeMacro(
223223
definition: definition,
224224
macroRole: .memberAttribute,
225225
attributeNode: attributeNode.detach(in: context, foldingWith: .standardOperators),
226-
declarationNode: member.detach(in: context),
226+
node: member.detach(in: context),
227227
parentDeclNode: declaration.detach(in: context),
228228
extendedType: nil,
229229
conformanceList: nil,
@@ -255,7 +255,7 @@ private func expandPeerMacroMember(
255255
definition: definition,
256256
macroRole: .peer,
257257
attributeNode: attributeNode.detach(in: context, foldingWith: .standardOperators),
258-
declarationNode: attachedTo.detach(in: context),
258+
node: attachedTo.detach(in: context),
259259
parentDeclNode: nil,
260260
extendedType: nil,
261261
conformanceList: nil,
@@ -287,7 +287,7 @@ private func expandPeerMacroCodeItem(
287287
definition: definition,
288288
macroRole: .peer,
289289
attributeNode: attributeNode.detach(in: context, foldingWith: .standardOperators),
290-
declarationNode: attachedTo.detach(in: context),
290+
node: attachedTo.detach(in: context),
291291
parentDeclNode: nil,
292292
extendedType: nil,
293293
conformanceList: nil,
@@ -319,7 +319,7 @@ private func expandAccessorMacroWithoutExistingAccessors(
319319
definition: definition,
320320
macroRole: .accessor,
321321
attributeNode: attributeNode.detach(in: context, foldingWith: .standardOperators),
322-
declarationNode: attachedTo.detach(in: context),
322+
node: attachedTo.detach(in: context),
323323
parentDeclNode: nil,
324324
extendedType: nil,
325325
conformanceList: nil,
@@ -354,7 +354,7 @@ private func expandAccessorMacroWithExistingAccessors(
354354
definition: definition,
355355
macroRole: .accessor,
356356
attributeNode: attributeNode.detach(in: context, foldingWith: .standardOperators),
357-
declarationNode: attachedTo.detach(in: context),
357+
node: attachedTo.detach(in: context),
358358
parentDeclNode: nil,
359359
extendedType: nil,
360360
conformanceList: nil,
@@ -391,7 +391,7 @@ private func expandExtensionMacro(
391391
definition: definition,
392392
macroRole: .extension,
393393
attributeNode: attributeNode.detach(in: context, foldingWith: .standardOperators),
394-
declarationNode: attachedTo.detach(in: context),
394+
node: attachedTo.detach(in: context),
395395
parentDeclNode: nil,
396396
extendedType: extendedType.detach(in: context),
397397
conformanceList: conformanceList,
@@ -423,7 +423,7 @@ private func expandPreambleMacro(
423423
in: context,
424424
foldingWith: .standardOperators
425425
),
426-
declarationNode: DeclSyntax(decl.detach(in: context)),
426+
node: DeclSyntax(decl.detach(in: context)),
427427
parentDeclNode: nil,
428428
extendedType: nil,
429429
conformanceList: nil,
@@ -456,7 +456,7 @@ private func expandBodyMacro(
456456
in: context,
457457
foldingWith: .standardOperators
458458
),
459-
declarationNode: Syntax(node.detach(in: context)),
459+
node: Syntax(node.detach(in: context)),
460460
parentDeclNode: nil,
461461
extendedType: nil,
462462
conformanceList: nil,

0 commit comments

Comments
 (0)