Skip to content

Commit d40e749

Browse files
authored
Merge pull request swiftlang#223 from dabelknap/one-space-after-keywords
Demote OneSpaceAfterKeywords to lint rule
2 parents 64827f3 + 5d947ac commit d40e749

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

Diff for: Sources/SwiftFormat/PopulatePipeline.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,6 @@ func populate(_ pipeline: Pipeline) {
120120
EnumDeclSyntax.self
121121
)
122122

123-
pipeline.addFormatter(
124-
OneSpaceAfterKeywords.self,
125-
for:
126-
TokenSyntax.self
127-
)
128-
129123
pipeline.addFormatter(
130124
OneSpaceInsideBraces.self,
131125
for:
@@ -341,6 +335,12 @@ func populate(_ pipeline: Pipeline) {
341335
TypealiasDeclSyntax.self
342336
)
343337

338+
pipeline.addLinter(
339+
OneSpaceAfterKeywords.self,
340+
for:
341+
TokenSyntax.self
342+
)
343+
344344
pipeline.addLinter(
345345
OnlyOneTrailingClosureArgument.self,
346346
for:

Diff for: Sources/SwiftFormatRules/OneSpaceAfterKeywords.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ import SwiftSyntax
1919
/// Lint: If a keyword appears before a token on the same line without a space between them, a lint
2020
/// error is raised.
2121
///
22-
/// Format: A single space will be inserted between keywords and other same-line tokens.
23-
///
2422
/// - SeeAlso: https://google.github.io/swift#horizontal-whitespace
25-
public final class OneSpaceAfterKeywords: SyntaxFormatRule {
26-
public override func visit(_ token: TokenSyntax) -> Syntax {
27-
guard let nextToken = token.nextToken else { return token }
23+
public final class OneSpaceAfterKeywords: SyntaxLintRule {
24+
public override func visit(_ token: TokenSyntax) {
25+
guard let nextToken = token.nextToken else { return }
2826

2927
// Keywords own their trailing spaces, so ensure it only has 1, if there's
3028
// another token on the same line.
@@ -33,11 +31,10 @@ public final class OneSpaceAfterKeywords: SyntaxFormatRule {
3331
!nextToken.leadingTrivia.containsNewlines {
3432
let numSpaces = token.trailingTrivia.numberOfSpaces
3533
if numSpaces > 1 {
36-
diagnose(.removeSpacesAfterKeyword(numSpaces - 1, token.description), on: token)
37-
return token.withOneTrailingSpace()
34+
diagnose(
35+
.removeSpacesAfterKeyword(numSpaces - 1, token.withoutTrivia().description), on: token)
3836
}
3937
}
40-
return token
4138
}
4239
}
4340

Diff for: Tests/SwiftFormatRulesTests/OneSpaceAfterKeywordsTests.swift

+33-24
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,39 @@ import XCTest
55

66
public class OneSpaceAfterKeywordsTests: DiagnosingTestCase {
77
public func testInvalidOneSpaceAfterKeywordsTests() {
8-
XCTAssertFormatting(
9-
OneSpaceAfterKeywords.self,
10-
input: """
11-
let v1 = "Test"
12-
var values: Values
13-
func isIdentifier(_ tokKind: TokenKind) -> Bool {
14-
guard let first = v1.first else { return false }
15-
if case .identifier(_) = tokKind {
16-
return true
17-
}
18-
return false
19-
}
20-
""",
21-
expected: """
22-
let v1 = "Test"
23-
var values: Values
24-
func isIdentifier(_ tokKind: TokenKind) -> Bool {
25-
guard let first = v1.first else { return false }
26-
if case .identifier(_) = tokKind {
27-
return true
28-
}
29-
return false
30-
}
31-
""")
8+
let input =
9+
"""
10+
let v1 = "Test"
11+
var values: Values
12+
func isIdentifier(_ tokKind: TokenKind) -> Bool {
13+
guard let first = v1.first else { return false }
14+
if case .identifier(_) = tokKind {
15+
return true
16+
}
17+
return false
18+
}
19+
"""
20+
21+
performLint(OneSpaceAfterKeywords.self, input: input)
22+
23+
// let v1 = "Test"
24+
XCTAssertDiagnosed(.removeSpacesAfterKeyword(2, "let"))
25+
26+
// var values: Values
27+
XCTAssertDiagnosed(.removeSpacesAfterKeyword(12, "var"))
28+
29+
// func isIdentifier(_ tokKind: TokenKind) -> Bool {
30+
XCTAssertDiagnosed(.removeSpacesAfterKeyword(6, "_"))
31+
32+
// guard let first = v1.first else { return false }
33+
XCTAssertDiagnosed(.removeSpacesAfterKeyword(4, "guard"))
34+
XCTAssertDiagnosed(.removeSpacesAfterKeyword(4, "let"))
35+
36+
// return true
37+
XCTAssertDiagnosed(.removeSpacesAfterKeyword(14, "return"))
38+
39+
// return false
40+
XCTAssertDiagnosed(.removeSpacesAfterKeyword(1, "return"))
3241
}
3342

3443
#if !os(macOS)

0 commit comments

Comments
 (0)