Skip to content

Commit 64827f3

Browse files
authored
Merge pull request swiftlang#222 from dabelknap/collection-literal-whitespace
Demote CollectionLiteralWhitespace to a lint rule
2 parents 2514684 + 1d22642 commit 64827f3

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

Diff for: Sources/SwiftFormat/PopulatePipeline.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ func populate(_ pipeline: Pipeline) {
3131
MemberDeclBlockSyntax.self
3232
)
3333

34-
pipeline.addFormatter(
35-
CollectionLiteralWhitespace.self,
36-
for:
37-
TokenSyntax.self
38-
)
39-
4034
pipeline.addFormatter(
4135
DoNotUseSemicolons.self,
4236
for:
@@ -268,6 +262,12 @@ func populate(_ pipeline: Pipeline) {
268262
TokenSyntax.self
269263
)
270264

265+
pipeline.addLinter(
266+
CollectionLiteralWhitespace.self,
267+
for:
268+
TokenSyntax.self
269+
)
270+
271271
pipeline.addLinter(
272272
ColonWhitespace.self,
273273
for:

Diff for: Sources/SwiftFormatRules/CollectionLiteralWhitespace.swift

+6-9
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,24 @@ import SwiftSyntax
2020
/// after the opening delimiter, or any spaces before the closing delimiter, a lint
2121
/// error is raised.
2222
///
23-
/// Format: Extraneous spaces at the beginning and end of collection literals will be removed.
24-
///
2523
/// - SeeAlso: https://google.github.io/swift#horizontal-whitespace
26-
public final class CollectionLiteralWhitespace: SyntaxFormatRule {
27-
public override func visit(_ token: TokenSyntax) -> Syntax {
24+
public final class CollectionLiteralWhitespace: SyntaxLintRule {
25+
public override func visit(_ token: TokenSyntax) {
2826
// Ensure we have an adjacent token on the same line
29-
guard let next = token.nextToken else { return token }
30-
if next.leadingTrivia.containsNewlines { return token }
27+
guard let next = token.nextToken else { return }
28+
if next.leadingTrivia.containsNewlines { return }
3129

3230
// If either this current token is a left delimiter, or the next token
3331
// is a right delimiter, then remove spaces from our trailing trivia.
3432
if token.tokenKind.isLeftBalancedDelimiter && token.trailingTrivia.containsSpaces {
3533
diagnose(.noSpacesAfter(token), on: token)
36-
return token.withTrailingTrivia(token.trailingTrivia.withoutSpaces())
34+
return
3735
}
3836

3937
if next.tokenKind.isRightBalancedDelimiter && token.trailingTrivia.containsSpaces {
4038
diagnose(.noSpacesBefore(next), on: next)
41-
return token.withTrailingTrivia(token.trailingTrivia.withoutSpaces())
39+
return
4240
}
43-
return token
4441
}
4542
}
4643

Diff for: Tests/SwiftFormatRulesTests/CollectionLiteralWhitespaceTests.swift

+42-23
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,53 @@ import SwiftSyntax
66

77
public class CollectionLiteralWhitespaceTests: DiagnosingTestCase {
88
public func testInvalidCollectionLiteralWhitespace() {
9-
XCTAssertFormatting(
10-
CollectionLiteralWhitespace.self,
11-
input: """
12-
func a( x: Int ) {
13-
print( [ x ] )
14-
if ( x == 0 ) {
15-
var arr = [ Int ]()
16-
arr.append(x )
17-
print( arr)
18-
}
19-
}
20-
""",
21-
expected: """
22-
func a(x: Int) {
23-
print([x])
24-
if (x == 0) {
25-
var arr = [Int]()
26-
arr.append(x)
27-
print(arr)
28-
}
29-
}
30-
""")
9+
let input =
10+
"""
11+
func a( x: Int ) {
12+
print( [ x ] )
13+
if ( x == 0 ) {
14+
var arr = [ Int ]()
15+
arr.append(x )
16+
print( arr)
17+
}
18+
}
19+
"""
20+
21+
performLint(CollectionLiteralWhitespace.self, input: input)
22+
23+
let leftParen = SyntaxFactory.makeLeftParenToken()
24+
let rightParen = SyntaxFactory.makeRightParenToken()
25+
let leftSquare = SyntaxFactory.makeLeftSquareBracketToken()
26+
let rightSquare = SyntaxFactory.makeRightSquareBracketToken()
27+
28+
// func a( x: Int ) {
29+
XCTAssertDiagnosed(.noSpacesAfter(leftParen))
30+
XCTAssertDiagnosed(.noSpacesBefore(rightParen))
31+
32+
// print( [ x ] )
33+
XCTAssertDiagnosed(.noSpacesAfter(leftParen))
34+
XCTAssertDiagnosed(.noSpacesAfter(leftSquare))
35+
XCTAssertDiagnosed(.noSpacesBefore(rightSquare))
36+
XCTAssertDiagnosed(.noSpacesBefore(rightParen))
37+
38+
// if ( x == 0 ) {
39+
XCTAssertDiagnosed(.noSpacesAfter(leftParen))
40+
XCTAssertDiagnosed(.noSpacesBefore(rightParen))
41+
42+
// var arr = [ Int ]()
43+
XCTAssertDiagnosed(.noSpacesAfter(leftSquare))
44+
XCTAssertDiagnosed(.noSpacesBefore(rightSquare))
45+
46+
// arr.append(x )
47+
XCTAssertDiagnosed(.noSpacesBefore(rightParen))
48+
49+
// print( arr)
50+
XCTAssertDiagnosed(.noSpacesAfter(leftParen))
3151
}
3252

3353
#if !os(macOS)
3454
static let allTests = [
3555
CollectionLiteralWhitespaceTests.testInvalidCollectionLiteralWhitespace,
3656
]
3757
#endif
38-
3958
}

0 commit comments

Comments
 (0)