-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test showing that a fix-it wrongly replaces parent instead of tar…
…geted child See #15.
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import MacroTesting | ||
import SwiftDiagnostics | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import XCTest | ||
|
||
private enum ReplaceFirstMemberMacro: MemberMacro { | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingMembersOf declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
guard | ||
let nodeToReplace = declaration.memberBlock.members.first, | ||
let newNode = try? MemberBlockItemSyntax( | ||
decl: VariableDeclSyntax(SyntaxNodeString(stringLiteral: "\n let oye: Oye")) | ||
) | ||
else { return [] } | ||
|
||
context.diagnose( | ||
Diagnostic( | ||
node: node.attributeName, | ||
message: SimpleDiagnosticMessage( | ||
message: "First member needs to be replaced", | ||
diagnosticID: MessageID(domain: "domain", id: "diagnostic2"), | ||
severity: .warning | ||
), | ||
fixIts: [ | ||
FixIt( | ||
message: SimpleDiagnosticMessage( | ||
message: "Replace the first member", | ||
diagnosticID: MessageID(domain: "domain", id: "fixit1"), | ||
severity: .error | ||
), | ||
changes: [ | ||
.replace(oldNode: Syntax(nodeToReplace), newNode: Syntax(newNode)) | ||
] | ||
) | ||
] | ||
) | ||
) | ||
|
||
return [] | ||
} | ||
} | ||
|
||
final class FixItTests: BaseTestCase { | ||
override func invokeTest() { | ||
withMacroTesting(macros: [ReplaceFirstMemberMacro.self]) { | ||
super.invokeTest() | ||
} | ||
} | ||
|
||
func testReplaceFirstMember_IncorrectlyReplacesParent() { | ||
assertMacro { | ||
""" | ||
@ReplaceFirstMember | ||
struct FooBar { | ||
let foo: Foo | ||
let bar: Bar | ||
let baz: Baz | ||
} | ||
""" | ||
} diagnostics: { | ||
""" | ||
@ReplaceFirstMember | ||
┬───────────────── | ||
╰─ ⚠️ First member needs to be replaced | ||
✏️ Replace the first member | ||
struct FooBar { | ||
let foo: Foo | ||
let bar: Bar | ||
let baz: Baz | ||
} | ||
""" | ||
} fixes: { | ||
""" | ||
@ReplaceFirstMember | ||
struct FooBar { | ||
let oye: Oye | ||
} | ||
""" | ||
} expansion: { | ||
""" | ||
struct FooBar { | ||
let oye: Oye | ||
} | ||
""" | ||
} | ||
} | ||
} |