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 82da284
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 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 Expand Up @@ -963,6 +967,12 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
node.accessorBlock = expandAccessors(of: node, existingAccessors: node.accessorBlock).accessors
return DeclSyntax(node)
}

// override func visit(_ node: StructDeclSyntax) -> DeclSyntax {
// let node = super.visit(node).cast(StructDeclSyntax.self)
// let expansion = expandAccessors(of: node, existingAccessors: nil)
// return DeclSyntax(node)
// }
}

// MARK: Attached macro expansions.
Expand Down

0 comments on commit 82da284

Please sign in to comment.