Skip to content

Commit b95296c

Browse files
LaurenWhiteallevato
authored andcommitted
Implement only one trailing closure argument (swiftlang#85)
1 parent 79d4492 commit b95296c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,15 @@ import SwiftSyntax
1010
/// - SeeAlso: https://google.github.io/swift#trailing-closures
1111
public final class OnlyOneTrailingClosureArgument: SyntaxLintRule {
1212

13+
public override func visit(_ node: FunctionCallExprSyntax) {
14+
guard (node.argumentList.contains { $0.expression is ClosureExprSyntax }) else { return }
15+
guard node.trailingClosure != nil else { return }
16+
diagnose(.removeTrailingClosure, on: node)
17+
}
18+
}
19+
20+
extension Diagnostic.Message {
21+
static let removeTrailingClosure =
22+
Diagnostic.Message(.warning,
23+
"function call shouldn't have both closure arguments and a trailing closure")
1324
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Foundation
2+
import SwiftSyntax
3+
import XCTest
4+
5+
@testable import Rules
6+
7+
public class OnlyOneTrailingClosureArgumentTests: DiagnosingTestCase {
8+
public func testInvalidTrailingClosureCall() {
9+
let input =
10+
"""
11+
callWithBoth(someClosure: {}) {
12+
// ...
13+
}
14+
callWithClosure(someClosure: {})
15+
callWithTrailingClosure {
16+
// ...
17+
}
18+
"""
19+
performLint(OnlyOneTrailingClosureArgument.self, input: input)
20+
XCTAssertDiagnosed(.removeTrailingClosure)
21+
XCTAssertNotDiagnosed(.removeTrailingClosure)
22+
XCTAssertNotDiagnosed(.removeTrailingClosure)
23+
}
24+
25+
#if !os(macOS)
26+
static let allTests = [
27+
OnlyOneTrailingClosureArgumentTests.testInvalidTrailingClosureCall,
28+
]
29+
#endif
30+
}

0 commit comments

Comments
 (0)