|
3 | 3 | */
|
4 | 4 |
|
5 | 5 | /**
|
6 |
| - * @template {Node} [Tree=Node] |
7 |
| - * @typedef {import('./complex-types.js').MapFunction<Tree>} MapFunction |
| 6 | + * @template {Node} [Kind=Node] |
| 7 | + * Node type. |
| 8 | + * @typedef {import('./complex-types.js').MapFunction<Kind>} MapFunction |
8 | 9 | * Function called with a node, its index, and its parent to produce a new
|
9 | 10 | * node.
|
10 | 11 | */
|
11 | 12 |
|
12 | 13 | /**
|
13 | 14 | * Create a new tree by mapping all nodes with the given function.
|
14 | 15 | *
|
15 |
| - * @template {Node} Tree |
| 16 | + * @template {Node} Kind |
16 | 17 | * Type of input tree.
|
17 |
| - * @param {Tree} tree |
| 18 | + * @param {Kind} tree |
18 | 19 | * Tree to map.
|
19 |
| - * @param {MapFunction<Tree>} mapFunction |
| 20 | + * @param {MapFunction<Kind>} mapFunction |
20 | 21 | * Function called with a node, its index, and its parent to produce a new
|
21 | 22 | * node.
|
22 |
| - * @returns {Tree} |
| 23 | + * @returns {Kind} |
23 | 24 | * New mapped tree.
|
24 | 25 | */
|
25 | 26 | export function map(tree, mapFunction) {
|
26 | 27 | // @ts-expect-error Looks like a children.
|
27 | 28 | return preorder(tree, null, null)
|
28 | 29 |
|
29 |
| - /** @type {import('./complex-types.js').MapFunction<Tree>} */ |
| 30 | + /** @type {import('./complex-types.js').MapFunction<Kind>} */ |
30 | 31 | function preorder(node, index, parent) {
|
31 |
| - var newNode = Object.assign({}, mapFunction(node, index, parent)) |
| 32 | + const newNode = Object.assign({}, mapFunction(node, index, parent)) |
32 | 33 |
|
33 | 34 | if ('children' in node) {
|
34 | 35 | // @ts-expect-error Looks like a parent.
|
35 | 36 | newNode.children = node.children.map(function (
|
36 |
| - /** @type {import('./complex-types.js').InclusiveDescendant<Tree>} */ child, |
| 37 | + /** @type {import('./complex-types.js').InclusiveDescendant<Kind>} */ child, |
37 | 38 | /** @type {number} */ index
|
38 | 39 | ) {
|
39 | 40 | return preorder(child, index, node)
|
|
0 commit comments