Skip to content

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

Closed
@TAGraves

Description

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:}].

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions