File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed
Sources/SwiftSyntaxBuilder
Tests/SwiftSyntaxBuilderTest Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ extension ExtensionDeclSyntax: HasTrailingMemberDeclBlock {}
93
93
extension ProtocolDeclSyntax : HasTrailingMemberDeclBlock { }
94
94
extension StructDeclSyntax : HasTrailingMemberDeclBlock { }
95
95
96
- // MARK: - IfStmt
96
+ // MARK: - IfStmtSyntax
97
97
// IfStmtSyntax is a special scenario as we also have the `else` body or an if-else
98
98
// So we cannot conform to `HasTrailingCodeBlock`
99
99
@@ -105,10 +105,21 @@ public extension IfStmtSyntax {
105
105
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
106
106
}
107
107
108
- init ( _ signature: String , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) {
108
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) {
109
109
self = " \( signature) {} "
110
110
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
111
111
self . elseBody = . ifStmt( elseIf)
112
112
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
113
113
}
114
114
}
115
+
116
+ // MARK: - SwitchStmtSyntax
117
+ // SwitchStmtSyntax is a special scenario as it don't have body or members
118
+ // So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
119
+
120
+ public extension SwitchStmtSyntax {
121
+ init ( _ signature: PartialSyntaxNodeString , @SwitchCaseListBuilder casesBuilder: ( ) -> SwitchCaseListSyntax = { SwitchCaseListSyntax ( [ ] ) } ) {
122
+ self = " \( signature) {} "
123
+ self . cases = casesBuilder ( )
124
+ }
125
+ }
Original file line number Diff line number Diff line change @@ -41,4 +41,31 @@ final class SwitchTests: XCTestCase {
41
41
"""
42
42
)
43
43
}
44
+
45
+ func testSwitchStmtSyntaxWithStringParsing( ) {
46
+ let syntax = SwitchStmtSyntax ( " switch count " ) {
47
+ for num in 1 ..< 3 {
48
+ SwitchCaseSyntax ( " case \( literal: num) : " ) {
49
+ ExprSyntax ( " print(count) " )
50
+ }
51
+ }
52
+ SwitchCaseSyntax ( " default: " ) {
53
+ BreakStmtSyntax ( " break " )
54
+ }
55
+ }
56
+
57
+ AssertBuildResult (
58
+ syntax,
59
+ """
60
+ switch count {
61
+ case 1:
62
+ print(count)
63
+ case 2:
64
+ print(count)
65
+ default:
66
+ break
67
+ }
68
+ """
69
+ )
70
+ }
44
71
}
You can’t perform that action at this time.
0 commit comments