From 2513ddb13da6e29d10dedb08762fb4eba91efa31 Mon Sep 17 00:00:00 2001 From: Daniel Chao Date: Tue, 9 Dec 2025 11:03:50 -0800 Subject: [PATCH] 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 --- pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt | 2 +- .../src/test/kotlin/org/pkl/formatter/FormatterTest.kt | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt b/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt index c89f8046..6ce74d17 100644 --- a/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt +++ b/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt @@ -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() diff --git a/pkl-formatter/src/test/kotlin/org/pkl/formatter/FormatterTest.kt b/pkl-formatter/src/test/kotlin/org/pkl/formatter/FormatterTest.kt index cd91fc6a..bbad5981 100644 --- a/pkl-formatter/src/test/kotlin/org/pkl/formatter/FormatterTest.kt +++ b/pkl-formatter/src/test/kotlin/org/pkl/formatter/FormatterTest.kt @@ -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") + } + } }