Skip to content

Commit df44c5c

Browse files
committed
Update to non-optional leading/trailingTrivia swift-syntax API
1 parent 89a0b6f commit df44c5c

File tree

5 files changed

+5
-13
lines changed

5 files changed

+5
-13
lines changed

Sources/SwiftFormatCore/SyntaxProtocol+Convenience.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ extension SyntaxProtocol {
2525
/// be returned.
2626
/// - Returns: The absolute position of the trivia piece.
2727
public func position(ofLeadingTriviaAt index: Trivia.Index) -> AbsolutePosition {
28-
let leadingTrivia = self.leadingTrivia ?? []
2928
guard leadingTrivia.indices.contains(index) else {
3029
preconditionFailure("Index was out of bounds in the node's leading trivia.")
3130
}

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
876876
// The node's content is either a `DictionaryElementListSyntax` or a `TokenSyntax` for a colon
877877
// token (for an empty dictionary).
878878
if !(node.content.as(DictionaryElementListSyntax.self)?.isEmpty ?? true)
879-
|| node.content.leadingTrivia?.numberOfComments ?? 0 > 0
879+
|| node.content.leadingTrivia.numberOfComments > 0
880880
|| node.rightSquare.leadingTrivia.numberOfComments > 0
881881
{
882882
after(node.leftSquare, tokens: .break(.open, size: 0), .open)
@@ -2161,7 +2161,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
21612161
appendToken(.syntax(segmentText))
21622162
}
21632163

2164-
if node.trailingTrivia?.containsBackslashes == true {
2164+
if node.trailingTrivia.containsBackslashes {
21652165
// Segments with trailing backslashes won't end with a literal newline; the backslash is
21662166
// considered trivia. To preserve the original text and wrapping, we need to manually render
21672167
// the backslash and a break into the token stream.

Sources/SwiftFormatRules/NoAssignmentInExpressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public final class NoAssignmentInExpressions: SyntaxFormatRule {
6868
semicolon: nil
6969
)
7070
.with(\.leadingTrivia, [.newlines(1)])
71-
.with(\.trailingTrivia, returnStmt.trailingTrivia?.withoutLeadingSpaces() ?? []))
71+
.with(\.trailingTrivia, returnStmt.trailingTrivia.withoutLeadingSpaces()))
7272

7373
default:
7474
newItems.append(newItem)

Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,7 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
194194

195195
// Only the first violation case can have displaced trivia, because any non-whitespace
196196
// trivia in the other violation cases would've prevented collapsing.
197-
if let displacedLeadingTrivia = cases.first!.leadingTrivia?.withoutLastLine() {
198-
let existingLeadingTrivia = newCase.leadingTrivia ?? []
199-
let mergedLeadingTrivia = displacedLeadingTrivia + existingLeadingTrivia
200-
return newCase.with(\.leadingTrivia, mergedLeadingTrivia)
201-
} else {
202-
return newCase
203-
}
197+
return newCase.with(\.leadingTrivia, cases.first!.leadingTrivia.withoutLastLine() + newCase.leadingTrivia)
204198
}
205199
}
206200

Sources/SwiftFormatRules/OrderedImports.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,7 @@ fileprivate class Line {
491491
// description includes all leading and trailing trivia. It would be unusual to have any
492492
// non-whitespace trivia on the components of the import. Trim off the leading trivia, where
493493
// comments could be, and trim whitespace that might be after the import.
494-
let leadingText = importDecl.leadingTrivia?.reduce(into: "") { $1.write(to: &$0) } ?? ""
495-
return importDecl.description.dropFirst(leadingText.count)
494+
return importDecl.with(\.leadingTrivia, []).description
496495
.trimmingCharacters(in: .whitespacesAndNewlines)
497496
}
498497

0 commit comments

Comments
 (0)