Skip to content

Commit 2eb4b3b

Browse files
committed
Include code comments before expectations which are preceded by try/await in recorded issues
1 parent 463a7a7 commit 2eb4b3b

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Sources/TestingMacros/ConditionMacro.swift

+10-1
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,17 @@ extension ConditionMacro {
157157
expandedFunctionName = conditionArgument.expandedFunctionName
158158
}
159159

160-
// Capture any comments as well (either in source or as a macro argument.)
160+
// Capture any comments as well -- either in source, preceding the
161+
// expression macro or one of its lexical context nodes, or as an argument
162+
// to the macro.
161163
let commentsArrayExpr = ArrayExprSyntax {
164+
// Lexical context is ordered innermost-to-outermost, so reverse it to
165+
// maintain the expected order.
166+
for lexicalSyntaxNode in context.lexicalContext.reversed() {
167+
for commentTraitExpr in createCommentTraitExprs(for: lexicalSyntaxNode) {
168+
ArrayElementSyntax(expression: commentTraitExpr)
169+
}
170+
}
162171
for commentTraitExpr in createCommentTraitExprs(for: macro) {
163172
ArrayElementSyntax(expression: commentTraitExpr)
164173
}

Tests/TestingMacrosTests/ConditionMacroTests.swift

+37
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,43 @@ struct ConditionMacroTests {
240240
// Capture me
241241
Testing.__checkValue(try x(), expression: .__fromSyntaxNode("try x()"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
242242
""",
243+
244+
"""
245+
// Capture me
246+
try #expect(x)
247+
""":
248+
"""
249+
// Capture me
250+
try Testing.__checkValue(x, expression: .__fromSyntaxNode("x"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
251+
""",
252+
253+
"""
254+
// Capture me
255+
await #expect(x)
256+
""":
257+
"""
258+
// Capture me
259+
await Testing.__checkValue(x, expression: .__fromSyntaxNode("x"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
260+
""",
261+
262+
"""
263+
// Ignore me
264+
265+
// Comment for try
266+
try
267+
// Comment for await
268+
await
269+
// Comment for expect
270+
#expect(x)
271+
""":
272+
"""
273+
// Comment for try
274+
try
275+
// Comment for await
276+
await
277+
// Comment for expect
278+
Testing.__checkValue(x, expression: .__fromSyntaxNode("x"), comments: [.__line("// Comment for try"), .__line("// Comment for await"), .__line("// Comment for expect")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
279+
""",
243280
]
244281
)
245282
func commentCapture(input: String, expectedOutput: String) throws {

0 commit comments

Comments
 (0)