5
5
*
6
6
* @typedef Options
7
7
* Configuration (optional).
8
- * @property {boolean } [cascade=true]
8
+ * @property {boolean | null | undefined } [cascade=true]
9
9
* Whether to drop parent nodes if they had children, but all their children
10
10
* were filtered out.
11
11
*/
@@ -27,59 +27,61 @@ const own = {}.hasOwnProperty
27
27
* `unist-util-is`-compatible test (such as a type).
28
28
* @returns
29
29
* New filtered tree.
30
+ *
30
31
* `null` is returned if `tree` itself didn’t pass the test, or is cascaded
31
32
* away.
32
33
*/
33
34
export const filter =
34
35
/**
35
36
* @type {(
36
- * (<Tree extends Node, Check extends Test>(node: Tree, options: Options, test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
37
+ * (<Tree extends Node, Check extends Test>(node: Tree, options: Options | null | undefined , test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
37
38
* (<Tree extends Node, Check extends Test>(node: Tree, test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
38
- * (<Tree extends Node>(node: Tree, options?: Options) => Tree)
39
+ * (<Tree extends Node>(node: Tree, options?: Options | null | undefined ) => Tree)
39
40
* )}
40
41
*/
41
42
(
42
43
/**
43
44
* @param {Node } tree
44
- * @param {Options } options
45
- * @param {Test } test
46
- * @returns {Node| null }
45
+ * @param {Options | Test | null | undefined } [ options]
46
+ * @param {Test | null | undefined } [ test]
47
+ * @returns {Node | null }
47
48
*/
48
49
function ( tree , options , test ) {
49
50
const is = convert ( test || options )
51
+ /** @type {boolean | null | undefined } */
52
+ const cascadeRaw =
53
+ options && typeof options === 'object' && 'cascade' in options
54
+ ? /** @type {boolean | null | undefined } */ ( options . cascade )
55
+ : undefined
50
56
const cascade =
51
- options . cascade === undefined || options . cascade === null
52
- ? true
53
- : options . cascade
57
+ cascadeRaw === undefined || cascadeRaw === null ? true : cascadeRaw
54
58
55
59
return preorder ( tree )
56
60
57
61
/**
58
62
* @param {Node } node
59
- * @param {number|undefined } [index]
60
- * @param {Parent|undefined } [parent]
61
- * @returns {Node|null }
63
+ * Current node.
64
+ * @param {number | undefined } [index]
65
+ * Index of `node` in `parent`.
66
+ * @param {Parent | undefined } [parent]
67
+ * Parent node.
68
+ * @returns {Node | null }
69
+ * Shallow copy of `node`.
62
70
*/
63
71
function preorder ( node , index , parent ) {
64
72
/** @type {Array<Node> } */
65
73
const children = [ ]
66
- /** @type {number } */
67
- let childIndex
68
- /** @type {Node } */
69
- let result
70
- /** @type {string } */
71
- let key
72
74
73
75
if ( ! is ( node , index , parent ) ) return null
74
76
75
77
// @ts -expect-error: Looks like a parent.
76
78
if ( node . children ) {
77
- childIndex = - 1
79
+ let childIndex = - 1
78
80
79
81
// @ts -expect-error Looks like a parent.
80
82
while ( ++ childIndex < node . children . length ) {
81
83
// @ts -expect-error Looks like a parent.
82
- result = preorder ( node . children [ childIndex ] , childIndex , node )
84
+ const result = preorder ( node . children [ childIndex ] , childIndex , node )
83
85
84
86
if ( result ) {
85
87
children . push ( result )
@@ -95,6 +97,8 @@ export const filter =
95
97
/** @type {typeof node } */
96
98
// @ts -expect-error all the fields will be copied over.
97
99
const next = { }
100
+ /** @type {string } */
101
+ let key
98
102
99
103
for ( key in node ) {
100
104
if ( own . call ( node , key ) ) {
0 commit comments