From 8fb3713a4f55554927ca01d1abb0adc7debe85ea Mon Sep 17 00:00:00 2001 From: Natik Gadzhi Date: Thu, 31 Aug 2023 13:31:18 -0700 Subject: [PATCH 1/2] 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: https://github.com/apple/swift-syntax/issues/1984 --- Sources/SwiftSyntax/Convenience.swift | 27 +++++++++++++++++++++++++ Tests/SwiftSyntaxTest/SyntaxTests.swift | 8 ++++++++ 2 files changed, 35 insertions(+) diff --git a/Sources/SwiftSyntax/Convenience.swift b/Sources/SwiftSyntax/Convenience.swift index eba5cea70ec..c16397f3df8 100644 --- a/Sources/SwiftSyntax/Convenience.swift +++ b/Sources/SwiftSyntax/Convenience.swift @@ -10,6 +10,33 @@ // //===----------------------------------------------------------------------===// +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. diff --git a/Tests/SwiftSyntaxTest/SyntaxTests.swift b/Tests/SwiftSyntaxTest/SyntaxTests.swift index 5dc4b521ed8..6ae3c48d5c3 100644 --- a/Tests/SwiftSyntaxTest/SyntaxTests.swift +++ b/Tests/SwiftSyntaxTest/SyntaxTests.swift @@ -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") + } } From a9acb00f3366b76db51d0ec2703649c65bb49451 Mon Sep 17 00:00:00 2001 From: Natik Gadzhi Date: Thu, 31 Aug 2023 16:13:57 -0700 Subject: [PATCH 2/2] Provides release notes entries for new APIs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provides release notes entries for: - `ClosureCaptureSyntax.init` https://github.com/apple/swift-syntax/pull/2127 - `EnumCaseParameterSyntax.init` https://github.com/apple/swift-syntax/pull/2112 Co-authored-by: Mateusz Bąk --- Release Notes/510.md | 10 +++++++++- Sources/SwiftSyntax/Convenience.swift | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Release Notes/510.md b/Release Notes/510.md index a3c1a6f9b55..1afc7f6e5b6 100644 --- a/Release Notes/510.md +++ b/Release Notes/510.md @@ -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 diff --git a/Sources/SwiftSyntax/Convenience.swift b/Sources/SwiftSyntax/Convenience.swift index c16397f3df8..6521bd0e569 100644 --- a/Sources/SwiftSyntax/Convenience.swift +++ b/Sources/SwiftSyntax/Convenience.swift @@ -12,7 +12,7 @@ extension ClosureCaptureSyntax { - /// Creates a `ClosureCaptureSyntax` with a `name`, and automatically adds an `equal` token to it since the name is non-optional. + /// 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:)``. /// @@ -39,7 +39,7 @@ extension ClosureCaptureSyntax { 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:)`` ///