Skip to content

Commit d5ab1c7

Browse files
authored
Merge pull request #2329 from Matejkob/update-macro-example-tests
2 parents 3914c0b + f567587 commit d5ab1c7

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

Examples/Sources/MacroExamples/Implementation/Peer/AddCompletionHandlerMacro.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public struct AddCompletionHandlerMacro: PeerMacro {
3434
var newEffects: FunctionEffectSpecifiersSyntax
3535
if let existingEffects = funcDecl.signature.effectSpecifiers {
3636
newEffects = existingEffects
37-
newEffects.asyncSpecifier = .keyword(.async)
37+
newEffects.asyncSpecifier = .keyword(.async, trailingTrivia: .space)
3838
} else {
39-
newEffects = FunctionEffectSpecifiersSyntax(asyncSpecifier: .keyword(.async))
39+
newEffects = FunctionEffectSpecifiersSyntax(asyncSpecifier: .keyword(.async, trailingTrivia: .space))
4040
}
4141

4242
var newSignature = funcDecl.signature

Examples/Tests/MacroExamples/Implementation/Expression/AddBlockerTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ final class AddBlockerTests: XCTestCase {
3939
)
4040
}
4141

42+
func testExpansionWithSubtractionAppliesFixIt() {
43+
assertMacroExpansion(
44+
"""
45+
#addBlocker(x * y + z)
46+
""",
47+
expandedSource: """
48+
x * y - z
49+
""",
50+
diagnostics: [
51+
DiagnosticSpec(
52+
message: "blocked an add; did you mean to subtract?",
53+
line: 1,
54+
column: 19,
55+
severity: .warning,
56+
fixIts: [FixItSpec(message: "use '-'")]
57+
)
58+
],
59+
macros: macros,
60+
applyFixIts: ["use '-'"],
61+
fixedSource:
62+
"""
63+
#addBlocker(x * y - z)
64+
""",
65+
indentationWidth: .spaces(2)
66+
)
67+
}
68+
4269
func testExpansionPreservesSubtraction() {
4370
assertMacroExpansion(
4471
"""

Examples/Tests/MacroExamples/Implementation/Peer/AddCompletionHandlerMacroTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,44 @@ final class AddCompletionHandlerMacroTests: XCTestCase {
9898
indentationWidth: .spaces(2)
9999
)
100100
}
101+
102+
func testExpansionOnNonAsyncFunctionAppliesFixIt() {
103+
assertMacroExpansion(
104+
"""
105+
struct Test {
106+
@AddCompletionHandler
107+
func fetchData() -> String {
108+
return "Hello, World!"
109+
}
110+
}
111+
""",
112+
expandedSource: """
113+
struct Test {
114+
func fetchData() -> String {
115+
return "Hello, World!"
116+
}
117+
}
118+
""",
119+
diagnostics: [
120+
DiagnosticSpec(
121+
message: "can only add a completion-handler variant to an 'async' function",
122+
line: 3,
123+
column: 3,
124+
severity: .error,
125+
fixIts: [FixItSpec(message: "add 'async'")]
126+
)
127+
],
128+
macros: macros,
129+
applyFixIts: ["add 'async'"],
130+
fixedSource: """
131+
struct Test {
132+
@AddCompletionHandler
133+
func fetchData() async -> String {
134+
return "Hello, World!"
135+
}
136+
}
137+
""",
138+
indentationWidth: .spaces(2)
139+
)
140+
}
101141
}

0 commit comments

Comments
 (0)