Finding type decorated by AttributeSyntax

I have swift source containing various types (struct, enum...etc), decorated with attributes. For example:

struct Bar {
    let x = 0
    var y = 1
    // . . .

I am using a SyntaxVisitor to search for SyntaxProtocol of type AttributeSyntax and checking the name is "Foo". This all works fine.

The next step after I've collected these together is to get the objects that are decorated by the "Foo" attribute. So in this case, I want the StructDeclSyntax from the source tree for the Bar object, so I can inspect its children.

I can't work out how to get this object though. Given an AttributeSyntax, is there a way to get the object (class, enum, struct...etc) that it is attached to?


If I'm reading the tree right, its parent should be an AttributeListSyntax, and the parent of that should be whatever node the attribute list is on.


Ah, there we go. Thanks @John_McCall.

1 Like

To work out related questions, consider the Swift AST explorer:


Wow. That’s super helpful. Thanks @wes1