Skip to content

LiteralNode.String() omits trailing blank lines for literal strings #872

Description

@semihbkgr

When parsing a multiline string with |+, LiteralNode.Value.Value contains the trailing blank lines, but LiteralNode.String() does not render them.

Is this intentional behavior?

I expected LiteralNode.String() to include these trailing blank lines, because these newlines are part of the AST node and change both the value and the representation in the source.

Go Playground reproduction:

https://go.dev/play/p/Y1m0qWMgymQ

This came up in yamldiff: semihbkgr/yamldiff#60

Example

Input:

description: |+
  one
  two


Observed output:

== raw input ==
01: description: |+$
02:   one$
03:   two$
04: $
05: $
06: $

== AST node type ==
Literal

== node.String() ==
01: |+$
02:   one$
03:   two$

== LiteralNode.Value.Value ==
01: one$
02: two$
03: $
04: $
05: $

Go quoted value: "one\ntwo\n\n\n\n"

LiteralNode.Value.Value includes the trailing blank lines:

"one\ntwo\n\n\n\n"

But node.String() renders only:

|+
  one
  two

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions