Skip to content

Commit ee564cc

Browse files
authored
Merge pull request swiftlang#221 from dabelknap/close-brace-whitespace
Demote CloseBraceWhitespace to a lint rule
2 parents 9560efa + 6e40b7d commit ee564cc

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
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-
CloseBraceWhitespace.self,
36-
for:
37-
TokenSyntax.self
38-
)
39-
4034
pipeline.addFormatter(
4135
CollectionLiteralWhitespace.self,
4236
for:
@@ -280,6 +274,12 @@ func populate(_ pipeline: Pipeline) {
280274
SwitchStmtSyntax.self
281275
)
282276

277+
pipeline.addLinter(
278+
CloseBraceWhitespace.self,
279+
for:
280+
TokenSyntax.self
281+
)
282+
283283
pipeline.addLinter(
284284
CommentWhitespace.self,
285285
for:

Diff for: Sources/SwiftFormatRules/CloseBraceWhitespace.swift

+5-9
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@ import SwiftSyntax
2020
/// Lint: If a close brace does not have a line break before it, except as covered by One Statement
2121
/// Per Line, a lint error will be raised.
2222
///
23-
/// Format: Line breaks will be inserted for all non-conforming close braces.
24-
///
2523
/// - SeeAlso: https://google.github.io/swift#braces
26-
public final class CloseBraceWhitespace: SyntaxFormatRule {
27-
public override func visit(_ token: TokenSyntax) -> Syntax {
28-
guard token.tokenKind == .rightBrace else { return token }
29-
if isInAllowedSingleLineContainer(token) { return token }
30-
if token.leadingTrivia.containsNewlines { return token }
24+
public final class CloseBraceWhitespace: SyntaxLintRule {
25+
public override func visit(_ token: TokenSyntax) {
26+
guard token.tokenKind == .rightBrace else { return }
27+
if isInAllowedSingleLineContainer(token) { return }
28+
if token.leadingTrivia.containsNewlines { return }
3129

3230
diagnose(.lineBreakRequiredBeforeCloseBrace, on: token)
33-
return token.withOneLeadingNewline()
3431
}
3532
}
3633

@@ -57,4 +54,3 @@ extension Diagnostic.Message {
5754
static let lineBreakRequiredBeforeCloseBrace =
5855
Diagnostic.Message(.warning, "insert a newline before this '}'")
5956
}
60-

Diff for: Tests/SwiftFormatRulesTests/CloseBraceWhitespaceTests.swift

+12-21
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,23 @@ import SwiftSyntax
66

77
public class CloseBraceWhitespaceTests: DiagnosingTestCase {
88
public func testInvalidCloseBraceWhitespace() {
9-
XCTAssertFormatting(
10-
CloseBraceWhitespace.self,
11-
input: """
12-
func a()
13-
{ print("hello")
14-
print("goodbye")}
15-
func b(){
16-
}
17-
func c() {}
18-
""",
19-
expected: """
20-
func a()
21-
{ print("hello")
22-
print("goodbye")
23-
}
24-
func b(){
25-
}
26-
func c() {}
27-
""")
9+
let input =
10+
"""
11+
func a()
12+
{ print("hello")
13+
print("goodbye")}
14+
func b(){
15+
}
16+
func c() {}
17+
"""
18+
19+
performLint(CloseBraceWhitespace.self, input: input)
20+
XCTAssertDiagnosed(.lineBreakRequiredBeforeCloseBrace)
2821
}
2922

3023
#if !os(macOS)
3124
static let allTests = [
3225
CloseBraceWhitespaceTests.testInvalidCloseBraceWhitespace,
3326
]
3427
#endif
35-
3628
}
37-

0 commit comments

Comments
 (0)