Skip to content

Commit 28e00f3

Browse files
authored
Merge pull request #2112 from natikgadzhi/syntax/enum-case-parameter
Add automatic colons or paranthesis to nodes when needed
2 parents 4652ad2 + 954c2bc commit 28e00f3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Sources/SwiftSyntax/Convenience.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,37 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
extension EnumCaseParameterSyntax {
14+
15+
/// Creates an `EnumCaseParameterSyntax` with a `firstName`, and automatically adds a `colon` to it.
16+
///
17+
/// - SeeAlso: For more information on the arguments, see ``EnumCaseParameterSyntax/init(leadingTrivia:_:modifiers:_:firstName:_:secondName:_:colon:_:type:_:defaultArgument:_:trailingComma:_:trailingTrivia:)``
18+
///
19+
public init(
20+
leadingTrivia: Trivia? = nil,
21+
modifiers: DeclModifierListSyntax = [],
22+
firstName: TokenSyntax,
23+
secondName: TokenSyntax? = nil,
24+
colon: TokenSyntax = TokenSyntax.colonToken(),
25+
type: some TypeSyntaxProtocol,
26+
defaultValue: InitializerClauseSyntax? = nil,
27+
trailingComma: TokenSyntax? = nil,
28+
trailingTrivia: Trivia? = nil
29+
) {
30+
self.init(
31+
leadingTrivia: leadingTrivia,
32+
modifiers: modifiers,
33+
firstName: firstName as TokenSyntax?,
34+
secondName: secondName,
35+
colon: colon,
36+
type: type,
37+
defaultValue: defaultValue,
38+
trailingComma: trailingComma,
39+
trailingTrivia: trailingTrivia
40+
)
41+
}
42+
}
43+
1344
extension MemberAccessExprSyntax {
1445
/// Creates a new ``MemberAccessExprSyntax`` where the accessed member is represented by
1546
/// an identifier without specifying argument labels.
@@ -45,3 +76,8 @@ extension MemberAccessExprSyntax {
4576
)
4677
}
4778
}
79+
80+
//==========================================================================//
81+
// IMPORTANT: If you are tempted to add an extension here, please insert //
82+
// it in alphabetical order above //
83+
//==========================================================================//

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)