From bf6b4d394193a9335320da645e356ecb85b6c5cf Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 30 Aug 2022 11:24:26 +1000 Subject: [PATCH] Fixed footer comment issue #1321 --- pkg/yqlib/operator_comments.go | 4 ++++ pkg/yqlib/operator_comments_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pkg/yqlib/operator_comments.go b/pkg/yqlib/operator_comments.go index a67f2178..60b3e068 100644 --- a/pkg/yqlib/operator_comments.go +++ b/pkg/yqlib/operator_comments.go @@ -63,8 +63,12 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod } if preferences.FootComment && candidate.Node.Kind == yaml.DocumentNode && comment != "" { candidate.TrailingContent = "# " + comment + } else if preferences.FootComment && candidate.Node.Kind == yaml.DocumentNode { + candidate.TrailingContent = comment + } else if preferences.FootComment && candidate.Node.Kind != yaml.DocumentNode { candidate.Node.FootComment = comment + candidate.TrailingContent = "" } } diff --git a/pkg/yqlib/operator_comments_test.go b/pkg/yqlib/operator_comments_test.go index a3fcd148..65effab9 100644 --- a/pkg/yqlib/operator_comments_test.go +++ b/pkg/yqlib/operator_comments_test.go @@ -62,6 +62,15 @@ var commentOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::a: cat\n# cat\n", }, }, + { + skipDoc: true, + description: "Set foot comment, using an expression", + document: "a: cat\n\n# hi", + expression: `. foot_comment=""`, + expected: []string{ + "D0, P[], (doc)::a: cat\n", + }, + }, { skipDoc: true, document: `a: cat`, @@ -112,6 +121,24 @@ var commentOperatorScenarios = []expressionScenario{ "D0, P[], (!!str)::welcome!\n", }, }, + { + skipDoc: true, + description: "strip trailing comment recurse all", + document: "a: cat\n\n# haha", + expression: `... comments= ""`, + expected: []string{ + "D0, P[], (!!map)::a: cat\n", + }, + }, + { + skipDoc: true, + description: "strip trailing comment recurse values", + document: "a: cat\n\n# haha", + expression: `.. comments= ""`, + expected: []string{ + "D0, P[], (!!map)::a: cat\n", + }, + }, { description: "Head comment with document split", dontFormatInputForDoc: true,