Skip to content

Commit

Permalink
Add a convenience init to ClosureCaptureSyntax
Browse files Browse the repository at this point in the history
**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: apple#1984
  • Loading branch information
natikgadzhi committed Aug 31, 2023
1 parent 12e2eb8 commit 269c036
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Sources/SwiftSyntax/Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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")
}
}

0 comments on commit 269c036

Please sign in to comment.