Skip to content
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
80 changes: 80 additions & 0 deletions Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// macros are invoked. //
//==========================================================================//

import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
Expand Down Expand Up @@ -299,4 +300,83 @@ final class MemberMacroTests: XCTestCase {
]
)
}

func testRemoveAttributeFixIt() {
struct ActorOnlyMacro: MemberMacro {
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
guard declaration.is(ActorDeclSyntax.self) else {
throw DiagnosticsError(diagnostics: [
Diagnostic(
node: node,
message: MacroExpansionErrorMessage("'@ActorOnly' is only applicable to actors."),
fixIt: FixIt(
message: MacroExpansionFixItMessage("Remove '@ActorOnly' attribute."),
changes: [
// This doesn't account for other attributes that *could* be present in the
// attribute list, but for this test it will be fine.
.replace(
oldNode: Syntax(declaration.attributes),
newNode: Syntax(AttributeListSyntax())
)
]
)
)
])
}
return []
}
}

assertMacroExpansion(
"""
actor Foo {
var foo: Int
}

@ActorOnly
struct Bar {
var bar: Int
}
""",
expandedSource:
"""
actor Foo {
var foo: Int
}
struct Bar {
var bar: Int
}
""",
diagnostics: [
DiagnosticSpec(
message: "'@ActorOnly' is only applicable to actors.",
line: 5,
column: 1,
fixIts: [
FixItSpec(message: "Remove '@ActorOnly' attribute.")
]
)
],
macros: [
"ActorOnly": ActorOnlyMacro.self
],
applyFixIts: [
"Remove '@ActorOnly' attribute."
],
fixedSource:
"""
actor Foo {
var foo: Int
}
struct Bar {
var bar: Int
}
"""
)
}
}