Fix formatting of nodes with no children (#1351)

For example, this fixes an issue where an empty module turns into ` \n`.

Closes https://github.com/apple/pkl/issues/1348

---------

Co-authored-by: Jen Basch <jbasch94@gmail.com>
This commit is contained in:
Daniel Chao
2025-12-09 11:03:50 -08:00
committed by GitHub
parent 9d41518553
commit 32e9087da9
2 changed files with 8 additions and 1 deletions

View File

@@ -1239,7 +1239,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
// skip semicolons
val children = children.filter { !it.isSemicolon() }
// short circuit
if (children.isEmpty()) return listOf(spaceOrLine())
if (children.isEmpty()) return emptyList()
if (children.size == 1) return listOf(format(children[0]))
val nodes = mutableListOf<FormatNode>()

View File

@@ -113,4 +113,11 @@ class FormatterTest {
walkDir(outputDir)
}
@Test
fun `whitespace only`() {
for (src in listOf(";;;", "\n", "\n\n\n", "\t")) {
assertThat(format(src)).isEqualTo("\n")
}
}
}