File tree 2 files changed +59
-1
lines changed
2 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,24 @@ import SwiftSyntax
10
10
/// Format: Empty parentheses in function calls with trailing closures will be removed.
11
11
///
12
12
/// - SeeAlso: https://google.github.io/swift#trailing-closures
13
- public final class NoEmptyTrailingClosureParentheses : SyntaxLintRule {
13
+ public final class NoEmptyTrailingClosureParentheses : SyntaxFormatRule {
14
14
15
+ public override func visit( _ node: FunctionCallExprSyntax ) -> ExprSyntax {
16
+ guard node. argumentList. count == 0 else { return node }
17
+
18
+ guard let name = node. calledExpression. firstToken? . withoutTrivia ( ) else { return node }
19
+ diagnose ( . removeEmptyTrailingParentheses( name: " \( name) " ) , on: node)
20
+
21
+ let formattedExp = replaceTrivia ( on: node. calledExpression,
22
+ token: node. calledExpression. lastToken,
23
+ trailingTrivia: . spaces( 1 ) ) as! ExprSyntax
24
+ return node. withLeftParen ( nil ) . withRightParen ( nil ) . withCalledExpression ( formattedExp)
25
+ }
26
+ }
27
+
28
+
29
+ extension Diagnostic . Message {
30
+ static func removeEmptyTrailingParentheses( name: String ) -> Diagnostic . Message {
31
+ return . init( . warning, " remove '()' after \( name) " )
32
+ }
15
33
}
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 NoEmptyTrailingClosureParenthesesTests : DiagnosingTestCase {
8
+ public func testInvalidEmptyParenTrailingClosure( ) {
9
+ XCTAssertFormatting (
10
+ NoEmptyTrailingClosureParentheses . self,
11
+ input: """
12
+ func greetEnthusiastically(_ nameProvider: () -> String) {
13
+ // ...
14
+ }
15
+ func greetApathetically(_ nameProvider: () -> String) {
16
+ // ...
17
+ }
18
+ greetEnthusiastically() { " John " }
19
+ greetApathetically { " not John " }
20
+ """ ,
21
+ expected: """
22
+ func greetEnthusiastically(_ nameProvider: () -> String) {
23
+ // ...
24
+ }
25
+ func greetApathetically(_ nameProvider: () -> String) {
26
+ // ...
27
+ }
28
+ greetEnthusiastically { " John " }
29
+ greetApathetically { " not John " }
30
+ """ )
31
+ XCTAssertDiagnosed ( . removeEmptyTrailingParentheses( name: " greetEnthusiastically " ) )
32
+ }
33
+
34
+ #if !os(macOS)
35
+ static let allTests = [
36
+ NoEmptyTrailingClosureParenthesesTests . testInvalidEmptyParenTrailingClosure,
37
+ ]
38
+ #endif
39
+
40
+ }
You can’t perform that action at this time.
0 commit comments