Skip to content

Commit f1c23f7

Browse files
authored
Fix misaligned indentation in comments (#734)
1 parent 6a6c998 commit f1c23f7

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

parser/parser.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ func (p *parser) validateMapKeyValueNextToken(ctx *context, keyTk, tk *Token) er
539539
if tk.Column() <= keyTk.Column() {
540540
return nil
541541
}
542+
if ctx.isComment() {
543+
return nil
544+
}
542545
if ctx.isFlow && (tk.Type() == token.CollectEntryType || tk.Type() == token.SequenceEndType) {
543546
return nil
544547
}

parser/parser_test.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,8 +1452,9 @@ foo:
14521452

14531453
func TestComment(t *testing.T) {
14541454
tests := []struct {
1455-
name string
1456-
yaml string
1455+
name string
1456+
yaml string
1457+
expected string
14571458
}{
14581459
{
14591460
name: "map with comment",
@@ -1541,6 +1542,52 @@ foo: > # comment
15411542
# This comment is in its own document
15421543
---
15431544
a: b
1545+
`,
1546+
},
1547+
{
1548+
name: "map with misaligned indentation in comments",
1549+
yaml: `
1550+
# commentA
1551+
a: #commentB
1552+
# commentC
1553+
b: c # commentD
1554+
# commentE
1555+
d: e # commentF
1556+
# commentG
1557+
`,
1558+
expected: `
1559+
# commentA
1560+
a: #commentB
1561+
# commentC
1562+
b: c # commentD
1563+
# commentE
1564+
d: e # commentF
1565+
# commentG
1566+
`,
1567+
},
1568+
{
1569+
name: "sequence with misaligned indentation in comments",
1570+
yaml: `
1571+
# commentA
1572+
- a # commentB
1573+
# commentC
1574+
- b: # commentD
1575+
# commentE
1576+
- d # commentF
1577+
# commentG
1578+
- e # commentG
1579+
# commentH
1580+
`,
1581+
expected: `
1582+
# commentA
1583+
- a # commentB
1584+
# commentC
1585+
- b: # commentD
1586+
# commentE
1587+
- d # commentF
1588+
# commentG
1589+
- e # commentG
1590+
# commentH
15441591
`,
15451592
},
15461593
}
@@ -1551,8 +1598,12 @@ a: b
15511598
t.Fatalf("%+v", err)
15521599
}
15531600
got := "\n" + f.String()
1554-
if test.yaml != got {
1555-
t.Fatalf("expected:%s\ngot:%s", test.yaml, got)
1601+
expected := test.yaml
1602+
if test.expected != "" {
1603+
expected = test.expected
1604+
}
1605+
if expected != got {
1606+
t.Fatalf("expected:%s\ngot:%s", expected, got)
15561607
}
15571608
})
15581609
}

0 commit comments

Comments
 (0)