unist-util-visit
Advanced tools
Comparing version 4.1.0 to 4.1.1
@@ -7,3 +7,3 @@ import type {Node, Parent} from 'unist' | ||
InclusiveDescendant | ||
} from 'unist-util-visit-parents/complex-types' | ||
} from 'unist-util-visit-parents/complex-types.js' | ||
@@ -10,0 +10,0 @@ /** |
/** | ||
* Visit children of tree which pass a test | ||
* Visit children of tree which pass test. | ||
* | ||
* @param tree Abstract syntax tree to walk | ||
* @param test Test, optional | ||
* @param visitor Function to run for each node | ||
* @param reverse Fisit the tree in reverse, defaults to false | ||
* @param tree | ||
* Tree to walk | ||
* @param [test] | ||
* `unist-util-is`-compatible test | ||
* @param visitor | ||
* Function called for nodes that pass `test`. | ||
* @param reverse | ||
* Traverse in reverse preorder (NRL) instead of preorder (NLR) (default). | ||
*/ | ||
@@ -15,9 +19,9 @@ export const visit: (< | ||
test: Check, | ||
visitor: import('./complex-types').BuildVisitor<Tree, Check>, | ||
reverse?: boolean | undefined | ||
visitor: import('./complex-types.js').BuildVisitor<Tree, Check>, | ||
reverse?: boolean | ||
) => void) & | ||
(<Tree_1 extends import('unist').Node<import('unist').Data>>( | ||
tree: Tree_1, | ||
visitor: import('./complex-types').BuildVisitor<Tree_1, string>, | ||
reverse?: boolean | undefined | ||
visitor: import('./complex-types.js').BuildVisitor<Tree_1, string>, | ||
reverse?: boolean | ||
) => void) | ||
@@ -28,6 +32,3 @@ export type Node = import('unist').Node | ||
export type VisitorResult = import('unist-util-visit-parents').VisitorResult | ||
export type Visitor = import('./complex-types').Visitor | ||
import {CONTINUE} from 'unist-util-visit-parents' | ||
import {SKIP} from 'unist-util-visit-parents' | ||
import {EXIT} from 'unist-util-visit-parents' | ||
export {CONTINUE, SKIP, EXIT} | ||
export type Visitor = import('./complex-types.js').Visitor | ||
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents' |
30
index.js
@@ -6,16 +6,18 @@ /** | ||
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult | ||
* @typedef {import('./complex-types').Visitor} Visitor | ||
* @typedef {import('./complex-types.js').Visitor} Visitor | ||
*/ | ||
import {visitParents, CONTINUE, SKIP, EXIT} from 'unist-util-visit-parents' | ||
import {visitParents} from 'unist-util-visit-parents' | ||
export {CONTINUE, SKIP, EXIT} | ||
/** | ||
* Visit children of tree which pass a test | ||
* Visit children of tree which pass test. | ||
* | ||
* @param tree Abstract syntax tree to walk | ||
* @param test Test, optional | ||
* @param visitor Function to run for each node | ||
* @param reverse Fisit the tree in reverse, defaults to false | ||
* @param tree | ||
* Tree to walk | ||
* @param [test] | ||
* `unist-util-is`-compatible test | ||
* @param visitor | ||
* Function called for nodes that pass `test`. | ||
* @param reverse | ||
* Traverse in reverse preorder (NRL) instead of preorder (NLR) (default). | ||
*/ | ||
@@ -25,4 +27,4 @@ export const visit = | ||
* @type {( | ||
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: import('./complex-types').BuildVisitor<Tree, Check>, reverse?: boolean) => void) & | ||
* (<Tree extends Node>(tree: Tree, visitor: import('./complex-types').BuildVisitor<Tree>, reverse?: boolean) => void) | ||
* (<Tree extends Node, Check extends Test>(tree: Tree, test: Check, visitor: import('./complex-types.js').BuildVisitor<Tree, Check>, reverse?: boolean) => void) & | ||
* (<Tree extends Node>(tree: Tree, visitor: import('./complex-types.js').BuildVisitor<Tree>, reverse?: boolean) => void) | ||
* )} | ||
@@ -34,3 +36,3 @@ */ | ||
* @param {Test} test | ||
* @param {import('./complex-types').Visitor} visitor | ||
* @param {import('./complex-types.js').Visitor} visitor | ||
* @param {boolean} [reverse] | ||
@@ -49,3 +51,3 @@ */ | ||
* @param {Node} node | ||
* @param {Array.<Parent>} parents | ||
* @param {Array<Parent>} parents | ||
*/ | ||
@@ -62,1 +64,3 @@ function overload(node, parents) { | ||
) | ||
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents' |
{ | ||
"name": "unist-util-visit", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"description": "unist utility to visit nodes", | ||
@@ -53,3 +53,3 @@ "license": "MIT", | ||
"unist-util-is": "^5.0.0", | ||
"unist-util-visit-parents": "^5.0.0" | ||
"unist-util-visit-parents": "^5.1.1" | ||
}, | ||
@@ -59,13 +59,14 @@ "devDependencies": { | ||
"c8": "^7.0.0", | ||
"mdast-util-from-markdown": "^1.0.0", | ||
"mdast-util-gfm": "^2.0.0", | ||
"micromark-extension-gfm": "^2.0.0", | ||
"prettier": "^2.0.0", | ||
"remark": "^14.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-gfm": "^2.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tsd": "^0.17.0", | ||
"tsd": "^0.22.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.44.0" | ||
"typescript": "^4.7.0", | ||
"xo": "^0.51.0" | ||
}, | ||
@@ -89,3 +90,7 @@ "scripts": { | ||
"xo": { | ||
"prettier": true | ||
"prettier": true, | ||
"rules": { | ||
"@typescript-eslint/ban-types": "off", | ||
"@typescript-eslint/array-type": "off" | ||
} | ||
}, | ||
@@ -92,0 +97,0 @@ "remarkConfig": { |
113
readme.md
@@ -11,11 +11,34 @@ # unist-util-visit | ||
[**unist**][unist] utility to visit nodes. | ||
[unist][] utility to walk the tree. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`visit(tree[, test], visitor[, reverse])`](#visittree-test-visitor-reverse) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This is a very important utility for working with unist as it lets you walk the | ||
tree. | ||
## When should I use this? | ||
You can use this utility when you want to walk the tree. | ||
You can use [`unist-util-visit-parents`][vp] if you care about the entire stack | ||
of parents. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -25,2 +48,16 @@ npm install unist-util-visit | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {visit} from "https://esm.sh/unist-util-visit@4" | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {visit} from "https://esm.sh/unist-util-visit@4?bundle" | ||
</script> | ||
``` | ||
## Use | ||
@@ -47,17 +84,10 @@ | ||
```js | ||
{ type: 'leaf', value: '1' } | ||
{ type: 'leaf', value: '2' } | ||
{ type: 'leaf', value: '3' } | ||
{type: 'leaf', value: '1'} | ||
{type: 'leaf', value: '2'} | ||
{type: 'leaf', value: '3'} | ||
``` | ||
Note: this example also uses `unist-builder`, to run the example ensure both `unist-builder` and `unist-util-visit` are installed: | ||
```sh | ||
npm install unist-builder unist-util-visit | ||
``` | ||
## API | ||
This package exports the following identifiers: `visit`, `CONTINUE`, `SKIP`, and | ||
`EXIT`. | ||
This package exports the identifiers `visit`, `CONTINUE`, `SKIP`, and `EXIT`. | ||
There is no default export. | ||
@@ -73,26 +103,41 @@ | ||
Instead of being passed an array of ancestors, `visitor` is called with the | ||
`node`’s [`index`][index] and its [`parent`][parent]. The optional return value | ||
`next` is documented in [`unist-util-visit-parents`][vp]’s readme. | ||
`node`’s [`index`][index] and its [`parent`][parent]. | ||
Otherwise the same as [`unist-util-visit-parents`][vp]. | ||
Please see [`unist-util-visit-parents`][vp]. | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types `Test`, `VisitorResult`, and `Visitor`. | ||
It also exports the types `BuildVisitor<Tree extends Node = Node, Check extends | ||
Test = string>` to properly type visitors from a tree and a test, from | ||
`unist-util-visit-parents/complex-types.d.ts`. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Related | ||
* [`unist-util-visit-parents`][vp] | ||
— Like `visit`, but with a stack of parents | ||
— walk the tree with a stack of parents | ||
* [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter) | ||
— Create a new tree with all nodes that pass a test | ||
— create a new tree with all nodes that pass a test | ||
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map) | ||
— Create a new tree with all nodes mapped by a given function | ||
— create a new tree with all nodes mapped by a given function | ||
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap) | ||
— Create a new tree by mapping (to an array) with the given function | ||
— create a new tree by mapping (to an array) with the given function | ||
* [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove) | ||
— Remove nodes from a tree that pass a test | ||
— remove nodes from a tree that pass a test | ||
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select) | ||
— Select nodes with CSS-like selectors | ||
— select nodes with CSS-like selectors | ||
## Contribute | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -138,2 +183,8 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -143,8 +194,10 @@ | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[health]: https://github.com/syntax-tree/.github | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
[unist]: https://github.com/syntax-tree/unist | ||
@@ -151,0 +204,0 @@ |
13906
137
204
14