diff --git a/Sources/SwiftFormatCore/SyntaxProtocol+Convenience.swift b/Sources/SwiftFormatCore/SyntaxProtocol+Convenience.swift index 01673ffcb..415c8d7f6 100644 --- a/Sources/SwiftFormatCore/SyntaxProtocol+Convenience.swift +++ b/Sources/SwiftFormatCore/SyntaxProtocol+Convenience.swift @@ -25,7 +25,6 @@ extension SyntaxProtocol { /// be returned. /// - Returns: The absolute position of the trivia piece. public func position(ofLeadingTriviaAt index: Trivia.Index) -> AbsolutePosition { - let leadingTrivia = self.leadingTrivia ?? [] guard leadingTrivia.indices.contains(index) else { preconditionFailure("Index was out of bounds in the node's leading trivia.") } diff --git a/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift b/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift index 5fc50c76f..6532c35e8 100644 --- a/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift +++ b/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift @@ -876,7 +876,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { // The node's content is either a `DictionaryElementListSyntax` or a `TokenSyntax` for a colon // token (for an empty dictionary). if !(node.content.as(DictionaryElementListSyntax.self)?.isEmpty ?? true) - || node.content.leadingTrivia?.numberOfComments ?? 0 > 0 + || node.content.leadingTrivia.numberOfComments > 0 || node.rightSquare.leadingTrivia.numberOfComments > 0 { after(node.leftSquare, tokens: .break(.open, size: 0), .open) @@ -2161,7 +2161,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { appendToken(.syntax(segmentText)) } - if node.trailingTrivia?.containsBackslashes == true { + if node.trailingTrivia.containsBackslashes { // Segments with trailing backslashes won't end with a literal newline; the backslash is // considered trivia. To preserve the original text and wrapping, we need to manually render // the backslash and a break into the token stream. diff --git a/Sources/SwiftFormatRules/NoAssignmentInExpressions.swift b/Sources/SwiftFormatRules/NoAssignmentInExpressions.swift index 8eda56e97..e327b59d7 100644 --- a/Sources/SwiftFormatRules/NoAssignmentInExpressions.swift +++ b/Sources/SwiftFormatRules/NoAssignmentInExpressions.swift @@ -60,7 +60,7 @@ public final class NoAssignmentInExpressions: SyntaxFormatRule { semicolon: nil ) .with(\.leadingTrivia, - (returnStmt.leadingTrivia ?? []) + (assignmentExpr.leadingTrivia ?? [])) + (returnStmt.leadingTrivia) + (assignmentExpr.leadingTrivia)) .with(\.trailingTrivia, [])) newItems.append( CodeBlockItemSyntax( @@ -68,7 +68,7 @@ public final class NoAssignmentInExpressions: SyntaxFormatRule { semicolon: nil ) .with(\.leadingTrivia, [.newlines(1)]) - .with(\.trailingTrivia, returnStmt.trailingTrivia?.withoutLeadingSpaces() ?? [])) + .with(\.trailingTrivia, returnStmt.trailingTrivia.withoutLeadingSpaces())) default: newItems.append(newItem) diff --git a/Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift b/Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift index b5aa6dc8e..cb5f6dc01 100644 --- a/Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift +++ b/Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift @@ -139,13 +139,11 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule { } // Check for any comments that are adjacent to the case or fallthrough statement. - if let leadingTrivia = switchCase.leadingTrivia, - leadingTrivia.drop(while: { !$0.isNewline }).contains(where: { $0.isComment }) + if switchCase.leadingTrivia.drop(while: { !$0.isNewline }).contains(where: { $0.isComment }) { return false } - if let leadingTrivia = onlyStatement.leadingTrivia, - leadingTrivia.drop(while: { !$0.isNewline }).contains(where: { $0.isComment }) + if onlyStatement.leadingTrivia.drop(while: { !$0.isNewline }).contains(where: { $0.isComment }) { return false } @@ -194,13 +192,7 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule { // Only the first violation case can have displaced trivia, because any non-whitespace // trivia in the other violation cases would've prevented collapsing. - if let displacedLeadingTrivia = cases.first!.leadingTrivia?.withoutLastLine() { - let existingLeadingTrivia = newCase.leadingTrivia ?? [] - let mergedLeadingTrivia = displacedLeadingTrivia + existingLeadingTrivia - return newCase.with(\.leadingTrivia, mergedLeadingTrivia) - } else { - return newCase - } + return newCase.with(\.leadingTrivia, cases.first!.leadingTrivia.withoutLastLine() + newCase.leadingTrivia) } } diff --git a/Sources/SwiftFormatRules/OrderedImports.swift b/Sources/SwiftFormatRules/OrderedImports.swift index 6b505a455..187b79673 100644 --- a/Sources/SwiftFormatRules/OrderedImports.swift +++ b/Sources/SwiftFormatRules/OrderedImports.swift @@ -298,23 +298,20 @@ fileprivate func generateLines(codeBlockItemList: CodeBlockItemListSyntax, conte } for block in codeBlockItemList { - - if let leadingTrivia = block.leadingTrivia { - afterNewline = false - - for piece in leadingTrivia { - switch piece { - // Create new Line objects when we encounter newlines. - case .newlines(let N): - for _ in 0.. DeclSyntax { guard let commentText = decl.docComment else { return decl } - guard let declLeadingTrivia = decl.leadingTrivia else { return decl } + let docComments = commentText.components(separatedBy: "\n") var pieces = [TriviaPiece]() // Ensures the documentation comment is a docLineComment. var hasFoundDocComment = false - for piece in declLeadingTrivia.reversed() { + for piece in decl.leadingTrivia.reversed() { if case .docBlockComment(_) = piece, !hasFoundDocComment { hasFoundDocComment = true diagnose(.avoidDocBlockComment, on: decl)