Skip to content

Commit 21ff218

Browse files
LaurenWhiteallevato
authored andcommitted
Implement NoEmptyAssociatedValues rule and tests. (swiftlang#61)
1 parent c3dd523 commit 21ff218

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tools/swift-format/Sources/Rules/NoEmptyAssociatedValues.swift

+16
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,21 @@ import SwiftSyntax
1111
///
1212
/// - SeeAlso: https://google.github.io/swift#enum-cases
1313
public final class NoEmptyAssociatedValues: SyntaxFormatRule {
14+
public override func visit(_ node: EnumCaseDeclSyntax) -> DeclSyntax {
15+
for element in node.elements {
16+
guard let associatedValue = element.associatedValue else { continue }
17+
if associatedValue.parameterList.count == 0 {
18+
diagnose(.removeEmptyParentheses(name: element.identifier.text), on: element)
19+
let newDecl = node.withElements(node.elements.replacing(childAt: element.indexInParent, with: element.withAssociatedValue(nil)))
20+
return newDecl
21+
}
22+
}
23+
return node
24+
}
25+
}
1426

27+
extension Diagnostic.Message {
28+
static func removeEmptyParentheses(name: String) -> Diagnostic.Message {
29+
return .init(.warning, "Remove '()' after \(name)")
30+
}
1531
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Foundation
2+
import XCTest
3+
import SwiftSyntax
4+
5+
@testable import Rules
6+
7+
public class NoEmptyAssociatedValuesTests: DiagnosingTestCase {
8+
func testEmptyAssociatedValue() {
9+
XCTAssertFormatting(NoEmptyAssociatedValues.self,
10+
input: """
11+
enum CompassPoint {
12+
case north
13+
private case east()
14+
case south(String)
15+
indirect case west
16+
case northeast()
17+
}
18+
""",
19+
expected: """
20+
enum CompassPoint {
21+
case north
22+
private case east
23+
case south(String)
24+
indirect case west
25+
case northeast
26+
}
27+
""")
28+
}
29+
30+
#if !os(macOS)
31+
static let allTests = [
32+
NoEmptyAssociatedValuesTests.testEmptyAssociatedValue,
33+
]
34+
#endif
35+
}

0 commit comments

Comments
 (0)