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 b474940 commit 14bc7f4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Sources/SwiftSyntax/Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,64 @@
//
//===----------------------------------------------------------------------===//

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.
///
/// - SeeAlso: For more information on the arguments, see ``EnumCaseParameterSyntax/init(leadingTrivia:_:modifiers:_:firstName:_:secondName:_:colon:_:type:_:defaultArgument:_:trailingComma:_:trailingTrivia:)``
///
public init(
leadingTrivia: Trivia? = nil,
modifiers: DeclModifierListSyntax = [],
firstName: TokenSyntax,
secondName: TokenSyntax? = nil,
colon: TokenSyntax = TokenSyntax.colonToken(),
type: some TypeSyntaxProtocol,
defaultValue: InitializerClauseSyntax? = nil,
trailingComma: TokenSyntax? = nil,
trailingTrivia: Trivia? = nil
) {
self.init(
leadingTrivia: leadingTrivia,
modifiers: modifiers,
firstName: firstName as TokenSyntax?,
secondName: secondName,
colon: colon,
type: type,
defaultValue: defaultValue,
trailingComma: trailingComma,
trailingTrivia: trailingTrivia
)
}
}

extension MemberAccessExprSyntax {
/// Creates a new ``MemberAccessExprSyntax`` where the accessed member is represented by
/// an identifier without specifying argument labels.
Expand Down
16 changes: 16 additions & 0 deletions Tests/SwiftSyntaxTest/SyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,20 @@ public class SyntaxTests: XCTestCase {
XCTAssertEqual(funcKW.endPosition, AbsolutePosition(utf8Offset: 7))
XCTAssertEqual(funcKW.trimmedLength, SourceLength(utf8Length: 4))
}

public func testEnumCaseParameterSyntaxConvenienceInit() {
let noFirstName = EnumCaseParameterSyntax(type: TypeSyntax("MyType"))
XCTAssertEqual(noFirstName.formatted().description, "MyType")

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 14bc7f4

Please sign in to comment.