Skip to content

Commit 8fb3713

Browse files
committed
Add a convenience init to ClosureCaptureSyntax
**Summary** This adds a convenience init to ClosureCaptureSyntax that adds equal token if the provided `name` is not nil. It's supposed to be used by developers, so it omits the unexpected tokens. Related: #1984
1 parent 29c1564 commit 8fb3713

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Sources/SwiftSyntax/Convenience.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
extension ClosureCaptureSyntax {
14+
15+
/// Creates a `ClosureCaptureSyntax` with a `name`, and automatically adds an `equal` token to it since the name is non-optional.
16+
///
17+
/// - SeeAlso: ``ClosureCaptureSyntax/init(leadingTrivia:_:specifier:_:name:_:equal:_:expression:_:trailingComma:_:trailingTrivia:)``.
18+
///
19+
public init(
20+
leadingTrivia: Trivia? = nil,
21+
specifier: ClosureCaptureSpecifierSyntax? = nil,
22+
name: TokenSyntax,
23+
equal: TokenSyntax = TokenSyntax.equalToken(),
24+
expression: some ExprSyntaxProtocol,
25+
trailingComma: TokenSyntax? = nil,
26+
trailingTrivia: Trivia? = nil
27+
) {
28+
self.init(
29+
leadingTrivia: leadingTrivia,
30+
specifier: specifier,
31+
name: name as TokenSyntax?,
32+
equal: equal,
33+
expression: expression,
34+
trailingComma: trailingComma,
35+
trailingTrivia: trailingTrivia
36+
)
37+
}
38+
}
39+
1340
extension EnumCaseParameterSyntax {
1441

1542
/// Creates an `EnumCaseParameterSyntax` with a `firstName`, and automatically adds a `colon` to it.

Tests/SwiftSyntaxTest/SyntaxTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,12 @@ public class SyntaxTests: XCTestCase {
133133
let node = EnumCaseParameterSyntax(firstName: "label", type: TypeSyntax("MyType"))
134134
XCTAssertEqual(node.formatted().description, "label: MyType")
135135
}
136+
137+
public func testClosureCaptureSyntaxConvenienceInitWithEqual() {
138+
let noNameClosureCapture = ClosureCaptureSyntax(expression: ExprSyntax("123"))
139+
XCTAssertEqual(noNameClosureCapture.formatted().description, "123")
140+
141+
let node = ClosureCaptureSyntax(name: "test", expression: ExprSyntax("123"))
142+
XCTAssertEqual(node.formatted().description, "test = 123")
143+
}
136144
}

0 commit comments

Comments
 (0)