Skip to content

Add a convenience init to ClosureCaptureSyntax #2127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Release Notes/510.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
## New APIs

- `SyntaxStringInterpolation.appendInterpolation(_: (some SyntaxProtocol)?)`
- 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.
- 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.
- Pull Request: https://github.com/apple/swift-syntax/pull/2085
- `SyntaxCollection.index(at:)`
- 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.
- Pull Request: https://github.com/apple/swift-syntax/pull/2014
- Convenience initializer `ClosureCaptureSyntax.init()`
- Description: Provides a convenience initializer for `ClosureCaptureSyntax` that takes a concrete `name` argument and automatically adds `equal = TokenSyntax.equalToken()` to it.
- Issue: https://github.com/apple/swift-syntax/issues/1984
- Pull Request: https://github.com/apple/swift-syntax/pull/2127
- Convenience initializer `EnumCaseParameterSyntax.init()`
- Description: Provides a convenience initializer for `EnumCaseParameterSyntax` that takes a concrete `firstName` value and adds `colon = TokenSyntax.colonToken()` automatically to it.
- Issue: https://github.com/apple/swift-syntax/issues/1984
- Pull Request: https://github.com/apple/swift-syntax/pull/2112

## API Behavior Changes

Expand Down
29 changes: 28 additions & 1 deletion Sources/SwiftSyntax/Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,36 @@
//
//===----------------------------------------------------------------------===//

extension ClosureCaptureSyntax {

/// Creates a ``ClosureCaptureSyntax`` with a `name`, and automatically adds an `equal` token to it since the name is non-optional.
///
/// - SeeAlso: ``ClosureCaptureSyntax/init(leadingTrivia:_:specifier:_:name:_:equal:_:expression:_:trailingComma:_:trailingTrivia:)``.
///
public init(
leadingTrivia: Trivia? = nil,
specifier: ClosureCaptureSpecifierSyntax? = nil,
name: TokenSyntax,
equal: TokenSyntax = TokenSyntax.equalToken(),
expression: some ExprSyntaxProtocol,
trailingComma: TokenSyntax? = nil,
trailingTrivia: Trivia? = nil
) {
self.init(
leadingTrivia: leadingTrivia,
specifier: specifier,
name: name as TokenSyntax?,
equal: equal,
expression: expression,
trailingComma: trailingComma,
trailingTrivia: trailingTrivia
)
}
}

extension EnumCaseParameterSyntax {

/// Creates an `EnumCaseParameterSyntax` with a `firstName`, and automatically adds a `colon` to it.
/// Creates an ``EnumCaseParameterSyntax`` with a `firstName`, and automatically adds a `colon` to it.
///
/// - SeeAlso: For more information on the arguments, see ``EnumCaseParameterSyntax/init(leadingTrivia:_:modifiers:_:firstName:_:secondName:_:colon:_:type:_:defaultArgument:_:trailingComma:_:trailingTrivia:)``
///
Expand Down
8 changes: 8 additions & 0 deletions Tests/SwiftSyntaxTest/SyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,12 @@ public class SyntaxTests: XCTestCase {
let node = EnumCaseParameterSyntax(firstName: "label", type: TypeSyntax("MyType"))
XCTAssertEqual(node.formatted().description, "label: MyType")
}

public func testClosureCaptureSyntaxConvenienceInitWithEqual() {
let noNameClosureCapture = ClosureCaptureSyntax(expression: ExprSyntax("123"))
XCTAssertEqual(noNameClosureCapture.formatted().description, "123")

let node = ClosureCaptureSyntax(name: "test", expression: ExprSyntax("123"))
XCTAssertEqual(node.formatted().description, "test = 123")
}
}