Skip to content

Commit ae1670d

Browse files
authored
Merge pull request #510 from CippoX/warning-sourceaccurate
Removed warnings
2 parents f7c295d + 437b6cb commit ae1670d

18 files changed

+238
-238
lines changed

Sources/SwiftFormatCore/LegacyTriviaBehavior.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private final class LegacyTriviaBehaviorRewriter: SyntaxRewriter {
2020
token = token.with(\.leadingTrivia, pendingLeadingTrivia + token.leadingTrivia)
2121
self.pendingLeadingTrivia = nil
2222
}
23-
if token.nextToken != nil,
23+
if token.nextToken(viewMode: .sourceAccurate) != nil,
2424
let firstIndexToMove = token.trailingTrivia.firstIndex(where: shouldTriviaPieceBeMoved)
2525
{
2626
pendingLeadingTrivia = Trivia(pieces: Array(token.trailingTrivia[firstIndexToMove...]))

Sources/SwiftFormatCore/RuleMask.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
136136
// MARK: - Syntax Visitation Methods
137137

138138
override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
139-
guard let firstToken = node.firstToken else {
139+
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
140140
return .visitChildren
141141
}
142142
let comments = loneLineComments(in: firstToken.leadingTrivia, isFirstToken: true)
@@ -159,14 +159,14 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
159159
}
160160

161161
override func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
162-
guard let firstToken = node.firstToken else {
162+
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
163163
return .visitChildren
164164
}
165165
return appendRuleStatusDirectives(from: firstToken, of: Syntax(node))
166166
}
167167

168168
override func visit(_ node: MemberDeclListItemSyntax) -> SyntaxVisitorContinueKind {
169-
guard let firstToken = node.firstToken else {
169+
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
170170
return .visitChildren
171171
}
172172
return appendRuleStatusDirectives(from: firstToken, of: Syntax(node))
@@ -183,7 +183,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
183183
private func appendRuleStatusDirectives(from token: TokenSyntax, of node: Syntax)
184184
-> SyntaxVisitorContinueKind
185185
{
186-
let isFirstInFile = token.previousToken == nil
186+
let isFirstInFile = token.previousToken(viewMode: .sourceAccurate) == nil
187187
let matches = loneLineComments(in: token.leadingTrivia, isFirstToken: isFirstInFile)
188188
.compactMap(ruleStatusDirectiveMatch)
189189
let sourceRange = node.sourceRange(converter: sourceLocationConverter)

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 203 additions & 203 deletions
Large diffs are not rendered by default.

Sources/SwiftFormatRules/AddModifierRewriter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,18 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
166166
for modifiersProvider: (NodeType) -> ModifierListSyntax?
167167
) -> NodeType {
168168
guard let modifier = modifiersProvider(node)?.firstAndOnly,
169-
let movingLeadingTrivia = modifier.nextToken?.leadingTrivia
169+
let movingLeadingTrivia = modifier.nextToken(viewMode: .sourceAccurate)?.leadingTrivia
170170
else {
171171
// Otherwise, there's no trivia that needs to be relocated so the node is fine.
172172
return node
173173
}
174174
let nodeWithTrivia = replaceTrivia(
175175
on: node,
176-
token: modifier.firstToken,
176+
token: modifier.firstToken(viewMode: .sourceAccurate),
177177
leadingTrivia: movingLeadingTrivia)
178178
return replaceTrivia(
179179
on: nodeWithTrivia,
180-
token: modifiersProvider(nodeWithTrivia)?.first?.nextToken,
180+
token: modifiersProvider(nodeWithTrivia)?.first?.nextToken(viewMode: .sourceAccurate),
181181
leadingTrivia: [])
182182
}
183183
}

Sources/SwiftFormatRules/DeclSyntaxProtocol+Comments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SwiftSyntax
1515
extension DeclSyntaxProtocol {
1616
/// Searches through the leading trivia of this decl for a documentation comment.
1717
var docComment: String? {
18-
guard let tok = firstToken else { return nil }
18+
guard let tok = firstToken(viewMode: .sourceAccurate) else { return nil }
1919
var comment = [String]()
2020

2121
// We need to skip trivia until we see the first comment. This trivia will include all the

Sources/SwiftFormatRules/DoNotUseSemicolons.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public final class DoNotUseSemicolons: SyntaxFormatRule {
5656
defer { newItems[idx] = newItem }
5757

5858
// Check if the leading trivia for this statement needs a new line.
59-
if previousHadSemicolon, let firstToken = newItem.firstToken,
59+
if previousHadSemicolon, let firstToken = newItem.firstToken(viewMode: .sourceAccurate),
6060
!firstToken.leadingTrivia.containsNewlines
6161
{
6262
let leadingTrivia = .newlines(1) + firstToken.leadingTrivia

Sources/SwiftFormatRules/FullyIndirectEnum.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
5353
// If the `indirect` keyword being added would be the first token in the decl, we need to move
5454
// the leading trivia from the `enum` keyword to the new modifier to preserve the existing
5555
// line breaks/comments/indentation.
56-
let firstTok = node.firstToken!
56+
let firstTok = node.firstToken(viewMode: .sourceAccurate)!
5757
let leadingTrivia: Trivia
5858
let newEnumDecl: EnumDeclSyntax
5959

6060
if firstTok.tokenKind == .keyword(.enum) {
6161
leadingTrivia = firstTok.leadingTrivia
6262
newEnumDecl = replaceTrivia(
63-
on: node, token: node.firstToken, leadingTrivia: [])
63+
on: node, token: node.firstToken(viewMode: .sourceAccurate), leadingTrivia: [])
6464
} else {
6565
leadingTrivia = []
6666
newEnumDecl = node
@@ -97,7 +97,7 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
9797
) -> EnumCaseDeclSyntax {
9898
if let modifiers = unformattedCase.modifiers, let first = modifiers.first {
9999
return replaceTrivia(
100-
on: unformattedCase, token: first.firstToken, leadingTrivia: leadingTrivia
100+
on: unformattedCase, token: first.firstToken(viewMode: .sourceAccurate), leadingTrivia: leadingTrivia
101101
)
102102
} else {
103103
return replaceTrivia(

Sources/SwiftFormatRules/ModifierListSyntax+Convenience.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ extension ModifierListSyntax {
7272

7373
if index == 0 {
7474
guard formatTrivia else { return inserting(modifier, at: index) }
75-
guard let firstMod = first, let firstTok = firstMod.firstToken else {
75+
guard let firstMod = first, let firstTok = firstMod.firstToken(viewMode: .sourceAccurate) else {
7676
return inserting(modifier, at: index)
7777
}
7878
let formattedMod = replaceTrivia(
7979
on: modifier,
80-
token: modifier.firstToken,
80+
token: modifier.firstToken(viewMode: .sourceAccurate),
8181
leadingTrivia: firstTok.leadingTrivia)
8282
newModifiers[0] = replaceTrivia(
8383
on: firstMod,

Sources/SwiftFormatRules/NoCasesWithOnlyFallthrough.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
150150

151151
// Check for any comments that are inline on the fallthrough statement. Inline comments are
152152
// always stored in the next token's leading trivia.
153-
if let nextLeadingTrivia = onlyStatement.nextToken?.leadingTrivia,
153+
if let nextLeadingTrivia = onlyStatement.nextToken(viewMode: .sourceAccurate)?.leadingTrivia,
154154
nextLeadingTrivia.prefix(while: { !$0.isNewline }).contains(where: { $0.isComment })
155155
{
156156
return false

Sources/SwiftFormatRules/NoEmptyTrailingClosureParentheses.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
2929
{
3030
return super.visit(node)
3131
}
32-
guard let name = node.calledExpression.lastToken?.with(\.leadingTrivia, []).with(\.trailingTrivia, []) else {
32+
guard let name = node.calledExpression.lastToken(viewMode: .sourceAccurate)?.with(\.leadingTrivia, []).with(\.trailingTrivia, []) else {
3333
return super.visit(node)
3434
}
3535

@@ -42,7 +42,7 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
4242
}
4343
let formattedExp = replaceTrivia(
4444
on: rewrittenCalledExpr,
45-
token: rewrittenCalledExpr.lastToken,
45+
token: rewrittenCalledExpr.lastToken(viewMode: .sourceAccurate),
4646
trailingTrivia: .spaces(1))
4747
let formattedClosure = visit(trailingClosure).as(ClosureExprSyntax.self)
4848
let result = node.with(\.leftParen, nil).with(\.rightParen, nil).with(\.calledExpression, formattedExp)

Sources/SwiftFormatRules/NoParensAroundConditions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public final class NoParensAroundConditions: SyntaxFormatRule {
5252
}
5353
return replaceTrivia(
5454
on: visitedExpr,
55-
token: visitedExpr.lastToken,
55+
token: visitedExpr.lastToken(viewMode: .sourceAccurate),
5656
leadingTrivia: visitedTuple.leftParen.leadingTrivia,
5757
trailingTrivia: visitedTuple.rightParen.trailingTrivia
5858
)

Sources/SwiftFormatRules/OneVariableDeclarationPerLine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private struct VariableDeclSplitter<Node: SyntaxProtocol> {
163163
// lines because the pretty printer will re-indent them correctly; we just
164164
// need to ensure that a newline is inserted before new decls.
165165
varDecl = replaceTrivia(
166-
on: varDecl, token: varDecl.firstToken, leadingTrivia: .newlines(1))
166+
on: varDecl, token: varDecl.firstToken(viewMode: .sourceAccurate), leadingTrivia: .newlines(1))
167167
fixedUpTrivia = true
168168
}
169169

Sources/SwiftFormatRules/OrderedImports.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ fileprivate func convertToCodeBlockItems(lines: [Line]) -> [CodeBlockItemSyntax]
362362
output.append(
363363
replaceTrivia(
364364
on: codeBlockItem,
365-
token: codeBlockItem.firstToken,
365+
token: codeBlockItem.firstToken(viewMode: .sourceAccurate),
366366
leadingTrivia: Trivia(pieces: triviaBuffer)
367367
)
368368
)
@@ -509,17 +509,17 @@ fileprivate class Line {
509509
guard let syntaxNode = syntaxNode else { return nil }
510510
switch syntaxNode {
511511
case .importCodeBlock(let codeBlock, _):
512-
return codeBlock.firstToken
512+
return codeBlock.firstToken(viewMode: .sourceAccurate)
513513
case .nonImportCodeBlocks(let codeBlocks):
514-
return codeBlocks.first?.firstToken
514+
return codeBlocks.first?.firstToken(viewMode: .sourceAccurate)
515515
}
516516
}
517517

518518
/// Returns a `LineType` the represents the type of import from the given import decl.
519519
private func importType(of importDecl: ImportDeclSyntax) -> LineType {
520-
if let attr = importDecl.attributes?.firstToken,
520+
if let attr = importDecl.attributes?.firstToken(viewMode: .sourceAccurate),
521521
attr.tokenKind == .atSign,
522-
attr.nextToken?.text == "testable"
522+
attr.nextToken(viewMode: .sourceAccurate)?.text == "testable"
523523
{
524524
return .testableImport
525525
}

Sources/SwiftFormatRules/ReturnVoidInsteadOfEmptyTuple.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
105105
return SimpleTypeIdentifierSyntax(
106106
name: TokenSyntax.identifier(
107107
"Void",
108-
leadingTrivia: node.firstToken?.leadingTrivia ?? [],
109-
trailingTrivia: node.lastToken?.trailingTrivia ?? []),
108+
leadingTrivia: node.firstToken(viewMode: .sourceAccurate)?.leadingTrivia ?? [],
109+
trailingTrivia: node.lastToken(viewMode: .sourceAccurate)?.trailingTrivia ?? []),
110110
genericArgumentClause: nil)
111111
}
112112
}

Sources/SwiftFormatRules/UseShorthandTypeNames.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
258258
// instead of discarding the comment.
259259
wrappedType =
260260
replaceTrivia(
261-
on: wrappedType, token: wrappedType.firstToken, leadingTrivia: leadingTrivia)
261+
on: wrappedType, token: wrappedType.firstToken(viewMode: .sourceAccurate), leadingTrivia: leadingTrivia)
262262
}
263263

264264
let optionalType = OptionalTypeSyntax(
@@ -346,7 +346,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
346346
// the comment.
347347
wrappedTypeExpr =
348348
replaceTrivia(
349-
on: wrappedTypeExpr, token: wrappedTypeExpr.firstToken, leadingTrivia: leadingTrivia)
349+
on: wrappedTypeExpr, token: wrappedTypeExpr.firstToken(viewMode: .sourceAccurate), leadingTrivia: leadingTrivia)
350350
}
351351

352352
return OptionalChainingExprSyntax(
@@ -410,7 +410,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
410410
case .optionalType(let optionalType):
411411
let result = makeOptionalTypeExpression(
412412
wrapping: optionalType.wrappedType,
413-
leadingTrivia: optionalType.firstToken?.leadingTrivia,
413+
leadingTrivia: optionalType.firstToken(viewMode: .sourceAccurate)?.leadingTrivia,
414414
questionMark: optionalType.questionMark)
415415
return ExprSyntax(result)
416416

@@ -493,8 +493,8 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
493493
-> (leadingTrivia: Trivia, trailingTrivia: Trivia)
494494
{
495495
return (
496-
leadingTrivia: node.firstToken?.leadingTrivia ?? [],
497-
trailingTrivia: node.lastToken?.trailingTrivia ?? []
496+
leadingTrivia: node.firstToken(viewMode: .sourceAccurate)?.leadingTrivia ?? [],
497+
trailingTrivia: node.lastToken(viewMode: .sourceAccurate)?.trailingTrivia ?? []
498498
)
499499
}
500500

Sources/SwiftFormatRules/UseTripleSlashForDocumentationComments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public final class UseTripleSlashForDocumentationComments: SyntaxFormatRule {
9898
return !hasFoundDocComment
9999
? decl : replaceTrivia(
100100
on: decl,
101-
token: decl.firstToken,
101+
token: decl.firstToken(viewMode: .sourceAccurate),
102102
leadingTrivia: Trivia(pieces: pieces.reversed())
103103
)
104104
}

Sources/SwiftFormatRules/UseWhereClausesInForLoops.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fileprivate func updateWithWhereCondition(
100100
statements: CodeBlockItemListSyntax
101101
) -> ForInStmtSyntax {
102102
// Construct a new `where` clause with the condition.
103-
let lastToken = node.sequenceExpr.lastToken
103+
let lastToken = node.sequenceExpr.lastToken(viewMode: .sourceAccurate)
104104
var whereLeadingTrivia = Trivia()
105105
if lastToken?.trailingTrivia.containsSpaces == false {
106106
whereLeadingTrivia = .spaces(1)

Sources/SwiftFormatRules/ValidateDocumentationComments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
124124
let needsThrowsDesc = throwsOrRethrowsKeyword?.tokenKind == .keyword(.throws)
125125

126126
if !needsThrowsDesc && throwsDesc != nil {
127-
diagnose(.removeThrowsComment(funcName: name), on: throwsOrRethrowsKeyword ?? node.firstToken)
127+
diagnose(.removeThrowsComment(funcName: name), on: throwsOrRethrowsKeyword ?? node.firstToken(viewMode: .sourceAccurate))
128128
} else if needsThrowsDesc && throwsDesc == nil {
129129
diagnose(.documentErrorsThrown(funcName: name), on: throwsOrRethrowsKeyword)
130130
}

0 commit comments

Comments
 (0)