Skip to content

Empty children arrays #1

Closed
Closed
@eush77

Description

@eush77

This is a question and a suggestion regarding this part of Unist readme (emphasis mine):

Unist nodes:

  • may have either a value property set to a string or a children property set to an array of one or more Unist nodes;

I read it as “Unist nodes may have a children property, in which case it is guaranteed that its length is ≥1”. If this is correct, then it follows that both retext, mdast, and hast violate this specification by producing trees with empty children arrays:

> retext.parse('')
{ type: 'RootNode', children: [] }
> mdast.parse('')
{ type: 'root',
  children: [],
  position: { start: { line: 1, column: 1 }, end: { line: 1, column: 1 } } }
> mdast.parse('#')
{ type: 'root',
  children: [ { type: 'heading', depth: 1, children: [], position: [Object] } ],
  position: { start: { line: 1, column: 1 }, end: { line: 1, column: 2 } } }
> hast.parse('')
{ type: 'root', children: [] }

If I haven't missed anything then I guess this requirement should be relaxed to include empty children (or removed if it doesn't require anything) or, alternatively, retext, mdast, and hast should be fixed to never output nodes with empty children arrays. The latter seems more problematic (the obvious workaround is returning null on empty input but I feel that it's better for parsers to always output a valid syntax tree) so I opened the issue here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions