File tree 3 files changed +50
-1
lines changed
SwiftSyntaxMacroExpansion
Tests/SwiftSyntaxBuilderTest
3 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,18 @@ extension CodeBlockItemListBuilder {
40
40
public static func buildExpression( _ expression: some Sequence < DeclSyntaxProtocol > ) -> Component {
41
41
buildExpression ( expression. map { CodeBlockItemSyntax ( item: . decl( DeclSyntax ( $0) ) ) } )
42
42
}
43
+
44
+ public static func buildFinalResult( _ component: Component ) -> CodeBlockItemListSyntax {
45
+ . init(
46
+ component. enumerated ( ) . map { ( index, expression) in
47
+ if index > component. startIndex, !expression. leadingTrivia. contains ( where: \. isNewline) {
48
+ return expression. with ( \. leadingTrivia, . newline. merging ( expression. leadingTrivia) )
49
+ } else {
50
+ return expression
51
+ }
52
+ }
53
+ )
54
+ }
43
55
}
44
56
45
57
extension ConditionElementListBuilder {
Original file line number Diff line number Diff line change @@ -528,5 +528,15 @@ public func collapse<Node: SyntaxProtocol>(
528
528
break
529
529
}
530
530
531
- return expansions. joined ( separator: separator)
531
+ // Join the expansions ensuring `separator` between them.
532
+ var collapsed = " "
533
+ for expansion in expansions {
534
+ if collapsed. isEmpty || expansion. hasPrefix ( separator) {
535
+ collapsed. append ( expansion)
536
+ } else {
537
+ collapsed. append ( separator + expansion)
538
+ }
539
+ }
540
+
541
+ return collapsed
532
542
}
Original file line number Diff line number Diff line change @@ -75,4 +75,31 @@ final class CollectionNodeFlatteningTests: XCTestCase {
75
75
"""
76
76
)
77
77
}
78
+
79
+ func test_FlattenCodeBlockItemListWithCodeBlockInterpolated( ) {
80
+ let block = CodeBlockItemListSyntax {
81
+ " let a = 1 "
82
+ " let b = 2 "
83
+ " let c = 3 "
84
+ }
85
+
86
+ let buildable = CodeBlockItemListSyntax {
87
+ " let one = object.methodOne() "
88
+ " let two = object.methodTwo() "
89
+ " let three = { \( block) }() "
90
+ }
91
+
92
+ assertBuildResult (
93
+ buildable,
94
+ """
95
+ let one = object.methodOne()
96
+ let two = object.methodTwo()
97
+ let three = {
98
+ let a = 1
99
+ let b = 2
100
+ let c = 3
101
+ }()
102
+ """
103
+ )
104
+ }
78
105
}
You can’t perform that action at this time.
0 commit comments