unist-util-visit
Advanced tools
Comparing version 4.1.1 to 4.1.2
@@ -1,54 +0,2 @@ | ||
import type {Node, Parent} from 'unist' | ||
import type {Test} from 'unist-util-is' | ||
import type { | ||
VisitorResult, | ||
Matches, | ||
InclusiveDescendant | ||
} from 'unist-util-visit-parents/complex-types.js' | ||
/** | ||
* Called when a node (matching test, if given) is found. | ||
* Visitors are free to transform node. | ||
* They can also transform the parent of node (the last of ancestors). | ||
* Replacing node itself, if `SKIP` is not returned, still causes its descendants to be visited. | ||
* If adding or removing previous siblings (or next siblings, in case of reverse) of node, | ||
* visitor should return a new index (number) to specify the sibling to traverse after node is traversed. | ||
* Adding or removing next siblings of node (or previous siblings, in case of reverse) | ||
* is handled as expected without needing to return a new index. | ||
* Removing the children property of an ancestor still results in them being traversed. | ||
*/ | ||
export type Visitor< | ||
Visited extends Node = Node, | ||
Ancestor extends Parent = Parent | ||
> = ( | ||
node: Visited, | ||
index: Visited extends Node ? number | null : never, | ||
parent: Ancestor extends Node ? Ancestor | null : Ancestor | ||
) => VisitorResult | ||
type ParentsOf< | ||
Ancestor extends Node, | ||
Child extends Node | ||
> = Ancestor extends Parent | ||
? Child extends Ancestor['children'][number] | ||
? Ancestor | ||
: never | ||
: never | ||
type BuildVisitorFromMatch< | ||
Visited extends Node, | ||
Ancestor extends Parent | ||
> = Visitor<Visited, ParentsOf<Ancestor, Visited>> | ||
type BuildVisitorFromDescendants< | ||
Descendant extends Node, | ||
Check extends Test | ||
> = BuildVisitorFromMatch< | ||
Matches<Descendant, Check>, | ||
Extract<Descendant, Parent> | ||
> | ||
export type BuildVisitor< | ||
Tree extends Node = Node, | ||
Check extends Test = string | ||
> = BuildVisitorFromDescendants<InclusiveDescendant<Tree>, Check> | ||
// To do: next major: remove this file. | ||
export type {Visitor, BuildVisitor} from './index.js' |
@@ -1,32 +0,9 @@ | ||
/** | ||
* Visit children of tree which pass test. | ||
* | ||
* @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). | ||
*/ | ||
export const visit: (< | ||
Tree extends import('unist').Node<import('unist').Data>, | ||
Check extends import('unist-util-is').Test | ||
>( | ||
tree: Tree, | ||
test: Check, | ||
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.js').BuildVisitor<Tree_1, string>, | ||
reverse?: boolean | ||
) => void) | ||
export type Node = import('unist').Node | ||
export type Parent = import('unist').Parent | ||
export type Test = import('unist-util-is').Test | ||
export type VisitorResult = import('unist-util-visit-parents').VisitorResult | ||
export type Visitor = import('./complex-types.js').Visitor | ||
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents' | ||
export type {Test} from 'unist-util-is' | ||
export type { | ||
Action, | ||
ActionTuple, | ||
Index, | ||
VisitorResult | ||
} from 'unist-util-visit-parents' | ||
export type {Visitor, BuildVisitor} from './lib/index.js' | ||
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js' |
63
index.js
@@ -1,61 +0,2 @@ | ||
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('unist-util-is').Test} Test | ||
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult | ||
* @typedef {import('./complex-types.js').Visitor} Visitor | ||
*/ | ||
import {visitParents} from 'unist-util-visit-parents' | ||
/** | ||
* Visit children of tree which pass test. | ||
* | ||
* @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). | ||
*/ | ||
export const visit = | ||
/** | ||
* @type {( | ||
* (<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) | ||
* )} | ||
*/ | ||
( | ||
/** | ||
* @param {Node} tree | ||
* @param {Test} test | ||
* @param {import('./complex-types.js').Visitor} visitor | ||
* @param {boolean} [reverse] | ||
*/ | ||
function (tree, test, visitor, reverse) { | ||
if (typeof test === 'function' && typeof visitor !== 'function') { | ||
reverse = visitor | ||
visitor = test | ||
test = null | ||
} | ||
visitParents(tree, test, overload, reverse) | ||
/** | ||
* @param {Node} node | ||
* @param {Array<Parent>} parents | ||
*/ | ||
function overload(node, parents) { | ||
const parent = parents[parents.length - 1] | ||
return visitor( | ||
node, | ||
parent ? parent.children.indexOf(node) : null, | ||
parent | ||
) | ||
} | ||
} | ||
) | ||
export {CONTINUE, EXIT, SKIP} from 'unist-util-visit-parents' | ||
// Note: types exported from `index.d.ts` | ||
export {CONTINUE, EXIT, SKIP, visit} from './lib/index.js' |
{ | ||
"name": "unist-util-visit", | ||
"version": "4.1.1", | ||
"version": "4.1.2", | ||
"description": "unist utility to visit nodes", | ||
@@ -46,2 +46,3 @@ "license": "MIT", | ||
"files": [ | ||
"lib/", | ||
"complex-types.d.ts", | ||
@@ -57,3 +58,3 @@ "index.d.ts", | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
@@ -66,15 +67,13 @@ "mdast-util-from-markdown": "^1.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tsd": "^0.22.0", | ||
"tsd": "^0.25.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.7.0", | ||
"xo": "^0.51.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"{index,test}.d.ts\" && tsc && tsd && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && tsd && 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", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -99,3 +98,3 @@ }, | ||
"plugins": [ | ||
"preset-wooorm" | ||
"remark-preset-wooorm" | ||
] | ||
@@ -102,0 +101,0 @@ }, |
147
readme.md
@@ -21,2 +21,12 @@ # unist-util-visit | ||
* [`visit(tree[, test], visitor[, reverse])`](#visittree-test-visitor-reverse) | ||
* [`CONTINUE`](#continue) | ||
* [`EXIT`](#exit) | ||
* [`SKIP`](#skip) | ||
* [`Action`](#action) | ||
* [`ActionTuple`](#actiontuple) | ||
* [`BuildVisitor`](#buildvisitor) | ||
* [`Index`](#index) | ||
* [`Test`](#test) | ||
* [`Visitor`](#visitor) | ||
* [`VisitorResult`](#visitorresult) | ||
* [Types](#types) | ||
@@ -42,3 +52,3 @@ * [Compatibility](#compatibility) | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]: | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
@@ -52,3 +62,3 @@ ```sh | ||
```js | ||
import {visit} from "https://esm.sh/unist-util-visit@4" | ||
import {visit} from 'https://esm.sh/unist-util-visit@4' | ||
``` | ||
@@ -60,3 +70,3 @@ | ||
<script type="module"> | ||
import {visit} from "https://esm.sh/unist-util-visit@4?bundle" | ||
import {visit} from 'https://esm.sh/unist-util-visit@4?bundle' | ||
</script> | ||
@@ -93,3 +103,4 @@ ``` | ||
This package exports the identifiers `visit`, `CONTINUE`, `SKIP`, and `EXIT`. | ||
This package exports the identifiers [`CONTINUE`][api-continue], | ||
[`EXIT`][api-exit], [`SKIP`][api-skip], and [`visit`][api-visit]. | ||
There is no default export. | ||
@@ -100,20 +111,92 @@ | ||
This function works exactly the same as [`unist-util-visit-parents`][vp], | ||
but `visitor` has a different signature. | ||
but [`Visitor`][api-visitor] has a different signature. | ||
#### `next? = visitor(node, index, parent)` | ||
### `CONTINUE` | ||
Instead of being passed an array of ancestors, `visitor` is called with the | ||
`node`’s [`index`][index] and its [`parent`][parent]. | ||
Continue traversing as normal (`true`). | ||
Please see [`unist-util-visit-parents`][vp]. | ||
### `EXIT` | ||
Stop traversing immediately (`false`). | ||
### `SKIP` | ||
Do not traverse this node’s children (`'skip'`). | ||
### `Action` | ||
Union of the action types (TypeScript type). | ||
See [`Action` in `unist-util-visit-parents`][vp-action]. | ||
### `ActionTuple` | ||
List with an action and an index (TypeScript type). | ||
See [`ActionTuple` in `unist-util-visit-parents`][vp-actiontuple]. | ||
### `BuildVisitor` | ||
Build a typed `Visitor` function from a tree and a test (TypeScript type). | ||
See [`BuildVisitor` in `unist-util-visit-parents`][vp-buildvisitor]. | ||
### `Index` | ||
Move to the sibling at `index` next (TypeScript type). | ||
See [`Index` in `unist-util-visit-parents`][vp-index]. | ||
### `Test` | ||
[`unist-util-is`][unist-util-is] compatible test (TypeScript type). | ||
### `Visitor` | ||
Handle a node (matching `test`, if given) (TypeScript type). | ||
Visitors are free to transform `node`. | ||
They can also transform `parent`. | ||
Replacing `node` itself, if `SKIP` is not returned, still causes its | ||
descendants to be walked (which is a bug). | ||
When adding or removing previous siblings of `node` (or next siblings, in | ||
case of reverse), the `Visitor` should return a new `Index` to specify the | ||
sibling to traverse after `node` is traversed. | ||
Adding or removing next siblings of `node` (or previous siblings, in case | ||
of reverse) is handled as expected without needing to return a new `Index`. | ||
Removing the children property of `parent` still results in them being | ||
traversed. | ||
###### Parameters | ||
* `node` ([`Node`][node]) | ||
— found node | ||
* `index` (`number` or `null`) | ||
— index of `node` in `parent` | ||
* `parent` ([`Node`][node] or `null`) | ||
— parent of `node` | ||
###### Returns | ||
What to do next. | ||
An `Index` is treated as a tuple of `[CONTINUE, Index]`. | ||
An `Action` is treated as a tuple of `[Action]`. | ||
Passing a tuple back only makes sense if the `Action` is `SKIP`. | ||
When the `Action` is `EXIT`, that action can be returned. | ||
When the `Action` is `CONTINUE`, `Index` can be returned. | ||
### `VisitorResult` | ||
Any value that can be returned from a visitor (TypeScript type). | ||
See [`VisitorResult` in `unist-util-visit-parents`][vp-visitorresult]. | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types `Test`, `VisitorResult`, and `Visitor`. | ||
It exports the additional types [`Action`][api-action], | ||
[`ActionTuple`][api-actiontuple], [`BuildVisitor`][api-buildvisitor], | ||
[`Index`][api-index], [`Test`][api-test], [`Visitor`][api-visitor], and | ||
[`VisitorResult`][api-visitorresult]. | ||
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 | ||
@@ -205,6 +288,38 @@ | ||
[node]: https://github.com/syntax-tree/unist#nodes | ||
[unist-util-is]: https://github.com/syntax-tree/unist-util-is | ||
[vp]: https://github.com/syntax-tree/unist-util-visit-parents | ||
[index]: https://github.com/syntax-tree/unist#index | ||
[vp-action]: https://github.com/syntax-tree/unist-util-visit-parents#action | ||
[parent]: https://github.com/syntax-tree/unist#parent-1 | ||
[vp-actiontuple]: https://github.com/syntax-tree/unist-util-visit-parents#actiontuple | ||
[vp-buildvisitor]: https://github.com/syntax-tree/unist-util-visit-parents#buildvisitor | ||
[vp-index]: https://github.com/syntax-tree/unist-util-visit-parents#index | ||
[vp-visitorresult]: https://github.com/syntax-tree/unist-util-visit-parents#visitorresult | ||
[api-visit]: #visittree-test-visitor-reverse | ||
[api-continue]: #continue | ||
[api-exit]: #exit | ||
[api-skip]: #skip | ||
[api-action]: #action | ||
[api-actiontuple]: #actiontuple | ||
[api-buildvisitor]: #buildvisitor | ||
[api-index]: #index | ||
[api-test]: #test | ||
[api-visitor]: #visitor | ||
[api-visitorresult]: #visitorresult |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22810
12
8
322
319
1