Skip to content

Commit 89dd2bd

Browse files
authored
Merge pull request #2471 from Matejkob/remove-attribute-fixit
Added unit test for testing the removal of an attached macro
2 parents f720b72 + d5dd345 commit 89dd2bd

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

Diff for: Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift

+80
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// macros are invoked. //
1919
//==========================================================================//
2020

21+
import SwiftDiagnostics
2122
import SwiftSyntax
2223
import SwiftSyntaxMacroExpansion
2324
import SwiftSyntaxMacros
@@ -424,4 +425,83 @@ final class MemberMacroTests: XCTestCase {
424425
indentationWidth: indentationWidth
425426
)
426427
}
428+
429+
func testRemoveAttributeFixIt() {
430+
struct ActorOnlyMacro: MemberMacro {
431+
static func expansion(
432+
of node: AttributeSyntax,
433+
providingMembersOf declaration: some DeclGroupSyntax,
434+
conformingTo protocols: [TypeSyntax],
435+
in context: some MacroExpansionContext
436+
) throws -> [DeclSyntax] {
437+
guard declaration.is(ActorDeclSyntax.self) else {
438+
throw DiagnosticsError(diagnostics: [
439+
Diagnostic(
440+
node: node,
441+
message: SwiftSyntaxMacros.MacroExpansionErrorMessage("'@ActorOnly' is only applicable to actors."),
442+
fixIt: FixIt(
443+
message: SwiftSyntaxMacros.MacroExpansionFixItMessage("Remove '@ActorOnly' attribute."),
444+
changes: [
445+
// This doesn't account for other attributes that *could* be present in the
446+
// attribute list, but for this test it will be fine.
447+
.replace(
448+
oldNode: Syntax(declaration.attributes),
449+
newNode: Syntax(AttributeListSyntax())
450+
)
451+
]
452+
)
453+
)
454+
])
455+
}
456+
return []
457+
}
458+
}
459+
460+
assertMacroExpansion(
461+
"""
462+
actor Foo {
463+
var foo: Int
464+
}
465+
@ActorOnly
466+
struct Bar {
467+
var bar: Int
468+
}
469+
""",
470+
expandedSource:
471+
"""
472+
actor Foo {
473+
var foo: Int
474+
}
475+
struct Bar {
476+
var bar: Int
477+
}
478+
""",
479+
diagnostics: [
480+
DiagnosticSpec(
481+
message: "'@ActorOnly' is only applicable to actors.",
482+
line: 4,
483+
column: 1,
484+
fixIts: [
485+
FixItSpec(message: "Remove '@ActorOnly' attribute.")
486+
]
487+
)
488+
],
489+
macros: [
490+
"ActorOnly": ActorOnlyMacro.self
491+
],
492+
applyFixIts: [
493+
"Remove '@ActorOnly' attribute."
494+
],
495+
fixedSource:
496+
"""
497+
actor Foo {
498+
var foo: Int
499+
}
500+
struct Bar {
501+
var bar: Int
502+
}
503+
""",
504+
indentationWidth: indentationWidth
505+
)
506+
}
427507
}

0 commit comments

Comments
 (0)