Skip to content

Commit 9955be7

Browse files
committed
Add an overload to EnumCaseParameterSyntax
1 parent e99a573 commit 9955be7

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Sources/SwiftSyntax/Convenience.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,56 @@ extension MemberAccessExprSyntax {
4545
)
4646
}
4747
}
48+
49+
extension EnumCaseParameterSyntax {
50+
51+
/// Creates an `EnumCaseParameterSyntax` with a `firstName`, and automatically adds a `colon` to it.
52+
///
53+
/// - Parameters:
54+
/// - leadingTrivia: Trivia to be prepended to the leading trivia of the node’s first token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
55+
/// - colon: If the parameter has a label, the colon separating the label from the type. SInce `firstName` is not nil, the default `colon` will be set to `TokenSyntax.colonToken()`.
56+
/// - type: The parameter's type.
57+
/// - defaultValue: If the parameter has a default value, the initializer clause describing the default value.
58+
/// - trailingComma: If the parameter is followed by another parameter, the comma separating them.
59+
/// - trailingTrivia: Trivia to be appended to the trailing trivia of the node’s last token. If the node is empty, there is no token to attach the trivia to and the parameter is ignored.
60+
public init(
61+
leadingTrivia: Trivia? = nil,
62+
_ unexpectedBeforeModifiers: UnexpectedNodesSyntax? = nil,
63+
modifiers: DeclModifierListSyntax = [],
64+
_ unexpectedBetweenModifiersAndFirstName: UnexpectedNodesSyntax? = nil,
65+
firstName: TokenSyntax,
66+
_ unexpectedBetweenFirstNameAndSecondName: UnexpectedNodesSyntax? = nil,
67+
secondName: TokenSyntax? = nil,
68+
_ unexpectedBetweenSecondNameAndColon: UnexpectedNodesSyntax? = nil,
69+
colon: TokenSyntax = TokenSyntax.colonToken(),
70+
_ unexpectedBetweenColonAndType: UnexpectedNodesSyntax? = nil,
71+
type: some TypeSyntaxProtocol,
72+
_ unexpectedBetweenTypeAndDefaultValue: UnexpectedNodesSyntax? = nil,
73+
defaultValue: InitializerClauseSyntax? = nil,
74+
_ unexpectedBetweenDefaultValueAndTrailingComma: UnexpectedNodesSyntax? = nil,
75+
trailingComma: TokenSyntax? = nil,
76+
_ unexpectedAfterTrailingComma: UnexpectedNodesSyntax? = nil,
77+
trailingTrivia: Trivia? = nil
78+
79+
) {
80+
// Wrapping firstName as an Optional so that we can use the generated overload.
81+
let firstNameAsOptional: TokenSyntax? = firstName
82+
self = .init(leadingTrivia: leadingTrivia,
83+
unexpectedBeforeModifiers,
84+
modifiers: modifiers,
85+
unexpectedBetweenModifiersAndFirstName,
86+
firstName: firstNameAsOptional,
87+
unexpectedBetweenFirstNameAndSecondName,
88+
secondName: secondName,
89+
unexpectedBetweenSecondNameAndColon,
90+
colon: colon,
91+
unexpectedBetweenColonAndType,
92+
type: type,
93+
unexpectedBetweenTypeAndDefaultValue,
94+
defaultValue: defaultValue,
95+
unexpectedBetweenDefaultValueAndTrailingComma,
96+
trailingComma: trailingComma,
97+
unexpectedAfterTrailingComma,
98+
trailingTrivia: trailingTrivia)
99+
}
100+
}

Tests/SwiftSyntaxTest/SyntaxTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,12 @@ public class SyntaxTests: XCTestCase {
125125
XCTAssertEqual(funcKW.endPosition, AbsolutePosition(utf8Offset: 7))
126126
XCTAssertEqual(funcKW.trimmedLength, SourceLength(utf8Length: 4))
127127
}
128+
129+
public func testEnumCaseParameterSyntaxConvenienceInit() {
130+
let noFirstName = EnumCaseParameterSyntax(type: TypeSyntax("MyType"))
131+
XCTAssertEqual(noFirstName.formatted().description, "MyType")
132+
133+
let node = EnumCaseParameterSyntax(firstName: "label", type: TypeSyntax("MyType"))
134+
XCTAssertEqual(node.formatted().description, "label: MyType")
135+
}
128136
}

0 commit comments

Comments
 (0)