Skip to content

Commit

Permalink
Merge pull request swiftlang#220 from dabelknap/colon-whitespace
Browse files Browse the repository at this point in the history
Demote ColonWhitespace to a lint rule
  • Loading branch information
dabelknap authored Feb 13, 2019
2 parents 8f7ec44 + ecf1aa7 commit 2514684
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
12 changes: 6 additions & 6 deletions Sources/SwiftFormat/PopulatePipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ func populate(_ pipeline: Pipeline) {
TokenSyntax.self
)

pipeline.addFormatter(
ColonWhitespace.self,
for:
TokenSyntax.self
)

pipeline.addFormatter(
DoNotUseSemicolons.self,
for:
Expand Down Expand Up @@ -274,6 +268,12 @@ func populate(_ pipeline: Pipeline) {
TokenSyntax.self
)

pipeline.addLinter(
ColonWhitespace.self,
for:
TokenSyntax.self
)

pipeline.addLinter(
CommaWhitespace.self,
for:
Expand Down
15 changes: 6 additions & 9 deletions Sources/SwiftFormatRules/ColonWhitespace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ import SwiftSyntax
/// Lint: If an invalid number of spaces appear before or after a colon, a lint error is
/// raised.
///
/// Format: All colons will have no spaces before, and a single space after.
///
/// - SeeAlso: https://google.github.io/swift#horizontal-whitespace
public final class ColonWhitespace: SyntaxFormatRule {
public override func visit(_ token: TokenSyntax) -> Syntax {
guard let next = token.nextToken else { return token }
public final class ColonWhitespace: SyntaxLintRule {
public override func visit(_ token: TokenSyntax) {
guard let next = token.nextToken else { return }

if token.tokenKind == .colon,
token.containingExprStmtOrDecl is DictionaryExprSyntax,
next.tokenKind == .rightSquareBracket,
token.trailingTrivia.numberOfSpaces > 0 {
diagnose(.noSpacesAfterColon, on: token)
return token.withoutTrailingTrivia()
return
}

/// Colons own their trailing spaces, so ensure it only has 1 if there's
Expand All @@ -48,16 +46,15 @@ public final class ColonWhitespace: SyntaxFormatRule {
if numSpaces == 0 {
diagnose(.addSpaceAfterColon, on: token)
}
return token.withOneTrailingSpace()
return
}

/// Otherwise, colon-adjacent tokens should have 0 spaces after.
if next.tokenKind == .colon, token.trailingTrivia.containsSpaces,
!(next.containingExprStmtOrDecl is TernaryExprSyntax) {
diagnose(.noSpacesBeforeColon, on: next)
return token.withTrailingTrivia(token.trailingTrivia.withoutSpaces())
return
}
return token
}
}

Expand Down
46 changes: 28 additions & 18 deletions Tests/SwiftFormatRulesTests/ColonWhitespaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ import XCTest

public class ColonWhitespaceTests: DiagnosingTestCase {
public func testInvalidColonWhitespace() {
XCTAssertFormatting(
ColonWhitespace.self,
input: """
let v1: Int = 0
let v2 : Int = 1
let v3 :Int = 1
let v4 \t: \t Int = 1
let v5: [Int: String] = [: ]
let v6: [Int: String] = [23: "twenty three"]
""",
expected: """
let v1: Int = 0
let v2: Int = 1
let v3: Int = 1
let v4: Int = 1
let v5: [Int: String] = [:]
let v6: [Int: String] = [23: "twenty three"]
""")
let input =
"""
let v1: Int = 0
let v2 : Int = 1
let v3 :Int = 1
let v4 \t: \t Int = 1
let v5: [Int: String] = [: ]
let v6: [Int: String] = [23: "twenty three"]
"""

performLint(ColonWhitespace.self, input: input)

// let v2 : Int = 1
XCTAssertDiagnosed(.noSpacesBeforeColon)

// let v3 :Int = 1
XCTAssertDiagnosed(.noSpacesBeforeColon)
XCTAssertDiagnosed(.addSpaceAfterColon)

// let v4 \t: \t Int = 1
XCTAssertDiagnosed(.noSpacesBeforeColon)
XCTAssertDiagnosed(.removeSpacesAfterColon(count: 6))

// let v5: [Int: String] = [: ]
XCTAssertDiagnosed(.noSpacesAfterColon)

// let v6: [Int: String] = [23: "twenty three"]
XCTAssertDiagnosed(.removeSpacesAfterColon(count: 1))
}

#if !os(macOS)
Expand Down

0 comments on commit 2514684

Please sign in to comment.