File tree 2 files changed +51
-0
lines changed
2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -11,5 +11,21 @@ import SwiftSyntax
11
11
///
12
12
/// - SeeAlso: https://google.github.io/swift#enum-cases
13
13
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
+ }
14
26
27
+ extension Diagnostic . Message {
28
+ static func removeEmptyParentheses( name: String ) -> Diagnostic . Message {
29
+ return . init( . warning, " Remove '()' after \( name) " )
30
+ }
15
31
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments