Comment processing fixes

This commit is contained in:
Mike Farah 2023-06-06 13:33:16 -07:00
parent 49b8eafaf5
commit f7c057b167
3 changed files with 26 additions and 4 deletions

View File

@ -82,7 +82,11 @@ func (ye *yamlEncoder) PrintLeadingContent(writer io.Writer, content string) err
func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error { func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
log.Debug("encoderYaml - going to print %v", NodeToString(node)) log.Debug("encoderYaml - going to print %v", NodeToString(node))
if node.Kind == ScalarNode && ye.prefs.UnwrapScalar { if node.Kind == ScalarNode && ye.prefs.UnwrapScalar {
return writeString(writer, node.Value+"\n") valueToPrint := node.Value
if node.LeadingContent == "" || valueToPrint != "" {
valueToPrint = valueToPrint + "\n"
}
return writeString(writer, valueToPrint)
} }
destination := writer destination := writer

View File

@ -266,7 +266,25 @@ var commentOperatorScenarios = []expressionScenario{
document: " # hi", document: " # hi",
expression: `.`, expression: `.`,
expected: []string{ expected: []string{
"D0, P[], (!!null):: # hi\n\n", "D0, P[], (!!null):: # hi\n",
},
},
{
description: "string spaces",
skipDoc: true,
document: "# hi\ncat\n",
expression: `.`,
expected: []string{
"D0, P[], (!!str)::# hi\ncat\n",
},
},
{
description: "leading spaces with new line",
skipDoc: true,
document: " # hi\n",
expression: `.`,
expected: []string{
"D0, P[], (!!null):: # hi\n",
}, },
}, },
{ {
@ -275,7 +293,7 @@ var commentOperatorScenarios = []expressionScenario{
document: "%YAML 1.1\n# hi\n", document: "%YAML 1.1\n# hi\n",
expression: `.`, expression: `.`,
expected: []string{ expected: []string{
"D0, P[], (!!null)::%YAML 1.1\n# hi\n\n", "D0, P[], (!!null)::%YAML 1.1\n# hi\n",
}, },
}, },
} }

View File

@ -10,7 +10,7 @@ var loadScenarios = []expressionScenario{
description: "Load empty file with a comment", description: "Load empty file with a comment",
expression: `load("../../examples/empty.yaml")`, expression: `load("../../examples/empty.yaml")`,
expected: []string{ expected: []string{
"D0, P[], (!!null)::# comment\n\n", "D0, P[], (!!null)::# comment\n",
}, },
}, },
{ {