Skip to content

Commit fced791

Browse files
committed
Fix types to allow literal nodes
1 parent 2810a31 commit fced791

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
/**
2-
* @typedef {import('unist').Node} Node
32
* @typedef {import('unist').Parent} Parent
3+
* @typedef {import('unist').Position} Position
4+
* @typedef {import('unist').Node} Node
5+
* @typedef {Record<string, unknown> & {type: string, position?: Position|undefined}} NodeLike
46
*/
57

68
/**
79
* Function called with a node to produce a new node.
810
*
911
* @callback MapFunction
10-
* @param {Node} node Current node being processed
12+
* @param {NodeLike|Node} node Current node being processed
1113
* @param {number} [index] Index of `node`, or `null`
1214
* @param {Parent} [parent] Parent of `node`, or `null`
13-
* @returns {Node} Node to be used in the new tree. Its children are not used: if the original node has children, those are mapped.
15+
* @returns {NodeLike|Node} Node to be used in the new tree. Its children are not used: if the original node has children, those are mapped.
1416
*/
1517

1618
/**
1719
* Unist utility to create a new tree by mapping all nodes with the given function.
1820
*
19-
* @param {Node} tree Tree to map
21+
* @param {NodeLike|Node} tree Tree to map
2022
* @param {MapFunction} iteratee Function that returns a new node
21-
* @returns {Node} New mapped tree.
23+
* @returns {NodeLike|Node} New mapped tree.
2224
*/
2325
export function map(tree, iteratee) {
2426
return preorder(tree, null, null)
2527

2628
/**
27-
* @param {Node} node
29+
* @param {NodeLike|Node} node
2830
* @param {number} [index]
2931
* @param {Parent} [parent]
3032
* @returns {Node}

test.js

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ test('unist-util-map', function (t) {
1313
'should map the specified node'
1414
)
1515

16+
t.deepEqual(
17+
map(
18+
{
19+
type: 'root',
20+
children: [
21+
{type: 'node', children: [{type: 'leaf', value: '1'}]},
22+
{type: 'leaf', value: '2'}
23+
]
24+
},
25+
changeLeaf
26+
),
27+
u('root', [u('node', [u('leaf', 'CHANGED')]), u('leaf', 'CHANGED')]),
28+
'should map the specified node'
29+
)
30+
1631
t.deepEqual(
1732
map(u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')]), nullLeaf),
1833
// @ts-expect-error: not valid but tested anyway.

0 commit comments

Comments
 (0)