Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comment position of map node has multiple values #532

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,12 @@ func (p *parser) createMapValueNode(ctx *context, key ast.MapKeyNode, colonToken
return nil, err
}
if comment != nil {
_ = value.SetComment(comment)
nextLineComment := key.GetToken().Position.Line < comment.GetToken().Position.Line
if n, ok := value.(*ast.MappingNode); ok && nextLineComment && len(n.Values) > 1 {
_ = n.Values[0].SetComment(comment)
} else {
_ = value.SetComment(comment)
}
}
return value, nil
}
Expand Down
57 changes: 57 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,63 @@ baz:
})
}

func TestCommentWithMap(t *testing.T) {
yml := `
single:
# foo comment
foo: bar

multiple:
# a comment
a: b
# c comment
c: d
`

file, err := parser.ParseBytes([]byte(yml), parser.ParseComments)
if err != nil {
t.Fatal(err)
}
if len(file.Docs) == 0 {
t.Fatal("cannot get file docs")
}
if file.Docs[0].Body == nil {
t.Fatal("cannot get docs body")
}
mapNode, ok := file.Docs[0].Body.(*ast.MappingNode)
if !ok {
t.Fatalf("failed to get map node. got: %T\n", file.Docs[0].Body)
}
if len(mapNode.Values) != 2 {
t.Fatalf("failed to get map values. got %d", len(mapNode.Values))
}

singleNode, ok := mapNode.Values[0].Value.(*ast.MappingValueNode)
if !ok {
t.Fatalf("failed to get single node. got %T", mapNode.Values[0].Value)
}
if singleNode.GetComment().GetToken().Value != " foo comment" {
t.Fatalf("failed to get comment from single. got %q", singleNode.GetComment().GetToken().Value)
}

multiNode, ok := mapNode.Values[1].Value.(*ast.MappingNode)
if !ok {
t.Fatalf("failed to get multiple node. got: %T", mapNode.Values[1])
}
if multiNode.GetComment() != nil {
t.Fatalf("found unexpected comment")
}
if len(multiNode.Values) != 2 {
t.Fatalf("failed to get multiple node values. got %d", len(multiNode.Values))
}
if multiNode.Values[0].GetComment().GetToken().Value != " a comment" {
t.Fatalf("failed to get comment from multiple[0]. got %q", multiNode.Values[0].GetComment().GetToken().Value)
}
if multiNode.Values[1].GetComment().GetToken().Value != " c comment" {
t.Fatalf("failed to get comment from multiple[1]. got %q", multiNode.Values[1].GetComment().GetToken().Value)
}
}

func TestNodePath(t *testing.T) {
yml := `
a: # commentA
Expand Down
Loading