Skip to content

Commit

Permalink
fix: use attributesToRemove instead of getting attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
RayZhao1998 committed Apr 26, 2024
1 parent fbc962f commit 09252c7
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,6 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
let attributedNode = node.asProtocol(WithAttributesSyntax.self),
!attributedNode.attributes.isEmpty
{
if (!macroAttributes(attachedTo: declSyntax, ofType: AccessorMacro.Type.self).isEmpty
&& !declSyntax.is(VariableDeclSyntax.self) && !declSyntax.is(SubscriptDeclSyntax.self))
{
contextGenerator(node).addDiagnostics(
from: MacroApplicationError.accessorMacroNotOnVariableOrSubscript,
node: declSyntax
)
}
// Apply body and preamble macros.
if let nodeWithBody = node.asProtocol(WithOptionalCodeBlockSyntax.self),
let declNodeWithBody = nodeWithBody as? any DeclSyntaxProtocol & WithOptionalCodeBlockSyntax
Expand All @@ -726,9 +718,21 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
let visitedNode = self.visit(declSyntax)
skipVisitAnyHandling.remove(Syntax(declSyntax))

let attributesToRemove = self.macroAttributes(attachedTo: visitedNode).map(\.attributeNode)
let attributesToRemove = self.macroAttributes(attachedTo: visitedNode)
attributesToRemove.forEach { (attribute, spec) in
if let _ = spec.type as? AccessorMacro.Type,
!declSyntax.is(VariableDeclSyntax.self) && !declSyntax.is(SubscriptDeclSyntax.self)
{
contextGenerator(node).addDiagnostics(
from: MacroApplicationError.accessorMacroNotOnVariableOrSubscript,
node: declSyntax
)
}
}

return AttributeRemover(removingWhere: { attributesToRemove.contains($0) }).rewrite(visitedNode)
return AttributeRemover(removingWhere: { attributesToRemove.map(\.attributeNode).contains($0) }).rewrite(
visitedNode
)
}

return nil
Expand Down

0 comments on commit 09252c7

Please sign in to comment.