Skip to content

Commit 8dbaa21

Browse files
Merge pull request #962 from emh-jump/empty-doc-seperator
Fix `ytt fmt` for files with a single document separator
2 parents 6c3e762 + 8a97166 commit 8dbaa21

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ linters:
1616
rules:
1717
- name: dot-imports
1818
disabled: true
19+
- name: add-constant
20+
arguments:
21+
- allowInts: "0,1"
1922
exclusions:
2023
generated: lax
2124
paths:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
hello: world
2+
---
3+
hello: moon
4+
5+
+++
6+
7+
hello: world
8+
---
9+
hello: moon

pkg/yamlfmt/printer.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ func (p *Printer) PrintStr(val interface{}) string {
4242
func (p *Printer) print(val interface{}, ws whitespace, writer *writer) {
4343
switch typedVal := val.(type) {
4444
case *yamlmeta.DocumentSet:
45+
// if the last document is empty, don't add a separator before it
46+
nonEmptyDocCount := len(typedVal.Items)
47+
lastDoc := typedVal.Items[len(typedVal.Items)-1]
48+
if lastDoc.IsEmpty() {
49+
nonEmptyDocCount--
50+
}
51+
4552
for i, item := range typedVal.Items {
4653
// TODO deal with first empty doc
4754
meta := p.printMeta(item, ws, writer, i == 0)
48-
if (i > 0 && i < len(typedVal.Items)-1) || len(meta.Suffix) > 0 {
55+
if (i > 0 && i < nonEmptyDocCount) || len(meta.Suffix) > 0 {
4956
writer.AddContent(writerChunk{
5057
Indent: ws.Indent,
5158
Content: "---" + meta.Suffix,

0 commit comments

Comments
 (0)