Skip to content

Commit 32d3139

Browse files
committed
Change to return undefined, never test tree
1 parent 9e86da1 commit 32d3139

File tree

3 files changed

+36
-54
lines changed

3 files changed

+36
-54
lines changed

lib/index.js

+16-23
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,30 @@ import {convert} from 'unist-util-is'
1717
/**
1818
* Change the given `tree` by removing all nodes that pass `test`.
1919
*
20+
* `tree` itself is never tested.
2021
* The tree is walked in preorder (NLR), visiting the node itself, then its
2122
* head, etc.
2223
*
23-
* @template {Node} Tree
24-
* Node kind.
25-
*
2624
* @overload
27-
* @param {Tree} node
25+
* @param {Node} node
2826
* @param {Test} [test]
29-
* @returns {Tree | undefined}
27+
* @returns {undefined}
3028
*
3129
* @overload
32-
* @param {Tree} node
30+
* @param {Node} node
3331
* @param {Options | null | undefined} options
3432
* @param {Test} [test]
35-
* @returns {Tree | undefined}
33+
* @returns {undefined}
3634
*
37-
* @param {Tree} tree
35+
* @param {Node} tree
3836
* Tree to change.
3937
* @param {Options | Test} options
4038
* Configuration (optional).
4139
* @param {Test} [test]
4240
* `unist-util-is` compatible test.
43-
* @returns {Tree | undefined}
44-
* The given `tree` without nodes that pass `test`.
45-
*
46-
* `undefined` is returned if `tree` itself didn’t pass the test or is
47-
* cascaded away.
41+
* @returns {undefined}
42+
* Nothing.
4843
*/
49-
// To do: next major: don’t return `tree`.
5044
export function remove(tree, options, test) {
5145
const is = convert(test || options)
5246
let cascade = true
@@ -60,21 +54,20 @@ export function remove(tree, options, test) {
6054
cascade = options.cascade
6155
}
6256

63-
return preorder(tree)
57+
preorder(tree)
6458

6559
/**
6660
* Check and remove nodes recursively in preorder.
6761
* For each composite node, modify its children array in-place.
6862
*
69-
* @template {Node} Kind
70-
* @param {Kind} node
63+
* @param {Node} node
7164
* @param {number | undefined} [index]
7265
* @param {Parent | undefined} [parent]
73-
* @returns {Kind | undefined}
66+
* @returns {boolean}
7467
*/
7568
function preorder(node, index, parent) {
76-
if (is(node, index, parent)) {
77-
return undefined
69+
if (node !== tree && is(node, index, parent)) {
70+
return false
7871
}
7972

8073
if ('children' in node && Array.isArray(node.children)) {
@@ -92,15 +85,15 @@ export function remove(tree, options, test) {
9285
}
9386

9487
// Cascade delete.
95-
if (cascade && !newChildIndex) {
96-
return undefined
88+
if (node !== tree && cascade && !newChildIndex) {
89+
return false
9790
}
9891

9992
// Drop other nodes.
10093
children.length = newChildIndex
10194
}
10295
}
10396

104-
return node
97+
return true
10598
}
10699
}

readme.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ There is no default export.
112112

113113
Change the given `tree` by removing all nodes that pass `test`.
114114

115+
`tree` itself is never tested.
115116
The tree is walked in *[preorder][]* (NLR), visiting the node itself, then its
116117
head, etc.
117118

@@ -126,10 +127,7 @@ head, etc.
126127

127128
###### Returns
128129

129-
A changed given `tree`, without nodes that pass `test`.
130-
131-
`undefined` is returned if `tree` itself didn’t pass the test or is cascaded
132-
away.
130+
Nothing (`undefined`).
133131

134132
### `Options`
135133

test.js

+18-27
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ test('remove', async function (t) {
2424
/** @type {Emphasis} */
2525
const tree = u('emphasis', children)
2626

27-
const next = remove(tree, {value: '2'})
27+
remove(tree, {value: '2'})
2828

2929
assert.deepEqual(tree, u('emphasis', [leaf1]))
30-
assert.equal(next, tree)
31-
assert.equal(next.children, children)
32-
assert.equal(next.children[0], leaf1)
3330
})
3431

3532
await t.test('should remove parent nodes', function () {
@@ -40,12 +37,9 @@ test('remove', async function (t) {
4037
/** @type {Root} */
4138
const tree = u('root', children)
4239

43-
const next = remove(tree, test)
40+
remove(tree, test)
4441

4542
assert.deepEqual(tree, u('root', [leaf2]))
46-
assert.equal(next, tree)
47-
assert.equal(next.children, children)
48-
assert.equal(next.children[0], leaf2)
4943

5044
/**
5145
* @param {Node} node
@@ -56,23 +50,25 @@ test('remove', async function (t) {
5650
}
5751
})
5852

59-
await t.test(
60-
'should return `undefined` if root node is removed',
61-
function () {
62-
/** @type {Root} */
63-
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
64-
const next = remove(tree, 'root')
53+
await t.test('should not check root nodes', function () {
54+
/** @type {Root} */
55+
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
6556

66-
assert.equal(next, undefined)
67-
}
68-
)
57+
remove(tree, 'root')
58+
59+
assert.deepEqual(
60+
tree,
61+
u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
62+
)
63+
})
6964

7065
await t.test('should cascade (remove) root nodes', function () {
7166
/** @type {Root} */
7267
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
73-
const next = remove(tree, 'text')
7468

75-
assert.equal(next, undefined)
69+
remove(tree, 'text')
70+
71+
assert.deepEqual(tree, u('root', []))
7672
})
7773

7874
await t.test(
@@ -122,9 +118,8 @@ test('remove', async function (t) {
122118
await t.test('should support `cascade = true`', function () {
123119
/** @type {Root} */
124120
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
125-
const next = remove(tree, {cascade: true}, 'text')
126-
127-
assert.equal(next, undefined)
121+
remove(tree, {cascade: true}, 'text')
122+
assert.deepEqual(tree, u('root', []))
128123
})
129124

130125
await t.test('should support `cascade = false`', function () {
@@ -136,13 +131,9 @@ test('remove', async function (t) {
136131
/** @type {Root} */
137132
const tree = u('root', siblings)
138133

139-
const next = remove(tree, {cascade: false}, 'text')
134+
remove(tree, {cascade: false}, 'text')
140135

141136
assert.deepEqual(tree, u('root', [u('emphasis', [])]))
142-
assert.equal(next, tree)
143-
assert.equal(next.children, siblings)
144-
assert.equal(next.children[0], node)
145-
assert.equal(next.children[0].children, nodeChildren)
146137
})
147138

148139
await t.test('should support the example from readme', function () {

0 commit comments

Comments
 (0)