@@ -103,11 +103,12 @@ extension SyntaxProtocol {
103
103
/// Returns a Swift expression that, when parsed, constructs this syntax node
104
104
/// (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).
105
105
/// 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
108
109
}
109
110
110
- private var debugInitCallExpr : ExprSyntax {
111
+ private func debugInitCallExpr( includeTrivia : Bool ) -> ExprSyntax {
111
112
let mirror = Mirror ( reflecting: self )
112
113
if self . isCollection {
113
114
let typeName = String ( describing: type ( of: self ) )
@@ -117,7 +118,7 @@ extension SyntaxProtocol {
117
118
expression: ArrayExpr {
118
119
for child in mirror. children {
119
120
let value = child. value as! SyntaxProtocol ?
120
- ArrayElement ( expression: value? . debugInitCallExpr ?? ExprSyntax ( NilLiteralExpr ( ) ) )
121
+ ArrayElement ( expression: value? . debugInitCallExpr ( includeTrivia : includeTrivia ) ?? ExprSyntax ( NilLiteralExpr ( ) ) )
121
122
}
122
123
}
123
124
)
@@ -145,14 +146,14 @@ extension SyntaxProtocol {
145
146
expression: StringLiteralExpr ( content: token. text)
146
147
)
147
148
}
148
- if !token. leadingTrivia. isEmpty {
149
+ if includeTrivia && !token. leadingTrivia. isEmpty {
149
150
TupleExprElement (
150
151
label: . identifier( " leadingTrivia " ) ,
151
152
colon: . colon,
152
153
expression: token. leadingTrivia. initializerExpr
153
154
)
154
155
}
155
- if !token. trailingTrivia. isEmpty {
156
+ if includeTrivia && !token. trailingTrivia. isEmpty {
156
157
TupleExprElement (
157
158
label: . identifier( " trailingTrivia " ) ,
158
159
colon: . colon,
@@ -180,7 +181,7 @@ extension SyntaxProtocol {
180
181
TupleExprElement (
181
182
label: isUnexpected ? nil : . identifier( label) ,
182
183
colon: isUnexpected ? nil : . colon,
183
- expression: value? . debugInitCallExpr ?? ExprSyntax ( NilLiteralExpr ( ) )
184
+ expression: value? . debugInitCallExpr ( includeTrivia : includeTrivia ) ?? ExprSyntax ( NilLiteralExpr ( ) )
184
185
)
185
186
}
186
187
}
0 commit comments