Skip to content

Commit 5b1c636

Browse files
JounQinwooorm
andauthored
Fix to add MapFunction type back
Closes GH-6. Closes GH-7. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Remco Haszing <[email protected]> Reviewed-by: Titus Wormer <[email protected]> Co-authored-by: Titus Wormer <[email protected]>
1 parent 90e8d93 commit 5b1c636

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
* @typedef {import('unist').Node} Node
33
*/
44

5+
/**
6+
* @template {Node} [Tree=Node]
7+
* @typedef {import('./complex-types.js').MapFunction<Tree>} MapFunction
8+
* Function called with a node, its index, and its parent to produce a new
9+
* node.
10+
*/
11+
512
/**
613
* Create a new tree by mapping all nodes with the given function.
714
*
815
* @template {Node} Tree
916
* Type of input tree.
1017
* @param {Tree} tree
1118
* Tree to map.
12-
* @param {import('./complex-types').MapFunction<Tree>} mapFunction
19+
* @param {MapFunction<Tree>} mapFunction
1320
* Function called with a node, its index, and its parent to produce a new
1421
* node.
1522
* @returns {Tree}

readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ if the original node has children, those are mapped instead.
133133
## Types
134134

135135
This package is fully typed with [TypeScript][].
136-
It exports a type `MapFunction<Tree extends Node = Node>` from
137-
`unist-util-map/complex-types.d.ts` to properly type `MapFunction`s.
136+
It exports a type `MapFunction<Tree extends Node = Node>` to properly type `MapFunction`s.
138137

139138
## Compatibility
140139

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ test('unist-util-map', function (t) {
4646
'should work when passing an empty object'
4747
)
4848

49+
/** @type {Root} */
50+
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
51+
52+
t.deepEqual(
53+
map(tree, asIs),
54+
tree,
55+
'should support an explicitly typed `MapFunction`'
56+
)
57+
4958
t.end()
5059

5160
/**
@@ -69,4 +78,11 @@ test('unist-util-map', function (t) {
6978
function addValue() {
7079
return {value: 'test'}
7180
}
81+
82+
/**
83+
* @type {import('./index.js').MapFunction<Root>}
84+
*/
85+
function asIs(node) {
86+
return node
87+
}
7288
})

0 commit comments

Comments
 (0)