Skip to content

[BasicMacroExpansionContext] attributes not found in basic expansion … #3056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Tests/SwiftSyntaxMacroExpansionTest/PeerMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,54 @@ final class PeerMacroTests: XCTestCase {
indentationWidth: indentationWidth
)
}

func testCanDetectAdjacentAttributesInContext() {
enum FoundObjCAttribute: String, Error, CustomDebugStringConvertible {
case noMethod
case noAttribute
case notFound
case found
var debugDescription: String { rawValue }
}

struct FindObjCAttribute: PeerMacro {
static func expansion(
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard let methodDecl = declaration.as(FunctionDeclSyntax.self) else {
throw FoundObjCAttribute.noMethod
}

guard let attribute = methodDecl.attributes.first(where: {
$0.as(AttributeSyntax.self)?.attributeName == "objc"
}) else {
throw FoundObjCAttribute.noAttribute
}

guard context.location(of: attribute) != nil else {
throw FoundObjCAttribute.notFound
}

return []
}
}
assertMacroExpansion(
"""
@objc class Foo {
@objc @findObjCAttribute
func memberFunction() {}
}
""",
expandedSource: """
@objc class Foo {
@objc
func memberFunction() {}
}
""",
diagnostics: [],
macros: ["findObjCAttribute": FindObjCAttribute.self],
)
}
}
Loading