Skip to content

Commit 3c65036

Browse files
committed
[Macros] Rename "formatted expansion" function to generalize to all post-expansion adjustments
This is preparation for additional adjustments.
1 parent 77874c1 commit 3c65036

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroExpansion.swift

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public func expandFreestandingMacro(
156156
(.codeItem, _), (.preamble, _), (.body, _):
157157
throw MacroExpansionError.unmatchedMacroRole(definition, macroRole)
158158
}
159-
return expandedSyntax.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
159+
return expandedSyntax.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
160160
} catch {
161161
context.addDiagnostics(from: error, node: node)
162162
return nil
@@ -273,7 +273,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
273273
in: context
274274
)
275275
return accessors.map {
276-
$0.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
276+
$0.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
277277
}
278278

279279
case (let attachedMacro as MemberAttributeMacro.Type, .memberAttribute):
@@ -294,7 +294,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
294294

295295
// Form a buffer containing an attribute list to return to the caller.
296296
return attributes.map {
297-
$0.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
297+
$0.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
298298
}
299299

300300
case (let attachedMacro as MemberMacro.Type, .member):
@@ -313,7 +313,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
313313

314314
// Form a buffer of member declarations to return to the caller.
315315
return members.map {
316-
$0.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
316+
$0.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
317317
}
318318

319319
case (let attachedMacro as PeerMacro.Type, .peer):
@@ -326,7 +326,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
326326

327327
// Form a buffer of peer declarations to return to the caller.
328328
return peers.map {
329-
$0.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
329+
$0.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
330330
}
331331

332332
case (let attachedMacro as ExtensionMacro.Type, .extension):
@@ -357,7 +357,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
357357

358358
// Form a buffer of peer declarations to return to the caller.
359359
return extensions.map {
360-
$0.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
360+
$0.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
361361
}
362362

363363
case (let attachedMacro as PreambleMacro.Type, .preamble):
@@ -375,7 +375,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
375375
in: context
376376
)
377377
return preamble.map {
378-
$0.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
378+
$0.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
379379
}
380380

381381
case (let attachedMacro as BodyMacro.Type, .body):
@@ -400,7 +400,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
400400
}
401401

402402
return body.map {
403-
$0.formattedExpansion(definition.formatMode, indentationWidth: indentationWidth)
403+
$0.adjustedMacroExpansion(for: definition, indentationWidth: indentationWidth)
404404
}
405405

406406
default:
@@ -511,15 +511,24 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
511511
}
512512

513513
fileprivate extension SyntaxProtocol {
514-
/// Perform a format if required and then trim any leading/trailing
515-
/// whitespace.
516-
func formattedExpansion(_ mode: FormatMode, indentationWidth: Trivia?) -> String {
517-
switch mode {
514+
/// Perform post-expansion adjustments to the result of a macro expansion.
515+
///
516+
/// This applies adjustments to the result of a macro expansion to normalize
517+
/// it for use in later tools. Each of the adjustments here should have a
518+
/// corresponding configuration option in the `Macro` protocol.
519+
func adjustedMacroExpansion(
520+
for macro: Macro.Type,
521+
indentationWidth: Trivia?
522+
) -> String {
523+
let syntax = Syntax(self)
524+
525+
// Formatting.
526+
switch macro.formatMode {
518527
case .auto:
519-
return self.formatted(using: BasicFormat(indentationWidth: indentationWidth))
528+
return syntax.formatted(using: BasicFormat(indentationWidth: indentationWidth))
520529
.trimmedDescription(matching: \.isWhitespace)
521530
case .disabled:
522-
return Syntax(self).description
531+
return syntax.description
523532
#if RESILIENT_LIBRARIES
524533
@unknown default:
525534
fatalError()

0 commit comments

Comments
 (0)