|
18 | 18 | // macros are invoked. //
|
19 | 19 | //==========================================================================//
|
20 | 20 |
|
| 21 | +import SwiftDiagnostics |
21 | 22 | import SwiftSyntax
|
22 | 23 | import SwiftSyntaxMacroExpansion
|
23 | 24 | import SwiftSyntaxMacros
|
@@ -424,4 +425,83 @@ final class MemberMacroTests: XCTestCase {
|
424 | 425 | indentationWidth: indentationWidth
|
425 | 426 | )
|
426 | 427 | }
|
| 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 | + } |
427 | 507 | }
|
0 commit comments