Skip to content

Commit 47951ff

Browse files
committed
Filter lexical context to only effectful expr nodes
1 parent 5cd63a8 commit 47951ff

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Sources/TestingMacros/ConditionMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ extension ConditionMacro {
163163
let commentsArrayExpr = ArrayExprSyntax {
164164
// Lexical context is ordered innermost-to-outermost, so reverse it to
165165
// maintain the expected order.
166-
for lexicalSyntaxNode in context.lexicalContext.reversed() {
166+
for lexicalSyntaxNode in context.lexicalContext.effectExpressions.reversed() {
167167
for commentTraitExpr in createCommentTraitExprs(for: lexicalSyntaxNode) {
168168
ArrayElementSyntax(expression: commentTraitExpr)
169169
}

Sources/TestingMacros/Support/EffectfulExpressionHandling.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftSyntax
1212
import SwiftSyntaxBuilder
1313
import SwiftSyntaxMacros
1414

15-
// MARK: - Finding effect keywords
15+
// MARK: - Finding effect keywords and expressions
1616

1717
/// A syntax visitor class that looks for effectful keywords in a given
1818
/// expression.
@@ -69,6 +69,19 @@ func findEffectKeywords(in node: some SyntaxProtocol, context: some MacroExpansi
6969
return effectFinder.effectKeywords
7070
}
7171

72+
extension Sequence<Syntax> {
73+
/// The syntax nodes in this sequence which are effectful expressions, such as
74+
/// those for `try` or `await`.
75+
var effectExpressions: some Sequence<Syntax> {
76+
filter { node in
77+
// This could be made simpler if/when swift-syntax introduces a protocol
78+
// which all effectful expression syntax node types conform to.
79+
// See https://github.com/swiftlang/swift-syntax/issues/3040
80+
node.is(TryExprSyntax.self) || node.is(AwaitExprSyntax.self) || node.is(UnsafeExprSyntax.self)
81+
}
82+
}
83+
}
84+
7285
// MARK: - Inserting effect keywords/thunks
7386

7487
/// Make a function call expression to an effectful thunk function provided by

Tests/TestingMacrosTests/ConditionMacroTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ struct ConditionMacroTests {
277277
// Comment for expect
278278
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()
279279
""",
280+
281+
"""
282+
// Ignore me
283+
func example() {
284+
// Capture me
285+
#expect(x())
286+
}
287+
""":
288+
"""
289+
func example() {
290+
// Capture me
291+
Testing.__checkFunctionCall((), calling: { _ in
292+
x()
293+
}, expression: .__fromFunctionCall(nil, "x"), comments: [.__line("// Capture me")], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
294+
}
295+
""",
280296
]
281297
)
282298
func commentCapture(input: String, expectedOutput: String) throws {

0 commit comments

Comments
 (0)