eemeli/yaml

It's possible to get a node without a range after successfully parsing a document with a map inside a collection

Closed this issue · 1 comments

Describe the bug
Sorry for the long winded title, but it's a bit hard to describe! I've found a case that parses successfully, but contains a YAMLMap node that does not have a range. This violates the TypeScript type definitions exposed by this library, although I'm fairly certain the issue is not with the type definitions, but with the parsing itself.

To Reproduce

const YAML = require('yaml');
const parsedDocument = YAML.parseDocument('[a:]')
const contents = parsedDocument.contents;
assert(YAML.isSeq(contents));
const firstItem = contents.items[0];
assert(firstItem);
const range = firstItem.range;
assert(range); // Uncaught AssertionError [ERR_ASSERTION]: undefined == true

According to TypeScript, range should never be null/undefined in this example, but it is in fact undefined.

Expected behaviour
Either parseDocument should fail because this is not compliant YAML, or firstItem should have a range, or the TypeScript types should correctly identify that range is optional on firstItem.

Versions (please complete the following information):

  • Environment: Node v22.6.0
  • yaml: 2.5.0

Additional context
Incidentally, the following does include a range when parsed: [{a:}].

Thank you for noticing and reporting this! There was a missing assignment of the range for the map for flow sequence pairs.