Skip to content

Commit b91459c

Browse files
committed
Refactor AttributeRemover to remove attributes matching a predicate
Updated `MacroApplication` and `AttributeRemoverTests` to the new predicate-based `AttributeRemover` usage.
1 parent 8bb0705 commit b91459c

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,23 +386,18 @@ struct MacroSystem {
386386
/// Removes attributes from a syntax tree while maintaining their surrounding trivia.
387387
@_spi(Testing)
388388
public class AttributeRemover: SyntaxRewriter {
389-
var attributesToRemove: [AttributeSyntax]
390-
let attributeEquals: (AttributeSyntax, AttributeSyntax) -> Bool
389+
let predicate: (AttributeSyntax) -> Bool
391390

392391
var triviaToAttachToNextToken: Trivia = Trivia()
393392

394-
public init(
395-
attributesToRemove: [AttributeSyntax],
396-
attributeEquals: @escaping (AttributeSyntax, AttributeSyntax) -> Bool = { $0 == $1 }
397-
) {
398-
self.attributesToRemove = attributesToRemove
399-
self.attributeEquals = attributeEquals
393+
public init(where predicate: @escaping (AttributeSyntax) -> Bool) {
394+
self.predicate = predicate
400395
}
401396

402397
public override func visit(_ node: AttributeListSyntax) -> AttributeListSyntax {
403398
var filteredAttributes: [AttributeListSyntax.Element] = []
404399
for case .attribute(let attribute) in node {
405-
if attributesToRemove.contains(where: { self.attributeEquals($0, attribute) }) {
400+
if self.predicate(attribute) {
406401
var leadingTrivia = attribute.leadingTrivia
407402

408403
// Don't leave behind an empty line when the attribute being removed is on its own line,
@@ -579,7 +574,7 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
579574

580575
let attributesToRemove = self.macroAttributes(attachedTo: visitedNode).map(\.attributeNode)
581576

582-
return AttributeRemover(attributesToRemove: attributesToRemove).rewrite(visitedNode)
577+
return AttributeRemover(where: { attributesToRemove.contains($0) }).rewrite(visitedNode)
583578
}
584579

585580
return nil

Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ fileprivate func assertSyntaxRemovingTestAttributes(
2222
file: StaticString = #filePath,
2323
line: UInt = #line
2424
) {
25-
let attributesToRemove = ["@Test"].map(AttributeSyntax.init(stringLiteral:))
25+
let attributeToRemove = AttributeSyntax(stringLiteral: "@Test")
2626

2727
let reducedSource = AttributeRemover(
28-
attributesToRemove: attributesToRemove,
29-
attributeEquals: { $0.trimmedDescription == $1.trimmedDescription }
28+
where: { $0.trimmedDescription == attributeToRemove.trimmedDescription }
3029
)
3130
.rewrite(
3231
Parser.parse(source: originalSource)

0 commit comments

Comments
 (0)