Skip to content

Commit f5c61fa

Browse files
committed
Allow creation of debugInitCall without trivia
1 parent d18b675 commit f5c61fa

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
@@ -99,19 +99,20 @@ extension SyntaxProtocol {
9999
/// Returns a Swift expression that, when parsed, constructs this syntax node
100100
/// (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).
101101
/// The intended use case for this is to print a syntax tree and create a substructure assertion from the generated expression.
102-
public var debugInitCall: String {
103-
return self.debugInitCallExpr.formatted(using: InitializerExprFormat()).description
102+
/// When `includeTrivia` is set to `false`, the token's leading and trailing trivia will not be included in the generated expression.
103+
public func debugInitCall(includeTrivia: Bool = true) -> String {
104+
return self.debugInitCallExpr(includeTrivia: includeTrivia).formatted(using: InitializerExprFormat()).description
104105
}
105106

106-
private var debugInitCallExpr: ExprSyntaxProtocol {
107+
private func debugInitCallExpr(includeTrivia: Bool) -> ExprSyntaxProtocol {
107108
let mirror = Mirror(reflecting: self)
108109
if self.isCollection {
109110
return FunctionCallExpr(callee: "\(type(of: self))") {
110111
TupleExprElement(
111112
expression: ArrayExpr() {
112113
for child in mirror.children {
113114
let value = child.value as! SyntaxProtocol?
114-
ArrayElement(expression: value?.debugInitCallExpr ?? NilLiteralExpr())
115+
ArrayElement(expression: value?.debugInitCallExpr(includeTrivia: includeTrivia) ?? NilLiteralExpr())
115116
}
116117
}
117118
)
@@ -137,14 +138,14 @@ extension SyntaxProtocol {
137138
expression: StringLiteralExpr(content: token.text)
138139
)
139140
}
140-
if !token.leadingTrivia.isEmpty {
141+
if includeTrivia, !token.leadingTrivia.isEmpty {
141142
TupleExprElement(
142143
label: .identifier("leadingTrivia"),
143144
colon: .colon,
144145
expression: token.leadingTrivia.initializerExpr
145146
)
146147
}
147-
if !token.trailingTrivia.isEmpty {
148+
if includeTrivia, !token.trailingTrivia.isEmpty {
148149
TupleExprElement(
149150
label: .identifier("trailingTrivia"),
150151
colon: .colon,
@@ -169,7 +170,7 @@ extension SyntaxProtocol {
169170
TupleExprElement(
170171
label: isUnexpected ? nil : .identifier(label),
171172
colon: isUnexpected ? nil : .colon,
172-
expression: value?.debugInitCallExpr ?? NilLiteralExpr()
173+
expression: value?.debugInitCallExpr(includeTrivia: includeTrivia) ?? NilLiteralExpr()
173174
)
174175
}
175176
}

0 commit comments

Comments
 (0)