mirror of
https://github.com/goccy/go-yaml.git
synced 2025-11-10 18:21:12 +00:00
Fix misaligned indentation in comments (#734)
This commit is contained in:
parent
6a6c998c2f
commit
f1c23f747b
2 changed files with 58 additions and 4 deletions
|
|
@ -539,6 +539,9 @@ func (p *parser) validateMapKeyValueNextToken(ctx *context, keyTk, tk *Token) er
|
||||||
if tk.Column() <= keyTk.Column() {
|
if tk.Column() <= keyTk.Column() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if ctx.isComment() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if ctx.isFlow && (tk.Type() == token.CollectEntryType || tk.Type() == token.SequenceEndType) {
|
if ctx.isFlow && (tk.Type() == token.CollectEntryType || tk.Type() == token.SequenceEndType) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1454,6 +1454,7 @@ func TestComment(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
yaml string
|
yaml string
|
||||||
|
expected string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "map with comment",
|
name: "map with comment",
|
||||||
|
|
@ -1541,6 +1542,52 @@ foo: > # comment
|
||||||
# This comment is in its own document
|
# This comment is in its own document
|
||||||
---
|
---
|
||||||
a: b
|
a: b
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "map with misaligned indentation in comments",
|
||||||
|
yaml: `
|
||||||
|
# commentA
|
||||||
|
a: #commentB
|
||||||
|
# commentC
|
||||||
|
b: c # commentD
|
||||||
|
# commentE
|
||||||
|
d: e # commentF
|
||||||
|
# commentG
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
# commentA
|
||||||
|
a: #commentB
|
||||||
|
# commentC
|
||||||
|
b: c # commentD
|
||||||
|
# commentE
|
||||||
|
d: e # commentF
|
||||||
|
# commentG
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "sequence with misaligned indentation in comments",
|
||||||
|
yaml: `
|
||||||
|
# commentA
|
||||||
|
- a # commentB
|
||||||
|
# commentC
|
||||||
|
- b: # commentD
|
||||||
|
# commentE
|
||||||
|
- d # commentF
|
||||||
|
# commentG
|
||||||
|
- e # commentG
|
||||||
|
# commentH
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
# commentA
|
||||||
|
- a # commentB
|
||||||
|
# commentC
|
||||||
|
- b: # commentD
|
||||||
|
# commentE
|
||||||
|
- d # commentF
|
||||||
|
# commentG
|
||||||
|
- e # commentG
|
||||||
|
# commentH
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -1551,8 +1598,12 @@ a: b
|
||||||
t.Fatalf("%+v", err)
|
t.Fatalf("%+v", err)
|
||||||
}
|
}
|
||||||
got := "\n" + f.String()
|
got := "\n" + f.String()
|
||||||
if test.yaml != got {
|
expected := test.yaml
|
||||||
t.Fatalf("expected:%s\ngot:%s", test.yaml, got)
|
if test.expected != "" {
|
||||||
|
expected = test.expected
|
||||||
|
}
|
||||||
|
if expected != got {
|
||||||
|
t.Fatalf("expected:%s\ngot:%s", expected, got)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue