Skip to content

Commit 0d40542

Browse files
atamez31allevato
authored andcommitted
Initial implementation of the CommaWhitespace rule and tests (swiftlang#51)
1 parent a67d563 commit 0d40542

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

Diff for: Sources/Rules/CommaWhitespace.swift

+34
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,39 @@ import SwiftSyntax
1414
///
1515
/// - SeeAlso: https://google.github.io/swift#horizontal-whitespace
1616
public final class CommaWhitespace: SyntaxFormatRule {
17+
public override func visit(_ token: TokenSyntax) -> Syntax {
18+
guard let next = token.nextToken else { return token }
1719

20+
// Commas own their trailing spaces, so ensure it only has 1 if there's
21+
// another token on the same line.
22+
if token.tokenKind == .comma && !next.leadingTrivia.containsNewlines {
23+
let numSpaces = token.trailingTrivia.numberOfSpaces
24+
if numSpaces > 1 {
25+
diagnose(.removeSpacesAfterComma(count: numSpaces - 1), on: token)
26+
}
27+
else if numSpaces == 0 {
28+
diagnose(.addSpaceAfterComma, on: token)
29+
}
30+
return token.withOneTrailingSpace()
31+
}
32+
33+
// Otherwise, comma-adjacent tokens should have 0 spaces after.
34+
if next.tokenKind == .comma && token.trailingTrivia.containsSpaces {
35+
diagnose(.noSpacesBeforeComma, on: next)
36+
return token.withTrailingTrivia(token.trailingTrivia.withoutSpaces())
37+
}
38+
return token
39+
}
40+
}
41+
42+
extension Diagnostic.Message {
43+
static func removeSpacesAfterComma(count: Int) -> Diagnostic.Message {
44+
let ending = count == 1 ? "" : "s"
45+
return Diagnostic.Message(.warning, "remove \(count) space\(ending) after ','")
46+
}
47+
48+
static let addSpaceAfterComma =
49+
Diagnostic.Message(.warning, "add one space after ','")
50+
static let noSpacesBeforeComma =
51+
Diagnostic.Message(.warning, "remove spaces before ','")
1852
}

Diff for: Tests/SwiftFormatTests/CommaWhitespaceTests.swift

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import SwiftSyntax
2+
import XCTest
3+
4+
@testable import Rules
5+
6+
public class CommaWhitespaceTests: DiagnosingTestCase {
7+
public func testInvalidFuncCommaWhiteSpace() {
8+
XCTAssertFormatting(
9+
CommaWhitespace.self,
10+
input: """
11+
func testComma(paramA: Bool,paramB: Bool ,paramC: Bool , paramD: Bool) -> Bool {
12+
13+
if paramA,
14+
!paramB ,
15+
paramC ,
16+
paramD {
17+
return true
18+
}
19+
return false
20+
}
21+
""",
22+
expected: """
23+
func testComma(paramA: Bool, paramB: Bool, paramC: Bool, paramD: Bool) -> Bool {
24+
25+
if paramA,
26+
!paramB,
27+
paramC,
28+
paramD {
29+
return true
30+
}
31+
return false
32+
}
33+
""")
34+
}
35+
36+
public func testInvalidDeclCommaWhiteSpace() {
37+
XCTAssertFormatting(
38+
CommaWhitespace.self,
39+
input: """
40+
let numA = [1,2,3]
41+
let numB = [1 ,2 ,3]
42+
let numC = [1 , 2 , 3]
43+
""",
44+
expected: """
45+
let numA = [1, 2, 3]
46+
let numB = [1, 2, 3]
47+
let numC = [1, 2, 3]
48+
""")
49+
}
50+
51+
#if !os(macOS)
52+
static let allTests = [
53+
CommaWhitespaceTests.testInvalidFuncCommaWhiteSpace,
54+
CommaWhitespaceTests.testInvalidDeclCommaWhiteSpace,
55+
]
56+
#endif
57+
}

0 commit comments

Comments
 (0)