Skip to content

Commit 6a00dbe

Browse files
authored
Merge pull request #2127 from natikgadzhi/syntax/convenience/closure-capture
Add a convenience init to ClosureCaptureSyntax
2 parents 2504b3f + a9acb00 commit 6a00dbe

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Release Notes/510.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
## New APIs
44

55
- `SyntaxStringInterpolation.appendInterpolation(_: (some SyntaxProtocol)?)`
6-
- Descriptions: Allows optional syntax nodes to be used inside string interpolation of syntax nodes. If the node is `nil`, nothing will get added to the string interpolation.
6+
- Description: Allows optional syntax nodes to be used inside string interpolation of syntax nodes. If the node is `nil`, nothing will get added to the string interpolation.
77
- Pull Request: https://github.com/apple/swift-syntax/pull/2085
88
- `SyntaxCollection.index(at:)`
99
- Description: Returns the index of the n-th element in a `SyntaxCollection`. This computation is in O(n) and `SyntaxCollection` is not subscriptable by an integer.
1010
- Pull Request: https://github.com/apple/swift-syntax/pull/2014
11+
- Convenience initializer `ClosureCaptureSyntax.init()`
12+
- Description: Provides a convenience initializer for `ClosureCaptureSyntax` that takes a concrete `name` argument and automatically adds `equal = TokenSyntax.equalToken()` to it.
13+
- Issue: https://github.com/apple/swift-syntax/issues/1984
14+
- Pull Request: https://github.com/apple/swift-syntax/pull/2127
15+
- Convenience initializer `EnumCaseParameterSyntax.init()`
16+
- Description: Provides a convenience initializer for `EnumCaseParameterSyntax` that takes a concrete `firstName` value and adds `colon = TokenSyntax.colonToken()` automatically to it.
17+
- Issue: https://github.com/apple/swift-syntax/issues/1984
18+
- Pull Request: https://github.com/apple/swift-syntax/pull/2112
1119

1220
## API Behavior Changes
1321

Sources/SwiftSyntax/Convenience.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,36 @@
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

15-
/// Creates an `EnumCaseParameterSyntax` with a `firstName`, and automatically adds a `colon` to it.
42+
/// Creates an ``EnumCaseParameterSyntax`` with a `firstName`, and automatically adds a `colon` to it.
1643
///
1744
/// - SeeAlso: For more information on the arguments, see ``EnumCaseParameterSyntax/init(leadingTrivia:_:modifiers:_:firstName:_:secondName:_:colon:_:type:_:defaultArgument:_:trailingComma:_:trailingTrivia:)``
1845
///

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)