Skip to content

Improve generic types #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
*.d.ts
index.d.ts
test.d.ts
*.log
coverage/
node_modules/
Expand Down
23 changes: 23 additions & 0 deletions complex-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {Node, Parent} from 'unist'

/**
* Internal utility to collect all descendants of in `Tree`.
* @see https://github.com/syntax-tree/unist-util-visit-parents/blob/18d36ad/complex-types.d.ts#L43
*/
export type InclusiveDescendant<
Tree extends Node = never,
Found = void
> = Tree extends Parent
?
| Tree
| InclusiveDescendant<
Exclude<Tree['children'][number], Found | Tree>,
Found | Tree
>
: Tree

export type MapFunction<Tree extends Node = Node> = (
node: InclusiveDescendant<Tree>,
index: number | null,
parent: InclusiveDescendant<Tree> | null
) => InclusiveDescendant<Tree>
31 changes: 7 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
/**
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Position} Position
* @typedef {import('unist').Node} Node
* @typedef {Record<string, unknown> & {type: string, position?: Position|undefined}} NodeLike
*/

/**
* Function called with a node to produce a new node.
*
* @callback MapFunction
* @param {NodeLike|Node} node Current node being processed
* @param {number} [index] Index of `node`, or `null`
* @param {Parent} [parent] Parent of `node`, or `null`
* @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.
*/

/**
* Unist utility to create a new tree by mapping all nodes with the given function.
*
* @param {NodeLike|Node} tree Tree to map
* @param {MapFunction} iteratee Function that returns a new node
* @returns {NodeLike|Node} New mapped tree.
* @template {Node} Tree
* @param {Tree} tree Tree to map
* @param {import('./complex-types').MapFunction<Tree>} iteratee Function that returns a new node
* @returns {Tree} New mapped tree.
*/
export function map(tree, iteratee) {
// @ts-expect-error Looks like a children.
return preorder(tree, null, null)

/**
* @param {NodeLike|Node} node
* @param {number} [index]
* @param {Parent} [parent]
* @returns {Node}
*/
/** @type {import('./complex-types').MapFunction<Tree>} */
function preorder(node, index, parent) {
var newNode = Object.assign({}, iteratee(node, index, parent))

if ('children' in node) {
// @ts-expect-error Looks like a parent.
newNode.children = node.children.map(function (
/** @type {Node} */ child,
/** @type {import('./complex-types').InclusiveDescendant<Tree>} */ child,
/** @type {number} */ index
) {
// @ts-expect-error Looks like a parent.
return preorder(child, index, node)
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"build": "rimraf \"{index,test}.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
Expand Down
18 changes: 11 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {{type: 'leaf', value: string}} Leaf
* @typedef {{type: 'node', children: Array<Node | Leaf>}} Node
* @typedef {{type: 'root', children: Array<Node | Leaf>}} Root
* @typedef {Root | Node | Leaf} AnyNode
*/

import test from 'tape'
Expand All @@ -15,13 +18,14 @@ test('unist-util-map', function (t) {

t.deepEqual(
map(
{
/** @type {Root} */
({
type: 'root',
children: [
{type: 'node', children: [{type: 'leaf', value: '1'}]},
{type: 'leaf', value: '2'}
]
},
}),
changeLeaf
),
u('root', [u('node', [u('leaf', 'CHANGED')]), u('leaf', 'CHANGED')]),
Expand All @@ -45,8 +49,8 @@ test('unist-util-map', function (t) {
t.end()

/**
* @param {Node} node
* @returns {Node}
* @param {AnyNode} node
* @returns {AnyNode}
*/
function changeLeaf(node) {
return node.type === 'leaf'
Expand All @@ -55,8 +59,8 @@ test('unist-util-map', function (t) {
}

/**
* @param {Node} node
* @returns {Node?}
* @param {AnyNode} node
* @returns {AnyNode?}
*/
function nullLeaf(node) {
return node.type === 'leaf' ? null : node
Expand Down