Skip to content

Commit 58d3d99

Browse files
LaurenWhiteallevato
authored andcommitted
Implement NoEmptyTrailingClosureParentheses (swiftlang#83)
1 parent 8c12a94 commit 58d3d99

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

Sources/Rules/NoEmptyTrailingClosureParentheses.swift

+19-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ import SwiftSyntax
1010
/// Format: Empty parentheses in function calls with trailing closures will be removed.
1111
///
1212
/// - SeeAlso: https://google.github.io/swift#trailing-closures
13-
public final class NoEmptyTrailingClosureParentheses: SyntaxLintRule {
13+
public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {
1414

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+
}
1533
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)