Skip to content

Fix a bug where an unfolded SequenceExpr would make it to the pretty-printer. #641

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 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: SequenceExprSyntax) -> SyntaxVisitorContinueKind {
preconditionFailure("SequenceExpr should have already been folded.")
preconditionFailure(
"""
SequenceExpr should have already been folded; found at byte offsets \
\(node.position.utf8Offset)..<\(node.endPosition.utf8Offset)
""")
}

override func visit(_ node: AssignmentExprSyntax) -> SyntaxVisitorContinueKind {
Expand Down
14 changes: 11 additions & 3 deletions Sources/SwiftFormat/Rules/UseExplicitNilCheckInConditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ public final class UseExplicitNilCheckInConditions: SyntaxFormatRule {
// trivia of the original node, since that token is being removed entirely.
var value = initializerClause.value
let trailingTrivia = value.trailingTrivia
value.trailingTrivia = []
value.trailingTrivia = [.spaces(1)]

return ConditionElementSyntax(
condition: .expression("\(node.leadingTrivia)\(value) != nil\(trailingTrivia)"))
var operatorExpr = BinaryOperatorExprSyntax(text: "!=")
operatorExpr.trailingTrivia = [.spaces(1)]

var inequalExpr = InfixOperatorExprSyntax(
leftOperand: value,
operator: operatorExpr,
rightOperand: NilLiteralExprSyntax())
inequalExpr.leadingTrivia = node.leadingTrivia
inequalExpr.trailingTrivia = trailingTrivia
return ConditionElementSyntax(condition: .expression(ExprSyntax(inequalExpr)))
default:
return node
}
Expand Down
11 changes: 5 additions & 6 deletions Sources/SwiftFormat/Rules/UseShorthandTypeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
effectSpecifiers: TypeEffectSpecifiersSyntax?,
arrow: TokenSyntax,
returnType: TypeSyntax
) -> SequenceExprSyntax? {
) -> InfixOperatorExprSyntax? {
guard
let parameterExprs = expressionRepresentation(of: parameters),
let returnTypeExpr = expressionRepresentation(of: returnType)
Expand All @@ -493,11 +493,10 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
let arrowExpr = ArrowExprSyntax(
effectSpecifiers: effectSpecifiers,
arrow: arrow)

return SequenceExprSyntax(
elements: ExprListSyntax([
ExprSyntax(tupleExpr), ExprSyntax(arrowExpr), returnTypeExpr,
]))
return InfixOperatorExprSyntax(
leftOperand: tupleExpr,
operator: arrowExpr,
rightOperand: returnTypeExpr)
}

/// Returns the leading and trailing trivia from the front and end of the entire given node.
Expand Down
11 changes: 11 additions & 0 deletions Tests/SwiftFormatTests/Rules/LintOrFormatRuleTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,16 @@ class LintOrFormatRuleTestCase: DiagnosingTestCase {
context: context,
file: file,
line: line)

// Verify that the pretty printer can consume the transformed tree (e.g., it does not contain
// any unfolded `SequenceExpr`s). We don't need to check the actual output here (we don't want
// the rule tests to be pretty-printer dependent), but this will catch invariants that aren't
// satisfied.
_ = PrettyPrinter(
context: context,
node: Syntax(actual),
printTokenStream: false,
whitespaceOnly: false
).prettyPrint()
}
}