Skip to content

Commit 95a3cef

Browse files
authored
Merge pull request #1140 from ahoppen/ahoppen/debuginitcall-without-trivia
Allow creation of debugInitCall without trivia
2 parents c5fa09c + 2ce1aa6 commit 95a3cef

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Sources/_SwiftSyntaxTestSupport/SyntaxProtocol+Initializer.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ extension SyntaxProtocol {
103103
/// Returns a Swift expression that, when parsed, constructs this syntax node
104104
/// (or at least an expression that's very close to constructing this node, the addition of a few manual upcast by hand is still needed).
105105
/// The intended use case for this is to print a syntax tree and create a substructure assertion from the generated expression.
106-
public var debugInitCall: String {
107-
return self.debugInitCallExpr.formatted(using: InitializerExprFormat()).description
106+
/// When `includeTrivia` is set to `false`, the token's leading and trailing trivia will not be included in the generated expression.
107+
public func debugInitCall(includeTrivia: Bool = true) -> String {
108+
return self.debugInitCallExpr(includeTrivia: includeTrivia).formatted(using: InitializerExprFormat()).description
108109
}
109110

110-
private var debugInitCallExpr: ExprSyntax {
111+
private func debugInitCallExpr(includeTrivia: Bool) -> ExprSyntax {
111112
let mirror = Mirror(reflecting: self)
112113
if self.isCollection {
113114
let typeName = String(describing: type(of: self))
@@ -117,7 +118,7 @@ extension SyntaxProtocol {
117118
expression: ArrayExpr {
118119
for child in mirror.children {
119120
let value = child.value as! SyntaxProtocol?
120-
ArrayElement(expression: value?.debugInitCallExpr ?? ExprSyntax(NilLiteralExpr()))
121+
ArrayElement(expression: value?.debugInitCallExpr(includeTrivia: includeTrivia) ?? ExprSyntax(NilLiteralExpr()))
121122
}
122123
}
123124
)
@@ -145,14 +146,14 @@ extension SyntaxProtocol {
145146
expression: StringLiteralExpr(content: token.text)
146147
)
147148
}
148-
if !token.leadingTrivia.isEmpty {
149+
if includeTrivia && !token.leadingTrivia.isEmpty {
149150
TupleExprElement(
150151
label: .identifier("leadingTrivia"),
151152
colon: .colon,
152153
expression: token.leadingTrivia.initializerExpr
153154
)
154155
}
155-
if !token.trailingTrivia.isEmpty {
156+
if includeTrivia && !token.trailingTrivia.isEmpty {
156157
TupleExprElement(
157158
label: .identifier("trailingTrivia"),
158159
colon: .colon,
@@ -180,7 +181,7 @@ extension SyntaxProtocol {
180181
TupleExprElement(
181182
label: isUnexpected ? nil : .identifier(label),
182183
colon: isUnexpected ? nil : .colon,
183-
expression: value?.debugInitCallExpr ?? ExprSyntax(NilLiteralExpr())
184+
expression: value?.debugInitCallExpr(includeTrivia: includeTrivia) ?? ExprSyntax(NilLiteralExpr())
184185
)
185186
}
186187
}

0 commit comments

Comments
 (0)