File tree Expand file tree Collapse file tree 3 files changed +50
-1
lines changed
SwiftSyntaxMacroExpansion
Tests/SwiftSyntaxBuilderTest Expand file tree Collapse file tree 3 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,18 @@ extension CodeBlockItemListBuilder {
4040 public static func buildExpression( _ expression: some Sequence < DeclSyntaxProtocol > ) -> Component {
4141 buildExpression ( expression. map { CodeBlockItemSyntax ( item: . decl( DeclSyntax ( $0) ) ) } )
4242 }
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+ }
4355}
4456
4557extension ConditionElementListBuilder {
Original file line number Diff line number Diff line change @@ -528,5 +528,15 @@ public func collapse<Node: SyntaxProtocol>(
528528 break
529529 }
530530
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
532542}
Original file line number Diff line number Diff line change @@ -75,4 +75,31 @@ final class CollectionNodeFlatteningTests: XCTestCase {
7575 """
7676 )
7777 }
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+ }
78105}
You can’t perform that action at this time.
0 commit comments