Skip to content

Commit 2dc1c26

Browse files
author
Adam Cmiel
committed
[BasicMacroExpansionContext] attributes not found in basic expansion without source file
1 parent 367883a commit 2dc1c26

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Tests/SwiftSyntaxMacroExpansionTest/PeerMacroTests.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,54 @@ final class PeerMacroTests: XCTestCase {
276276
indentationWidth: indentationWidth
277277
)
278278
}
279+
280+
func testCanDetectAdjacentAttributesInContext() {
281+
enum FoundObjCAttribute: String, Error, CustomDebugStringConvertible {
282+
case noMethod
283+
case noAttribute
284+
case notFound
285+
case found
286+
var debugDescription: String { rawValue }
287+
}
288+
289+
struct FindObjCAttribute: PeerMacro {
290+
static func expansion(
291+
of node: AttributeSyntax,
292+
providingPeersOf declaration: some DeclSyntaxProtocol,
293+
in context: some MacroExpansionContext
294+
) throws -> [DeclSyntax] {
295+
guard let methodDecl = declaration.as(FunctionDeclSyntax.self) else {
296+
throw FoundObjCAttribute.noMethod
297+
}
298+
299+
guard let attribute = methodDecl.attributes.first(where: {
300+
$0.as(AttributeSyntax.self)?.attributeName == "objc"
301+
}) else {
302+
throw FoundObjCAttribute.noAttribute
303+
}
304+
305+
guard context.location(of: attribute) != nil else {
306+
throw FoundObjCAttribute.notFound
307+
}
308+
309+
return []
310+
}
311+
}
312+
assertMacroExpansion(
313+
"""
314+
@objc class Foo {
315+
@objc @findObjCAttribute
316+
func memberFunction() {}
317+
}
318+
""",
319+
expandedSource: """
320+
@objc class Foo {
321+
@objc
322+
func memberFunction() {}
323+
}
324+
""",
325+
diagnostics: [],
326+
macros: ["findObjCAttribute": FindObjCAttribute.self],
327+
)
328+
}
279329
}

0 commit comments

Comments
 (0)