Skip to content

Removed warnings #510

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
Apr 12, 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
2 changes: 1 addition & 1 deletion Sources/SwiftFormatCore/LegacyTriviaBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private final class LegacyTriviaBehaviorRewriter: SyntaxRewriter {
token = token.with(\.leadingTrivia, pendingLeadingTrivia + token.leadingTrivia)
self.pendingLeadingTrivia = nil
}
if token.nextToken != nil,
if token.nextToken(viewMode: .sourceAccurate) != nil,
let firstIndexToMove = token.trailingTrivia.firstIndex(where: shouldTriviaPieceBeMoved)
{
pendingLeadingTrivia = Trivia(pieces: Array(token.trailingTrivia[firstIndexToMove...]))
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftFormatCore/RuleMask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
// MARK: - Syntax Visitation Methods

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

override func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
guard let firstToken = node.firstToken else {
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
return .visitChildren
}
return appendRuleStatusDirectives(from: firstToken, of: Syntax(node))
}

override func visit(_ node: MemberDeclListItemSyntax) -> SyntaxVisitorContinueKind {
guard let firstToken = node.firstToken else {
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
return .visitChildren
}
return appendRuleStatusDirectives(from: firstToken, of: Syntax(node))
Expand All @@ -183,7 +183,7 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
private func appendRuleStatusDirectives(from token: TokenSyntax, of node: Syntax)
-> SyntaxVisitorContinueKind
{
let isFirstInFile = token.previousToken == nil
let isFirstInFile = token.previousToken(viewMode: .sourceAccurate) == nil
let matches = loneLineComments(in: token.leadingTrivia, isFirstToken: isFirstInFile)
.compactMap(ruleStatusDirectiveMatch)
let sourceRange = node.sourceRange(converter: sourceLocationConverter)
Expand Down
406 changes: 203 additions & 203 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Sources/SwiftFormatRules/AddModifierRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,18 @@ fileprivate final class AddModifierRewriter: SyntaxRewriter {
for modifiersProvider: (NodeType) -> ModifierListSyntax?
) -> NodeType {
guard let modifier = modifiersProvider(node)?.firstAndOnly,
let movingLeadingTrivia = modifier.nextToken?.leadingTrivia
let movingLeadingTrivia = modifier.nextToken(viewMode: .sourceAccurate)?.leadingTrivia
else {
// Otherwise, there's no trivia that needs to be relocated so the node is fine.
return node
}
let nodeWithTrivia = replaceTrivia(
on: node,
token: modifier.firstToken,
token: modifier.firstToken(viewMode: .sourceAccurate),
leadingTrivia: movingLeadingTrivia)
return replaceTrivia(
on: nodeWithTrivia,
token: modifiersProvider(nodeWithTrivia)?.first?.nextToken,
token: modifiersProvider(nodeWithTrivia)?.first?.nextToken(viewMode: .sourceAccurate),
leadingTrivia: [])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SwiftSyntax
extension DeclSyntaxProtocol {
/// Searches through the leading trivia of this decl for a documentation comment.
var docComment: String? {
guard let tok = firstToken else { return nil }
guard let tok = firstToken(viewMode: .sourceAccurate) else { return nil }
var comment = [String]()

// We need to skip trivia until we see the first comment. This trivia will include all the
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/DoNotUseSemicolons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class DoNotUseSemicolons: SyntaxFormatRule {
defer { newItems[idx] = newItem }

// Check if the leading trivia for this statement needs a new line.
if previousHadSemicolon, let firstToken = newItem.firstToken,
if previousHadSemicolon, let firstToken = newItem.firstToken(viewMode: .sourceAccurate),
!firstToken.leadingTrivia.containsNewlines
{
let leadingTrivia = .newlines(1) + firstToken.leadingTrivia
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftFormatRules/FullyIndirectEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
// If the `indirect` keyword being added would be the first token in the decl, we need to move
// the leading trivia from the `enum` keyword to the new modifier to preserve the existing
// line breaks/comments/indentation.
let firstTok = node.firstToken!
let firstTok = node.firstToken(viewMode: .sourceAccurate)!
let leadingTrivia: Trivia
let newEnumDecl: EnumDeclSyntax

if firstTok.tokenKind == .keyword(.enum) {
leadingTrivia = firstTok.leadingTrivia
newEnumDecl = replaceTrivia(
on: node, token: node.firstToken, leadingTrivia: [])
on: node, token: node.firstToken(viewMode: .sourceAccurate), leadingTrivia: [])
} else {
leadingTrivia = []
newEnumDecl = node
Expand Down Expand Up @@ -97,7 +97,7 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
) -> EnumCaseDeclSyntax {
if let modifiers = unformattedCase.modifiers, let first = modifiers.first {
return replaceTrivia(
on: unformattedCase, token: first.firstToken, leadingTrivia: leadingTrivia
on: unformattedCase, token: first.firstToken(viewMode: .sourceAccurate), leadingTrivia: leadingTrivia
)
} else {
return replaceTrivia(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ extension ModifierListSyntax {

if index == 0 {
guard formatTrivia else { return inserting(modifier, at: index) }
guard let firstMod = first, let firstTok = firstMod.firstToken else {
guard let firstMod = first, let firstTok = firstMod.firstToken(viewMode: .sourceAccurate) else {
return inserting(modifier, at: index)
}
let formattedMod = replaceTrivia(
on: modifier,
token: modifier.firstToken,
token: modifier.firstToken(viewMode: .sourceAccurate),
leadingTrivia: firstTok.leadingTrivia)
newModifiers[0] = replaceTrivia(
on: firstMod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {

// Check for any comments that are inline on the fallthrough statement. Inline comments are
// always stored in the next token's leading trivia.
if let nextLeadingTrivia = onlyStatement.nextToken?.leadingTrivia,
if let nextLeadingTrivia = onlyStatement.nextToken(viewMode: .sourceAccurate)?.leadingTrivia,
nextLeadingTrivia.prefix(while: { !$0.isNewline }).contains(where: { $0.isComment })
{
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
{
return super.visit(node)
}
guard let name = node.calledExpression.lastToken?.with(\.leadingTrivia, []).with(\.trailingTrivia, []) else {
guard let name = node.calledExpression.lastToken(viewMode: .sourceAccurate)?.with(\.leadingTrivia, []).with(\.trailingTrivia, []) else {
return super.visit(node)
}

Expand All @@ -42,7 +42,7 @@ public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
}
let formattedExp = replaceTrivia(
on: rewrittenCalledExpr,
token: rewrittenCalledExpr.lastToken,
token: rewrittenCalledExpr.lastToken(viewMode: .sourceAccurate),
trailingTrivia: .spaces(1))
let formattedClosure = visit(trailingClosure).as(ClosureExprSyntax.self)
let result = node.with(\.leftParen, nil).with(\.rightParen, nil).with(\.calledExpression, formattedExp)
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/NoParensAroundConditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class NoParensAroundConditions: SyntaxFormatRule {
}
return replaceTrivia(
on: visitedExpr,
token: visitedExpr.lastToken,
token: visitedExpr.lastToken(viewMode: .sourceAccurate),
leadingTrivia: visitedTuple.leftParen.leadingTrivia,
trailingTrivia: visitedTuple.rightParen.trailingTrivia
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private struct VariableDeclSplitter<Node: SyntaxProtocol> {
// lines because the pretty printer will re-indent them correctly; we just
// need to ensure that a newline is inserted before new decls.
varDecl = replaceTrivia(
on: varDecl, token: varDecl.firstToken, leadingTrivia: .newlines(1))
on: varDecl, token: varDecl.firstToken(viewMode: .sourceAccurate), leadingTrivia: .newlines(1))
fixedUpTrivia = true
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftFormatRules/OrderedImports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ fileprivate func convertToCodeBlockItems(lines: [Line]) -> [CodeBlockItemSyntax]
output.append(
replaceTrivia(
on: codeBlockItem,
token: codeBlockItem.firstToken,
token: codeBlockItem.firstToken(viewMode: .sourceAccurate),
leadingTrivia: Trivia(pieces: triviaBuffer)
)
)
Expand Down Expand Up @@ -509,17 +509,17 @@ fileprivate class Line {
guard let syntaxNode = syntaxNode else { return nil }
switch syntaxNode {
case .importCodeBlock(let codeBlock, _):
return codeBlock.firstToken
return codeBlock.firstToken(viewMode: .sourceAccurate)
case .nonImportCodeBlocks(let codeBlocks):
return codeBlocks.first?.firstToken
return codeBlocks.first?.firstToken(viewMode: .sourceAccurate)
}
}

/// Returns a `LineType` the represents the type of import from the given import decl.
private func importType(of importDecl: ImportDeclSyntax) -> LineType {
if let attr = importDecl.attributes?.firstToken,
if let attr = importDecl.attributes?.firstToken(viewMode: .sourceAccurate),
attr.tokenKind == .atSign,
attr.nextToken?.text == "testable"
attr.nextToken(viewMode: .sourceAccurate)?.text == "testable"
{
return .testableImport
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
return SimpleTypeIdentifierSyntax(
name: TokenSyntax.identifier(
"Void",
leadingTrivia: node.firstToken?.leadingTrivia ?? [],
trailingTrivia: node.lastToken?.trailingTrivia ?? []),
leadingTrivia: node.firstToken(viewMode: .sourceAccurate)?.leadingTrivia ?? [],
trailingTrivia: node.lastToken(viewMode: .sourceAccurate)?.trailingTrivia ?? []),
genericArgumentClause: nil)
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftFormatRules/UseShorthandTypeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
// instead of discarding the comment.
wrappedType =
replaceTrivia(
on: wrappedType, token: wrappedType.firstToken, leadingTrivia: leadingTrivia)
on: wrappedType, token: wrappedType.firstToken(viewMode: .sourceAccurate), leadingTrivia: leadingTrivia)
}

let optionalType = OptionalTypeSyntax(
Expand Down Expand Up @@ -346,7 +346,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
// the comment.
wrappedTypeExpr =
replaceTrivia(
on: wrappedTypeExpr, token: wrappedTypeExpr.firstToken, leadingTrivia: leadingTrivia)
on: wrappedTypeExpr, token: wrappedTypeExpr.firstToken(viewMode: .sourceAccurate), leadingTrivia: leadingTrivia)
}

return OptionalChainingExprSyntax(
Expand Down Expand Up @@ -410,7 +410,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
case .optionalType(let optionalType):
let result = makeOptionalTypeExpression(
wrapping: optionalType.wrappedType,
leadingTrivia: optionalType.firstToken?.leadingTrivia,
leadingTrivia: optionalType.firstToken(viewMode: .sourceAccurate)?.leadingTrivia,
questionMark: optionalType.questionMark)
return ExprSyntax(result)

Expand Down Expand Up @@ -493,8 +493,8 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
-> (leadingTrivia: Trivia, trailingTrivia: Trivia)
{
return (
leadingTrivia: node.firstToken?.leadingTrivia ?? [],
trailingTrivia: node.lastToken?.trailingTrivia ?? []
leadingTrivia: node.firstToken(viewMode: .sourceAccurate)?.leadingTrivia ?? [],
trailingTrivia: node.lastToken(viewMode: .sourceAccurate)?.trailingTrivia ?? []
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public final class UseTripleSlashForDocumentationComments: SyntaxFormatRule {
return !hasFoundDocComment
? decl : replaceTrivia(
on: decl,
token: decl.firstToken,
token: decl.firstToken(viewMode: .sourceAccurate),
leadingTrivia: Trivia(pieces: pieces.reversed())
)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/UseWhereClausesInForLoops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fileprivate func updateWithWhereCondition(
statements: CodeBlockItemListSyntax
) -> ForInStmtSyntax {
// Construct a new `where` clause with the condition.
let lastToken = node.sequenceExpr.lastToken
let lastToken = node.sequenceExpr.lastToken(viewMode: .sourceAccurate)
var whereLeadingTrivia = Trivia()
if lastToken?.trailingTrivia.containsSpaces == false {
whereLeadingTrivia = .spaces(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
let needsThrowsDesc = throwsOrRethrowsKeyword?.tokenKind == .keyword(.throws)

if !needsThrowsDesc && throwsDesc != nil {
diagnose(.removeThrowsComment(funcName: name), on: throwsOrRethrowsKeyword ?? node.firstToken)
diagnose(.removeThrowsComment(funcName: name), on: throwsOrRethrowsKeyword ?? node.firstToken(viewMode: .sourceAccurate))
} else if needsThrowsDesc && throwsDesc == nil {
diagnose(.documentErrorsThrown(funcName: name), on: throwsOrRethrowsKeyword)
}
Expand Down