eemeli / yaml

YAML parser and stringifier for JavaScript
https://eemeli.org/yaml
ISC License
1.31k stars 115 forks source link

`range` of a Block Scalar should match its `value` #532

Closed k-jiang closed 8 months ago

k-jiang commented 8 months ago

Describe the bug The "start offset" of a Block Scalar value should be the beginning of the string value, not the "|-" part

      v Range should not starts from here
test: |-
    This is a string

To Reproduce

test: |-
    This is a string
var yaml = require("yaml")
var text = "test: |-\n    This is a string"
var doc = yaml.parseDocument(text, { keepSourceTokens: true, strict: false })

console.log("value:", doc.contents.items[0].value.value) // "This is a string"
console.log("offset:", doc.contents.items[0].value.range[0]) // 6, should be 13 instead

Expected behaviour

test: |-
    This is a string
    ^ The range offset should start here

Versions:

eemeli commented 8 months ago

Why?

k-jiang commented 8 months ago

Well I'm actually trying to get the exact offset of the string value. By instinct I would think doc.contents.items[0].value.range only indicating the start and end of the value itself, yet it seems covering something else as well.

I did not read through the YAML specification. If the value.range does work as intended, that should be fine.

I will try to calculate it through srcToken though it is a bit of complicated 😂

eemeli commented 8 months ago

For a block scalar, the header is a required part of the source to determining its value. So what you're asking for would be analogous for the range of a quoted scalar like "foo" to not include the " marks.