Skip to content

Commit ffe7e47

Browse files
committed
Change to always return a node
1 parent 6f555a0 commit ffe7e47

File tree

8 files changed

+24
-17
lines changed

8 files changed

+24
-17
lines changed

lib/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,25 @@ import {createState} from './state.js'
8282
* mdast tree.
8383
* @param {Options | null | undefined} [options]
8484
* Configuration (optional).
85-
* @returns {HastNodes | undefined}
85+
* @returns {HastNodes}
8686
* hast tree.
8787
*/
88-
// To do: next major: always return a single `root`.
8988
export function toHast(tree, options) {
9089
const state = createState(tree, options)
9190
const node = state.one(tree, undefined)
9291
const foot = footer(state)
92+
/** @type {HastNodes} */
93+
const result = Array.isArray(node)
94+
? {type: 'root', children: node}
95+
: node || {type: 'root', children: []}
9396

9497
if (foot) {
9598
// If there’s a footer, there were definitions, meaning block
9699
// content.
97-
// So `node` is a parent node.
98-
assert(node)
99-
assert('children' in node)
100-
node.children.push({type: 'text', value: '\n'}, foot)
100+
// So `result` is a parent node.
101+
assert('children' in result)
102+
result.children.push({type: 'text', value: '\n'}, foot)
101103
}
102104

103-
// To do: next major: always return root?
104-
return Array.isArray(node) ? {type: 'root', children: node} : node
105+
return result
105106
}

lib/state.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* Transform an mdast node to hast.
4545
* @property {Options} options
4646
* Configuration.
47-
* @property {(from: MdastNodes, node: HastNodes) => void} patch
47+
* @property {(from: MdastNodes, node: HastNodes) => undefined} patch
4848
* Copy a node’s positional info.
4949
* @property {<Type extends HastRootContent>(nodes: Array<Type>, loose?: boolean | undefined) => Array<HastText | Type>} wrap
5050
* Wrap `nodes` with line endings between each node, adds initial/final line endings when `loose`.
@@ -224,7 +224,7 @@ export function createState(tree, options) {
224224
* mdast node to copy from.
225225
* @param {HastNodes} to
226226
* hast node to copy into.
227-
* @returns {void}
227+
* @returns {undefined}
228228
* Nothing.
229229
*/
230230
function patch(from, to) {

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Transform mdast to hast.
135135

136136
###### Returns
137137

138-
hast tree ([`HastNode | undefined`][hast-node]).
138+
hast tree ([`HastNode`][hast-node]).
139139

140140
##### Notes
141141

test/definition.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import {toHast} from '../index.js'
55

66
test('definition', async function (t) {
77
await t.test('should ignore `definition`', async function () {
8-
assert.equal(
8+
assert.deepEqual(
99
toHast({
1010
type: 'definition',
1111
identifier: 'alpha',
1212
url: 'bravo'
1313
}),
14-
undefined
14+
{type: 'root', children: []}
1515
)
1616
})
1717

test/footnote-definition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('footnoteDefinition', async function (t) {
1313
{type: 'paragraph', children: [{type: 'text', value: 'bravo'}]}
1414
]
1515
}),
16-
undefined
16+
{type: 'root', children: []}
1717
)
1818
})
1919

test/html.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {toHast} from '../index.js'
44

55
test('HTML', async function (t) {
66
await t.test('should ignore `html`', async function () {
7-
assert.equal(toHast({type: 'html', value: '<alpha></alpha>'}), undefined)
7+
assert.deepEqual(toHast({type: 'html', value: '<alpha></alpha>'}), {
8+
type: 'root',
9+
children: []
10+
})
811
})
912

1013
await t.test(

test/toml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test('toml', async function (t) {
99
// @ts-expect-error: check how a `toml` node is handled (not registered
1010
// normally, but supported here).
1111
toHast({type: 'toml', value: 'alpha'}),
12-
undefined
12+
{type: 'root', children: []}
1313
)
1414
})
1515
})

test/yaml.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {toHast} from '../index.js'
44

55
test('yaml', async function (t) {
66
await t.test('should ignore `yaml`', async function () {
7-
assert.deepEqual(toHast({type: 'yaml', value: 'alpha'}), undefined)
7+
assert.deepEqual(toHast({type: 'yaml', value: 'alpha'}), {
8+
type: 'root',
9+
children: []
10+
})
811
})
912
})

0 commit comments

Comments
 (0)