@@ -384,19 +384,24 @@ struct MacroSystem {
384
384
// MARK: - MacroApplication
385
385
386
386
/// Removes attributes from a syntax tree while maintaining their surrounding trivia.
387
- private class AttributeRemover : SyntaxRewriter {
388
- var attributesToRemove : [ AttributeSyntax ]
387
+ @_spi ( Testing)
388
+ public class AttributeRemover : SyntaxRewriter {
389
+ let predicate : ( AttributeSyntax ) -> Bool
389
390
390
391
var triviaToAttachToNextToken : Trivia = Trivia ( )
391
392
392
- init ( attributesToRemove: [ AttributeSyntax ] ) {
393
- self . attributesToRemove = attributesToRemove
393
+ /// Initializes an attribute remover with a given predicate to determine which attributes to remove.
394
+ ///
395
+ /// - Parameter predicate: A closure that determines whether a given `AttributeSyntax` should be removed.
396
+ /// If this closure returns `true` for an attribute, that attribute will be removed.
397
+ public init ( removingWhere predicate: @escaping ( AttributeSyntax ) -> Bool ) {
398
+ self . predicate = predicate
394
399
}
395
400
396
- override func visit( _ node: AttributeListSyntax ) -> AttributeListSyntax {
401
+ public override func visit( _ node: AttributeListSyntax ) -> AttributeListSyntax {
397
402
var filteredAttributes : [ AttributeListSyntax . Element ] = [ ]
398
403
for case . attribute( let attribute) in node {
399
- if attributesToRemove . contains ( attribute) {
404
+ if self . predicate ( attribute) {
400
405
var leadingTrivia = attribute. leadingTrivia
401
406
402
407
// Don't leave behind an empty line when the attribute being removed is on its own line,
@@ -450,7 +455,7 @@ private class AttributeRemover: SyntaxRewriter {
450
455
return AttributeListSyntax ( filteredAttributes)
451
456
}
452
457
453
- override func visit( _ token: TokenSyntax ) -> TokenSyntax {
458
+ public override func visit( _ token: TokenSyntax ) -> TokenSyntax {
454
459
return prependAndClearAccumulatedTrivia ( to: token)
455
460
}
456
461
@@ -573,7 +578,7 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
573
578
574
579
let attributesToRemove = self . macroAttributes ( attachedTo: visitedNode) . map ( \. attributeNode)
575
580
576
- return AttributeRemover ( attributesToRemove : attributesToRemove) . rewrite ( visitedNode)
581
+ return AttributeRemover ( removingWhere : { attributesToRemove. contains ( $0 ) } ) . rewrite ( visitedNode)
577
582
}
578
583
579
584
return nil
0 commit comments