# unist-util-visit-parents 中文文档

规范地址：https://npmcn.com/package/unist-util-visit-parents/

版本：6\.1\.0
npm 元数据更新时间：2026-10-24T17:49:56.714Z
中文译文更新时间：2026-11-05T15:45:15.206Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/unist-util-visit-parents
原文对照：https://npmcn.com/package/unist-util-visit-parents/?lang=original
项目主页：https://github.com/syntax-tree/unist-util-visit-parents#readme
源码仓库：https://github.com/syntax-tree/unist-util-visit-parents

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：62.7 kB，14 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2016-03-25T13:55:33.557Z
- 最新版本发布：2026-10-24T17:49:56.518Z
- 近 30 天下载量：297792201（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：26（近 12 个月 1 个）

## 中文简介

带有祖传信息的反相走过节点的无源工具

## 安装

```sh
npm install unist-util-visit-parents
```

也可以使用：`pnpm add unist-util-visit-parents`、`yarn add unist-util-visit-parents`、`bun add unist-util-visit-parents`

## README

# 非静态访问父母

[![Build](https://github.com/syntax-tree/unist-util-visit-parents/workflows/main/badge.svg)](https://github.com/syntax-tree/unist-util-visit-parents/actions) [![Coverage](https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit-parents.svg)](https://codecov.io/github/syntax-tree/unist-util-visit-parents) [![Downloads](https://img.shields.io/npm/dm/unist-util-visit-parents.svg)](https://www.npmjs.com/package/unist-util-visit-parents) [![Size](https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-visit-parents)](https://bundlejs.com/?q=unist-util-visit-parents) [![Sponsors](https://opencollective.com/unified/sponsors/badge.svg)](https://opencollective.com/unified) [![Backers](https://opencollective.com/unified/backers/badge.svg)](https://opencollective.com/unified) [![Chat](https://img.shields.io/badge/chat-discussions-success.svg)](https://github.com/syntax-tree/unist/discussions)

[无符号](https://github.com/syntax-tree/unist) 用于用一叠父母来行走树.

## 目录

-   [这是什么?](https://npmcn.com/package/unist-util-visit-parents/#readme-what-is-this)
-   [我什么时候用这个?](https://npmcn.com/package/unist-util-visit-parents/#readme-when-should-i-use-this)
-   [安装](https://npmcn.com/package/unist-util-visit-parents/#readme-install)
-   [使用](https://npmcn.com/package/unist-util-visit-parents/#readme-use)
-   [API 密码](https://npmcn.com/package/unist-util-visit-parents/#readme-api)
    -   [`visitParents(tree[, test], enter|visitors[, reverse])`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitparentstree-test-entervisitors-reverse)
    -   [`CONTINUE`](https://npmcn.com/package/unist-util-visit-parents/#readme-continue)
    -   [`EXIT`](https://npmcn.com/package/unist-util-visit-parents/#readme-exit)
    -   [`SKIP`](https://npmcn.com/package/unist-util-visit-parents/#readme-skip)
    -   [`Action`](https://npmcn.com/package/unist-util-visit-parents/#readme-action)
    -   [`ActionTuple`](https://npmcn.com/package/unist-util-visit-parents/#readme-actiontuple)
    -   [`BuildVisitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-buildvisitor)
    -   [`BuildVisitors`](https://npmcn.com/package/unist-util-visit-parents/#readme-buildvisitors)
    -   [`Index`](https://npmcn.com/package/unist-util-visit-parents/#readme-index)
    -   [`Test`](https://npmcn.com/package/unist-util-visit-parents/#readme-test)
    -   [`Visitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitor)
    -   [`VisitorExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorexit)
    -   [`VisitorResult`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorresult)
    -   [`VisitorResultExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorresultexit)
    -   [`Visitors`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitors)
-   [类型](https://npmcn.com/package/unist-util-visit-parents/#readme-types)
-   [兼容性](https://npmcn.com/package/unist-util-visit-parents/#readme-compatibility)
-   [相关](https://npmcn.com/package/unist-util-visit-parents/#readme-related)
-   [贡献](https://npmcn.com/package/unist-util-visit-parents/#readme-contribute)
-   [许可证](https://npmcn.com/package/unist-util-visit-parents/#readme-license)

## 这是什么?

这是与无主合作的非常重要的用途,因为它让你走 树.

## 我什么时候用这个?

想要行走树并想知道情况时可以使用此工具 每个节点的每个家长. 你用得着 [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit) 如果你不介意的话 整个一叠父母.

## 安装

这个软件包是 [仅限无害环境管理](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)。 。 。 . 在Node.js(第16版+)中,安装为 [无](https://docs.npmjs.com/cli/install)数字 :

```sh
npm install unist-util-visit-parents
```

在德诺与 [`esm.sh`](https://esm.sh/)数字 :

```js
import {visitParents} from 'https://esm.sh/unist-util-visit-parents@6'
```

在浏览器中 [`esm.sh`](https://esm.sh/)数字 :

```html
<script type="module">
  import {visitParents} from 'https://esm.sh/unist-util-visit-parents@6?bundle'
</script>
```

## 使用

```js
import {visitParents} from 'unist-util-visit-parents'
import {fromMarkdown} from 'mdast-util-from-markdown'

const tree = fromMarkdown('Some *emphasis*, **strong**, and `code`.')

visitParents(tree, 'strong', function (node, ancestors) {
  console.log(node.type, ancestors.map(ancestor => ancestor.type))
})
```

仪:

```js
strong ['root', 'paragraph']
```

## API 密码

此软件包导出标识符 [`CONTINUE`](https://npmcn.com/package/unist-util-visit-parents/#readme-continue), (中文(简体) ), [`EXIT`](https://npmcn.com/package/unist-util-visit-parents/#readme-exit), (中文(简体) ), [`SKIP`](https://npmcn.com/package/unist-util-visit-parents/#readme-skip),以及 [`visitParents`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitparentstree-test-entervisitors-reverse)。 。 。 . 没有默认导出 .

### `visitParents(tree[, test], enter|visitors[, reverse])`

访问节点,有祖传信息.

此算法执行 _[深度第一个](https://github.com/syntax-tree/unist#depth-first-traversal)_ _[树倒转](https://github.com/syntax-tree/unist#tree-traversal)_ 内 _[预排序](https://github.com/syntax-tree/unist#preorder)_ (单位:千美元)**NLR 语言**)或者,如果 `reverse` 给予, 内 _倒转前序_ (单位:千美元)**无**) (中文(简体) ). 一个 [`exit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitors) 来人了 _[邮购](https://github.com/syntax-tree/unist#postorder)_ (单位:千美元)**LRN 语言**(中文(简体) ) 或如果 `reverse` 以 _倒转后序_ (单位:千美元)**民族解放军**) (中文(简体) ).

您可以通过通过一个 `test`。 。 。 . 对于复杂的测试,你应该测试 自己在访客,因为它将是 更快,并将改进类型信息.

走出这棵树是一项艰巨的任务. 尽可能使用访客的返回值. 而不是走一棵树多次,走一次,使用 [`unist-util-is`](https://github.com/syntax-tree/unist-util-is) 来检查节点是否匹配,然后执行 不同的操作.

汝能更树. 在一个节点内部的改变 当进入它走, 退出时所做的更改. 见 [`Visitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitor) 财务报告和审定财务报表 [`VisitorExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorexit) 为了更多 信息输入.

###### 签署

-   `visitParents(tree[, test], enter[, reverse])`
-   `visitParents(tree[, test], visitors[, reverse])`

###### 参数

-   `tree` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node)(中文(简体) ) - 树到穿行
-   `test` (单位:千美元)[`Test`](https://npmcn.com/package/unist-util-visit-parents/#readme-test),可选) —— 说 [`unist-util-is`](https://github.com/syntax-tree/unist-util-is)\- 兼容测试
-   `enter` (单位:千美元)[`Visitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitor)(中文(简体) ) ——在输入时处理每个节点
-   `visitors` (单位:千美元)[`Visitors`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitors)(中文(简体) ) - 退出时处理每个节点, 在进入时可以任意选择
-   `reverse` (单位:千美元)`boolean`,默认值 : `false`(中文(简体) ) 儿童从最后步行到第一; 页:1 `enter`,倒转前序(NRL)而不是前序(NLR); 页:1 `exit`,倒转后序(RLN)而不是后序(LRN)

###### 回返

无`undefined`) (中文(简体) ).

### `CONTINUE`

继续正常地转弯( E)`true`) (中文(简体) ).

### `EXIT`

立即停止横行(`false`) (中文(简体) ).

### `SKIP`

不要穿过这个节点的孩子(`'skip'`) (中文(简体) ). 只有在进入节点时才有效.

### `Action`

动作类型的结合(TypeScript type).

###### 类型

```ts
type Action = typeof CONTINUE | typeof EXIT | typeof SKIP
```

### `ActionTuple`

列出一个或两个值, 第一个是动作, 第二个是索引 (简体中文).

###### 类型

```ts
type ActionTuple = [
  (Action | null | undefined | void)?,
  (Index | null | undefined)?
]
```

### `BuildVisitor`

构建已键入 `Visitor` 函数来自树和测试(TypeScript类型).

将推断哪些价值观是 `node` 以及作为 `parents`。 。 。 .

###### 类型参数

-   `Tree` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node),默认值 : `Node`(中文(简体) ) 树型
-   `Check` (单位:千美元)[`Test`](https://npmcn.com/package/unist-util-visit-parents/#readme-test),默认值 : `Test`(中文(简体) ) - 测试类型

###### 回返

[`Visitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitor)。 。 。 .

### `BuildVisitors`

构建已输入 [`Visitors`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitors) 从树和测试( TypeScript) 类型).

将推断哪些价值观是 `node` 以及作为 `parents`。 。 。 .

###### 类型参数

-   `Tree` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node),默认值 : `Node`(中文(简体) ) 树型
-   `Check` (单位:千美元)[`Test`](https://npmcn.com/package/unist-util-visit-parents/#readme-test),默认值 : `Test`(中文(简体) ) - 测试类型

###### 回返

[`Visitors`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitors)。 。 。 .

### `Index`

移动到兄弟会 `index` 下一个( 节点本身是完全的) transversed (TypeScript类型).

如果树变异, 如删除节点, 访问者目前有用 或之前的兄弟姐妹们. 成果低于 `0` 大于或等于 `children.length` 停下来 绕道父母.

###### 类型

```ts
type Index = number
```

### `Test`

[`unist-util-is`](https://github.com/syntax-tree/unist-util-is) 兼容测试(TypeScript type).

### `Visitor`

处理节点( 匹配) `test`,如果给出),在孩子之前(TypeScript) 类型).

内部变化 `node` 接下来是步行. 替换或去除在 `exit`。 。 。 . 当替换或移出这里时, 返回时 `SKIP` (否则它的孩子就会被送走), 或者说 `[SKIP, index]` 以继续当前节点 `index`。 。 。 . 换下一位兄弟姐妹(按步行顺序排列)很好; 改变前兄弟姐妹需要一个新的 `Index`。 。 。 . 不支持删除祖先或改变其前兄弟姐妹.

###### 参数

-   `node` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node)(中文(简体) ) ——找到节点
-   `parents` (单位:千美元)[`Array<Node>`](https://github.com/syntax-tree/unist#node)(中文(简体) ) - 祖辈辈辈 `node`
-   `index` (单位:千美元)`number` 或者说 `undefined`(中文(简体) ) ——指数 `node` 在其父母中(其中最后一个) `parents`, (中文(简体) ), 或者说 `undefined` 对于树本身

###### 回返

下一步怎么办[`VisitorResult`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorresult)) (中文(简体) ).

传出一圈回([`ActionTuple`](https://npmcn.com/package/unist-util-visit-parents/#readme-actiontuple)只有这样才有意义 `[SKIP, Index]`。 。 。 .

### `VisitorExit`

处理节点( 匹配) `test`,如果给出)在其子后(TypeScript类型).

内部变化 `node` 没有走路, 使这里成为改变的好地方 `node` 根据其子女, 或替换或删除. 删除时 `node` 或改变以前的兄弟姐妹(按步行顺序排列), 返回一个新的 `Index`。 。 。 . 不支持删除祖先或改变其前兄弟姐妹.

###### 参数

-   `node` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node)(中文(简体) ) ——找到节点
-   `parents` (单位:千美元)[`Array<Node>`](https://github.com/syntax-tree/unist#node)(中文(简体) ) - 祖辈辈辈 `node`
-   `index` (单位:千美元)`number` 或者说 `undefined`(中文(简体) ) ——指数 `node` 在其父母中(其中最后一个) `parents`, (中文(简体) ), 或者说 `undefined` 对于树本身

###### 回返

下一步怎么办[`VisitorResultExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorresultexit)) (中文(简体) ).

`CONTINUE` (单位:千美元)`true`就一直保持正常状态, `EXIT` (单位:千美元)`false`停止横行, 财务报告和审定财务报表 `Index` 继续在那兄弟会.

### `VisitorResult`

输入访问者可以返回的任何值( TypeScript 类型) .

喜欢 [`VisitorResultExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorresultexit) 不过 `SKIP` 可返回到 绝不是结点的子孙所行走的, 财务报告和审定财务报表 [`ActionTuple`](https://npmcn.com/package/unist-util-visit-parents/#readme-actiontuple) 用于将动作和 指数.

###### 类型

```ts
type VisitorResult =
  | Action
  | ActionTuple
  | Index
  | null
  | undefined
  | void
```

### `VisitorResultExit`

从退出访问者(TypeScript类型)返回的任何值.

###### 类型

```ts
type VisitorResultExit = Index | boolean | null | undefined | void
```

### `Visitors`

进出时处理节点(TypeScript类型). 这个 `enter` 访问者是可选的,而 `exit` 需要时间.

通过一个 `enter` 访问者直接只处理输入时的节点.

`enter` 在儿童(如果有的话)步行之前 `exit` 后说. `exit` 仍然叫当 `enter` 返回时 `SKIP`。 。 。 . 两者 `enter` 财务报告和审定财务报表 `exit` 仅需要匹配可选的节点 `test`。 。 。 .

###### 字段

-   `enter` (单位:千美元)[`Visitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitor),可选) - 在孩子面前处理一个节点
-   `exit` (单位:千美元)[`VisitorExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorexit)(中文(简体) ) - 在孩子后处理一个节点

## 类型

此软件包已全部输入 [类型脚本](https://www.typescriptlang.org/)。 。 。 . 出口其他类型 [`Action`](https://npmcn.com/package/unist-util-visit-parents/#readme-action), (中文(简体) ), [`ActionTuple`](https://npmcn.com/package/unist-util-visit-parents/#readme-actiontuple), (中文(简体) ), [`BuildVisitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-buildvisitor), (中文(简体) ), [`BuildVisitors`](https://npmcn.com/package/unist-util-visit-parents/#readme-buildvisitors), (中文(简体) ), [`Index`](https://npmcn.com/package/unist-util-visit-parents/#readme-index), (中文(简体) ), [`Test`](https://npmcn.com/package/unist-util-visit-parents/#readme-test), (中文(简体) ), [`Visitor`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitor), (中文(简体) ), [`VisitorExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorexit), (中文(简体) ), [`VisitorResult`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorresult), (中文(简体) ), [`VisitorResultExit`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitorresultexit),以及 [`Visitors`](https://npmcn.com/package/unist-util-visit-parents/#readme-visitors)。 。 。 .

## 兼容性

由统一集体管理的项目与维持的项目兼容 (原始内容存档于2018-10-21). Executive of Node.js.

当我们剪下新的主要版本时,我们放弃了对未维护版本的支持 节点. 这意味着我们试图保持目前的释放线, `unist-util-visit-parents@^6`,与Node.js 16相兼容.

## 相关

-   [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit) - 和父母一起走树
-   [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter) ——创建一棵有所有节点通过测试的新树
-   [`unist-util-map`](https://github.com/syntax-tree/unist-util-map) ——创建由给定函数映射出所有节点的新树
-   [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap) —— 通过映射( 到数组) 给定函数创建新树
-   [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove) - 从通过测试的树上去掉节点
-   [`unist-util-select`](https://github.com/syntax-tree/unist-util-select) —— 选择带有类似 CSS 选择器的节点

## 贡献

见 [`contributing.md`](https://github.com/syntax-tree/.github/blob/HEAD/contributing.md) 内 [`syntax-tree/.github`](https://github.com/syntax-tree/.github) 页:1 开始的方法. 见 [`support.md`](https://github.com/syntax-tree/.github/blob/HEAD/support.md) 为了寻求帮助.

这个项目有一个 [行为守则](https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md)。 。 。 . 通过与这个寄存器,组织或社区的互动,你同意 遵守它的规定.

## 许可证

[麻省理工学院](https://npmcn.com/package/unist-util-visit-parents/license) © [Titus 虫类](https://wooorm.com/)

## 常见问题

### unist-util-visit-parents 是什么？

带有祖传信息的反相走过节点的无源工具

### 如何安装 unist-util-visit-parents？

在项目目录运行 npm install unist-util-visit-parents，或使用 pnpm add / yarn add / bun add。

### unist-util-visit-parents 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### unist-util-visit-parents 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### unist-util-visit-parents 可以商用吗？

unist-util-visit-parents 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### unist-util-visit-parents 的下载量有多少？

根据 npm 官方下载统计，unist-util-visit-parents 在 2026/10/05 – 2026/11/03 这 30 天内被下载 297,792,201 次，日均约 9,926,407 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### unist-util-visit-parents 还在维护吗？

最新版本 6\.1\.0 发布于 2026/10/25（12 天前）；至今共发布 26 个版本，其中过去 12 个月发布了 1 个；首次发布于 2016/03/25。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

