Skip to content

Commit 32e9087

Browse files
bioballHT154
andauthored
Fix formatting of nodes with no children (#1351)
For example, this fixes an issue where an empty module turns into ` \n`. Closes #1348 --------- Co-authored-by: Jen Basch <[email protected]>
1 parent 9d41518 commit 32e9087

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
12391239
// skip semicolons
12401240
val children = children.filter { !it.isSemicolon() }
12411241
// short circuit
1242-
if (children.isEmpty()) return listOf(spaceOrLine())
1242+
if (children.isEmpty()) return emptyList()
12431243
if (children.size == 1) return listOf(format(children[0]))
12441244

12451245
val nodes = mutableListOf<FormatNode>()

pkl-formatter/src/test/kotlin/org/pkl/formatter/FormatterTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,11 @@ class FormatterTest {
113113

114114
walkDir(outputDir)
115115
}
116+
117+
@Test
118+
fun `whitespace only`() {
119+
for (src in listOf(";;;", "\n", "\n\n\n", "\t")) {
120+
assertThat(format(src)).isEqualTo("\n")
121+
}
122+
}
116123
}

0 commit comments

Comments
 (0)