Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: syntax-tree/unist-util-visit
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 4.1.1
Choose a base ref
...
head repository: syntax-tree/unist-util-visit
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4.1.2
Choose a head ref
  • 10 commits
  • 12 files changed
  • 1 contributor

Commits on Jan 24, 2023

  1. Update dev-dependencies

    wooorm committed Jan 24, 2023
    Copy the full SHA
    c0a433b View commit details
  2. Update Actions

    wooorm committed Jan 24, 2023
    Copy the full SHA
    9a2399d View commit details
  3. Update tsconfig.json

    wooorm committed Jan 24, 2023
    Copy the full SHA
    69cbec6 View commit details
  4. Copy the full SHA
    9f4813b View commit details
  5. Refactor code-style

    *   Add more docs to JSDoc
    *   Add support for `null` in input of API types
    wooorm committed Jan 24, 2023
    Copy the full SHA
    754d038 View commit details
  6. Add ignore-scripts to .npmrc

    wooorm committed Jan 24, 2023
    Copy the full SHA
    7b3adaf View commit details
  7. Use Node test runner

    wooorm committed Jan 24, 2023
    Copy the full SHA
    169f938 View commit details
  8. Copy the full SHA
    83df9b3 View commit details
  9. Add improved docs

    wooorm committed Jan 24, 2023
    Copy the full SHA
    332b6e0 View commit details
  10. 4.1.2

    wooorm committed Jan 24, 2023
    Copy the full SHA
    3ef009b View commit details
Showing with 436 additions and 253 deletions.
  1. +4 −4 .github/workflows/main.yml
  2. +1 −1 .gitignore
  3. +1 −0 .npmrc
  4. +2 −54 complex-types.d.ts
  5. +9 −0 index.d.ts
  6. +2 −61 index.js
  7. +8 −1 index.test-d.ts
  8. +182 −0 lib/index.js
  9. +9 −10 package.json
  10. +131 −16 readme.md
  11. +78 −99 test.js
  12. +9 −7 tsconfig.json
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -7,15 +7,15 @@ jobs:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dcodeIO/setup-node-nvm@master
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v3
strategy:
matrix:
node:
- lts/fermium
- lts/gallium
- node
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DS_Store
index.d.ts
lib/index.d.ts
test.d.ts
*.log
coverage/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
ignore-scripts=true
56 changes: 2 additions & 54 deletions complex-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,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'
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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 changes: 2 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,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'
9 changes: 8 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-empty-function */

import {expectError, expectType} from 'tsd'
import {Node, Parent, Literal} from 'unist'
import type {Node, Parent, Literal} from 'unist'
import {is} from 'unist-util-is'
import {visit, SKIP, EXIT, CONTINUE} from './index.js'

@@ -31,6 +31,7 @@ const complexTree: Root = {
]
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Element extends Parent {
type: 'element'
tagName: string
@@ -41,36 +42,42 @@ interface Element extends Parent {

type Content = Flow | Phrasing

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Root extends Parent {
type: 'root'
children: Array<Flow>
}

type Flow = Blockquote | Heading | Paragraph

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Blockquote extends Parent {
type: 'blockquote'
children: Array<Flow>
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Heading extends Parent {
type: 'heading'
depth: number
children: Array<Phrasing>
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Paragraph extends Parent {
type: 'paragraph'
children: Array<Phrasing>
}

type Phrasing = Text | Emphasis

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Emphasis extends Parent {
type: 'emphasis'
children: Array<Phrasing>
}

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Text extends Literal {
type: 'text'
value: string
Loading