Skip to content

Commit d242664

Browse files
authored
Merge pull request #2858 from ahoppen/fix-duplicate-newline
Don’t add an implicit leading newline in `CodeBlockItemList` result builder if previous element already has a trailing newline
2 parents a32bdbf + 5080068 commit d242664

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

Sources/SwiftSyntaxBuilder/ResultBuilderExtensions.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@ extension CodeBlockItemListBuilder {
4242
}
4343

4444
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) {
45+
// Treat the first element as being on a new line. It doesn't need a leading newline
46+
var previousEndedInNewline = true
47+
48+
return CodeBlockItemListSyntax(
49+
component.map { expression in
50+
defer {
51+
previousEndedInNewline = expression.trailingTrivia.pieces.last?.isNewline ?? false
52+
}
53+
if !previousEndedInNewline, !expression.leadingTrivia.contains(where: \.isNewline) {
4854
return expression.with(\.leadingTrivia, .newline.merging(expression.leadingTrivia))
4955
} else {
5056
return expression

Sources/_SwiftSyntaxGenericTestSupport/AssertEqualWithDiff.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public func failStringsEqualWithDiff(
8383

8484
// Use `CollectionDifference` on supported platforms to get `diff`-like line-based output. On
8585
// older platforms, fall back to simple string comparison.
86-
let actualLines = actual.split(separator: "\n")
87-
let expectedLines = expected.split(separator: "\n")
86+
let actualLines = actual.split(separator: "\n", omittingEmptySubsequences: false)
87+
let expectedLines = expected.split(separator: "\n", omittingEmptySubsequences: false)
8888

8989
let difference = actualLines.difference(from: expectedLines)
9090

Tests/SwiftSyntaxBuilderTest/CollectionNodeFlatteningTests.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SwiftSyntaxBuilder
1515
import XCTest
1616

1717
final class CollectionNodeFlatteningTests: XCTestCase {
18-
func test_FlattenCodeBlockItemListWithBuilder() {
18+
func testFlattenCodeBlockItemListWithBuilder() {
1919
@CodeBlockItemListBuilder
2020
func buildInnerCodeBlockItemList() -> CodeBlockItemListSyntax {
2121
[ExprSyntax("innerBuilder1"), ExprSyntax("innerBuilder2")].lazy.map {
@@ -47,7 +47,7 @@ final class CollectionNodeFlatteningTests: XCTestCase {
4747
)
4848
}
4949

50-
func test_FlattenCodeBlockItemListWithCodeBlockItemStrings() {
50+
func testFlattenCodeBlockItemListWithCodeBlockItemStrings() {
5151
let buildable = CodeBlockItemListSyntax {
5252
"let one = object.methodOne()"
5353
"let two = object.methodTwo()"
@@ -62,7 +62,7 @@ final class CollectionNodeFlatteningTests: XCTestCase {
6262
)
6363
}
6464

65-
func test_FlattenCodeBlockItemListWithCodeBlockItemStringArray() {
65+
func testFlattenCodeBlockItemListWithCodeBlockItemStringArray() {
6666
let buildable = CodeBlockItemListSyntax {
6767
["let one = object.methodOne()", "let two = object.methodTwo()"]
6868
}
@@ -76,7 +76,7 @@ final class CollectionNodeFlatteningTests: XCTestCase {
7676
)
7777
}
7878

79-
func test_FlattenCodeBlockItemListWithCodeBlockInterpolated() {
79+
func testFlattenCodeBlockItemListWithCodeBlockInterpolated() {
8080
let block = CodeBlockItemListSyntax {
8181
"let a = 1"
8282
"let b = 2"
@@ -102,4 +102,21 @@ final class CollectionNodeFlatteningTests: XCTestCase {
102102
"""
103103
)
104104
}
105+
106+
func testFlattenCodeBlockItemListWithTrailingNewline() {
107+
let buildable = CodeBlockItemListSyntax {
108+
DeclSyntax("let a = 1").with(\.trailingTrivia, .newline)
109+
DeclSyntax("let b = 2").with(\.trailingTrivia, .newline)
110+
}
111+
112+
assertBuildResult(
113+
buildable,
114+
"""
115+
let a = 1
116+
let b = 2
117+
118+
"""
119+
)
120+
}
121+
105122
}

0 commit comments

Comments
 (0)