Skip to content

Allow creation of debugInitCall without trivia #1140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ extension SyntaxProtocol {
/// Returns a Swift expression that, when parsed, constructs this syntax node
/// (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).
/// The intended use case for this is to print a syntax tree and create a substructure assertion from the generated expression.
public var debugInitCall: String {
return self.debugInitCallExpr.formatted(using: InitializerExprFormat()).description
/// When `includeTrivia` is set to `false`, the token's leading and trailing trivia will not be included in the generated expression.
public func debugInitCall(includeTrivia: Bool = true) -> String {
return self.debugInitCallExpr(includeTrivia: includeTrivia).formatted(using: InitializerExprFormat()).description
}

private var debugInitCallExpr: ExprSyntax {
private func debugInitCallExpr(includeTrivia: Bool) -> ExprSyntax {
let mirror = Mirror(reflecting: self)
if self.isCollection {
let typeName = String(describing: type(of: self))
Expand All @@ -117,7 +118,7 @@ extension SyntaxProtocol {
expression: ArrayExpr {
for child in mirror.children {
let value = child.value as! SyntaxProtocol?
ArrayElement(expression: value?.debugInitCallExpr ?? ExprSyntax(NilLiteralExpr()))
ArrayElement(expression: value?.debugInitCallExpr(includeTrivia: includeTrivia) ?? ExprSyntax(NilLiteralExpr()))
}
}
)
Expand Down Expand Up @@ -145,14 +146,14 @@ extension SyntaxProtocol {
expression: StringLiteralExpr(content: token.text)
)
}
if !token.leadingTrivia.isEmpty {
if includeTrivia && !token.leadingTrivia.isEmpty {
TupleExprElement(
label: .identifier("leadingTrivia"),
colon: .colon,
expression: token.leadingTrivia.initializerExpr
)
}
if !token.trailingTrivia.isEmpty {
if includeTrivia && !token.trailingTrivia.isEmpty {
TupleExprElement(
label: .identifier("trailingTrivia"),
colon: .colon,
Expand Down Expand Up @@ -180,7 +181,7 @@ extension SyntaxProtocol {
TupleExprElement(
label: isUnexpected ? nil : .identifier(label),
colon: isUnexpected ? nil : .colon,
expression: value?.debugInitCallExpr ?? ExprSyntax(NilLiteralExpr())
expression: value?.debugInitCallExpr(includeTrivia: includeTrivia) ?? ExprSyntax(NilLiteralExpr())
)
}
}
Expand Down