Skip to content

Commit db5c06a

Browse files
committed
Handle complete function names with raw pieces
1 parent 73e0701 commit db5c06a

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

Sources/TestingMacros/Support/Additions/FunctionDeclSyntaxAdditions.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010

1111
import SwiftSyntax
12+
import SwiftSyntaxBuilder
1213
import SwiftSyntaxMacros
1314

1415
extension FunctionDeclSyntax {
@@ -35,16 +36,24 @@ extension FunctionDeclSyntax {
3536

3637
/// The name of this function including parentheses, parameter labels, and
3738
/// colons.
38-
var completeName: String {
39-
var result = [name.textWithoutBackticks, "(",]
40-
41-
for parameter in signature.parameterClause.parameters {
42-
result.append(parameter.firstName.textWithoutBackticks)
43-
result.append(":")
39+
var completeName: DeclReferenceExprSyntax {
40+
func possiblyRaw(_ token: TokenSyntax) -> TokenSyntax {
41+
if let rawIdentifier = token.rawIdentifier {
42+
return .identifier("`\(rawIdentifier)`")
43+
}
44+
return .identifier(token.textWithoutBackticks)
4445
}
45-
result.append(")")
4646

47-
return result.joined()
47+
return DeclReferenceExprSyntax(
48+
baseName: possiblyRaw(name),
49+
argumentNames: DeclNameArgumentsSyntax(
50+
arguments: DeclNameArgumentListSyntax {
51+
for parameter in signature.parameterClause.parameters {
52+
DeclNameArgumentSyntax(name: possiblyRaw(parameter.firstName))
53+
}
54+
}
55+
)
56+
)
4857
}
4958

5059
/// An array of tuples representing this function's parameters.

Sources/TestingMacros/TestDeclarationMacro.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
407407
var testsBody: CodeBlockItemListSyntax = """
408408
return [
409409
.__function(
410-
named: \(literal: functionDecl.completeName),
410+
named: \(literal: functionDecl.completeName.trimmedDescription),
411411
in: \(typeNameExpr),
412412
xcTestCompatibleSelector: \(selectorExpr ?? "nil"),
413413
\(raw: attributeInfo.functionArgumentList(in: context)),
@@ -433,7 +433,7 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
433433
private \(_staticKeyword(for: typeName)) nonisolated func \(unavailableTestName)() async -> [Testing.Test] {
434434
[
435435
.__function(
436-
named: \(literal: functionDecl.completeName),
436+
named: \(literal: functionDecl.completeName.trimmedDescription),
437437
in: \(typeNameExpr),
438438
xcTestCompatibleSelector: \(selectorExpr ?? "nil"),
439439
\(raw: attributeInfo.functionArgumentList(in: context)),

Tests/TestingMacrosTests/TestDeclarationMacroTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ struct TestDeclarationMacroTests {
271271
#expect(TokenSyntax.identifier("`class struct`").rawIdentifier != nil)
272272
}
273273

274+
@Test("Raw function name components")
275+
func rawFunctionNameComponents() throws {
276+
let decl = """
277+
func `__raw__$hello`(`__raw__$world`: T, etc: U, `blah`: V) {}
278+
""" as DeclSyntax
279+
let functionDecl = try #require(decl.as(FunctionDeclSyntax.self))
280+
#expect(functionDecl.completeName.trimmedDescription == "`hello`(`world`:etc:blah:)")
281+
}
282+
274283
@Test("Warning diagnostics emitted on API misuse",
275284
arguments: [
276285
// return types

0 commit comments

Comments
 (0)