@songtonyli/dsh-cli 0.1.84 → 0.1.86

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/README.md +1 -1
  2. package/bin/dsh.mjs +1 -1
  3. package/node_modules/@deepseek-ai/dsh-client-modules/lib/index.js +1 -1
  4. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
  5. package/node_modules/@deepseek-ai/dsh-experimental-tool-agent-team/README.i18n.yaml +14 -14
  6. package/node_modules/@deepseek-ai/dsh-experimental-tool-agent-team/README.md +7 -7
  7. package/node_modules/@deepseek-ai/dsh-experimental-tool-agent-team/README.zh.md +7 -7
  8. package/node_modules/@deepseek-ai/dsh-experimental-tool-agent-team/lib/index.js +28 -4
  9. package/node_modules/@deepseek-ai/dsh-llm-pi-ai/lib/index.js +39 -52
  10. package/node_modules/@deepseek-ai/dsh-subagent/README.i18n.yaml +4 -4
  11. package/node_modules/@deepseek-ai/dsh-subagent/README.md +3 -3
  12. package/node_modules/@deepseek-ai/dsh-subagent/README.zh.md +3 -3
  13. package/node_modules/@deepseek-ai/dsh-subagent/lib/index.js +8 -5
  14. package/node_modules/@deepseek-ai/dsh-subagent/lib/types/child-agent.js +9 -4
  15. package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +10 -10
  16. package/node_modules/@deepseek-ai/dsh-tui-app/README.md +16 -11
  17. package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +16 -11
  18. package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +1938 -1409
  19. package/node_modules/execa/package.json +1 -1
  20. package/node_modules/unist-util-visit/lib/index.js +173 -161
  21. package/node_modules/unist-util-visit/license +1 -1
  22. package/node_modules/unist-util-visit/package.json +46 -56
  23. package/node_modules/unist-util-visit/readme.md +87 -27
  24. package/node_modules/unist-util-visit-parents/lib/color.js +4 -0
  25. package/node_modules/unist-util-visit-parents/lib/color.node.js +4 -0
  26. package/node_modules/unist-util-visit-parents/lib/index.js +235 -52
  27. package/node_modules/unist-util-visit-parents/license +1 -1
  28. package/node_modules/unist-util-visit-parents/package.json +44 -54
  29. package/node_modules/unist-util-visit-parents/readme.md +185 -74
  30. package/package.json +4 -4
@@ -14,17 +14,21 @@
14
14
  * [Install](#install)
15
15
  * [Use](#use)
16
16
  * [API](#api)
17
- * [`visit(tree[, test], visitor[, reverse])`](#visittree-test-visitor-reverse)
17
+ * [`visit(tree[, test], enter|visitors[, reverse])`](#visittree-test-entervisitors-reverse)
18
18
  * [`CONTINUE`](#continue)
19
19
  * [`EXIT`](#exit)
20
20
  * [`SKIP`](#skip)
21
21
  * [`Action`](#action)
22
22
  * [`ActionTuple`](#actiontuple)
23
23
  * [`BuildVisitor`](#buildvisitor)
24
+ * [`BuildVisitors`](#buildvisitors)
24
25
  * [`Index`](#index)
25
26
  * [`Test`](#test)
26
27
  * [`Visitor`](#visitor)
28
+ * [`VisitorExit`](#visitorexit)
27
29
  * [`VisitorResult`](#visitorresult)
30
+ * [`VisitorResultExit`](#visitorresultexit)
31
+ * [`Visitors`](#visitors)
28
32
  * [Compatibility](#compatibility)
29
33
  * [Related](#related)
30
34
  * [Contribute](#contribute)
@@ -100,17 +104,27 @@ It exports the [TypeScript][] types
100
104
  [`ActionTuple`][api-action-tuple],
101
105
  [`Action`][api-action],
102
106
  [`BuildVisitor`][api-build-visitor],
107
+ [`BuildVisitors`][api-build-visitors],
103
108
  [`Index`][api-index],
104
109
  [`Test`][api-test],
105
- [`VisitorResult`][api-visitor-result], and
110
+ [`VisitorExit`][api-visitor-exit],
111
+ [`VisitorResultExit`][api-visitor-result-exit],
112
+ [`VisitorResult`][api-visitor-result],
113
+ [`Visitors`][api-visitors], and
106
114
  [`Visitor`][api-visitor].
107
115
  There is no default export.
108
116
 
109
- ### `visit(tree[, test], visitor[, reverse])`
117
+ ### `visit(tree[, test], enter|visitors[, reverse])`
110
118
 
111
119
  This function works exactly the same as
112
120
  [`unist-util-visit-parents`][github-vp],
113
- but [`Visitor`][api-visitor] has a different signature.
121
+ but [`Visitor`][api-visitor] and [`VisitorExit`][api-visitor-exit] have a
122
+ different signature.
123
+
124
+ ###### Signatures
125
+
126
+ * `visit(tree[, test], enter[, reverse])`
127
+ * `visit(tree[, test], visitors[, reverse])`
114
128
 
115
129
  ### `CONTINUE`
116
130
 
@@ -139,6 +153,12 @@ See [`ActionTuple` in `unist-util-visit-parents`][github-vp-action-tuple].
139
153
  Build a typed `Visitor` function from a tree and a test (TypeScript type).
140
154
  See [`BuildVisitor` in `unist-util-visit-parents`][github-vp-build-visitor].
141
155
 
156
+ ### `BuildVisitors`
157
+
158
+ Build typed [`Visitors`][api-visitors] from a tree and a test (TypeScript
159
+ type).
160
+ See [`BuildVisitors` in `unist-util-visit-parents`][github-vp-build-visitors].
161
+
142
162
  ### `Index`
143
163
 
144
164
  Move to the sibling at `index` next (TypeScript type).
@@ -151,42 +171,46 @@ See [`Index` in `unist-util-visit-parents`][github-vp-index].
151
171
 
152
172
  ### `Visitor`
153
173
 
154
- Handle a node (matching `test`, if given) (TypeScript type).
174
+ Handle a node (matching `test`, if given) before its children (TypeScript
175
+ type).
176
+ Like [`Visitor` in `unist-util-visit-parents`][github-vp-visitor],
177
+ but with `index` and `parent` instead of `parents`.
155
178
 
156
- Visitors are free to transform `node`.
157
- They can also transform `parent`.
179
+ ###### Parameters
158
180
 
159
- Replacing `node` itself, if `SKIP` is not returned, still causes its
160
- descendants to be walked (which is a bug).
181
+ * `node` ([`Node`][github-unist-node])
182
+ — found node
183
+ * `index` (`number` or `undefined`)
184
+ — index of `node` in `parent`,
185
+ or `undefined` for the tree itself
186
+ * `parent` ([`Node`][github-unist-node] or `undefined`)
187
+ — parent of `node`,
188
+ or `undefined` for the tree itself
189
+
190
+ ###### Returns
191
+
192
+ What to do next ([`VisitorResult`][api-visitor-result]).
161
193
 
162
- When adding or removing previous siblings of `node` (or next siblings, in
163
- case of reverse), the `Visitor` should return a new `Index` to specify the
164
- sibling to traverse after `node` is traversed.
165
- Adding or removing next siblings of `node` (or previous siblings, in case
166
- of reverse) is handled as expected without needing to return a new `Index`.
194
+ ### `VisitorExit`
167
195
 
168
- Removing the children property of `parent` still results in them being
169
- traversed.
196
+ Handle a node (matching `test`, if given) after its children (TypeScript type).
197
+ Like [`VisitorExit` in `unist-util-visit-parents`][github-vp-visitor-exit],
198
+ but with `index` and `parent` instead of `parents`.
170
199
 
171
200
  ###### Parameters
172
201
 
173
202
  * `node` ([`Node`][github-unist-node])
174
203
  — found node
175
204
  * `index` (`number` or `undefined`)
176
- — index of `node` in `parent`
205
+ — index of `node` in `parent`,
206
+ or `undefined` for the tree itself
177
207
  * `parent` ([`Node`][github-unist-node] or `undefined`)
178
- — parent of `node`
208
+ — parent of `node`,
209
+ or `undefined` for the tree itself
179
210
 
180
211
  ###### Returns
181
212
 
182
- What to do next.
183
-
184
- An `Index` is treated as a tuple of `[CONTINUE, Index]`.
185
- An `Action` is treated as a tuple of `[Action]`.
186
-
187
- Passing a tuple back only makes sense if the `Action` is `SKIP`.
188
- When the `Action` is `EXIT`, that action can be returned.
189
- When the `Action` is `CONTINUE`, `Index` can be returned.
213
+ What to do next ([`VisitorResultExit`][api-visitor-result-exit]).
190
214
 
191
215
  ### `VisitorResult`
192
216
 
@@ -194,6 +218,24 @@ Any value that can be returned from a visitor (TypeScript type).
194
218
  See [`VisitorResult` in
195
219
  `unist-util-visit-parents`][github-vp-visitor-result].
196
220
 
221
+ ### `VisitorResultExit`
222
+
223
+ Any value that can be returned from an exit visitor (TypeScript type).
224
+ See [`VisitorResultExit` in
225
+ `unist-util-visit-parents`][github-vp-visitor-result-exit].
226
+
227
+ ### `Visitors`
228
+
229
+ Handle nodes when entering and exiting them (TypeScript type).
230
+ See [`Visitors` in `unist-util-visit-parents`][github-vp-visitors].
231
+
232
+ ###### Fields
233
+
234
+ * `enter` ([`Visitor`][api-visitor], optional)
235
+ — handle a node before its children
236
+ * `exit` ([`VisitorExit`][api-visitor-exit])
237
+ — handle a node after its children
238
+
197
239
  ## Compatibility
198
240
 
199
241
  Projects maintained by the unified collective are compatible with maintained
@@ -242,6 +284,8 @@ or community you agree to abide by its terms.
242
284
 
243
285
  [api-build-visitor]: #buildvisitor
244
286
 
287
+ [api-build-visitors]: #buildvisitors
288
+
245
289
  [api-continue]: #continue
246
290
 
247
291
  [api-exit]: #exit
@@ -252,12 +296,18 @@ or community you agree to abide by its terms.
252
296
 
253
297
  [api-test]: #test
254
298
 
255
- [api-visit]: #visittree-test-visitor-reverse
299
+ [api-visit]: #visittree-test-entervisitors-reverse
256
300
 
257
301
  [api-visitor]: #visitor
258
302
 
303
+ [api-visitor-exit]: #visitorexit
304
+
259
305
  [api-visitor-result]: #visitorresult
260
306
 
307
+ [api-visitor-result-exit]: #visitorresultexit
308
+
309
+ [api-visitors]: #visitors
310
+
261
311
  [badge-build-image]: https://github.com/syntax-tree/unist-util-visit/workflows/main/badge.svg
262
312
 
263
313
  [badge-build-url]: https://github.com/syntax-tree/unist-util-visit/actions
@@ -294,10 +344,20 @@ or community you agree to abide by its terms.
294
344
 
295
345
  [github-vp-build-visitor]: https://github.com/syntax-tree/unist-util-visit-parents#buildvisitor
296
346
 
347
+ [github-vp-build-visitors]: https://github.com/syntax-tree/unist-util-visit-parents#buildvisitors
348
+
297
349
  [github-vp-index]: https://github.com/syntax-tree/unist-util-visit-parents#index
298
350
 
351
+ [github-vp-visitor]: https://github.com/syntax-tree/unist-util-visit-parents#visitor
352
+
353
+ [github-vp-visitor-exit]: https://github.com/syntax-tree/unist-util-visit-parents#visitorexit
354
+
299
355
  [github-vp-visitor-result]: https://github.com/syntax-tree/unist-util-visit-parents#visitorresult
300
356
 
357
+ [github-vp-visitor-result-exit]: https://github.com/syntax-tree/unist-util-visit-parents#visitorresultexit
358
+
359
+ [github-vp-visitors]: https://github.com/syntax-tree/unist-util-visit-parents#visitors
360
+
301
361
  [health]: https://github.com/syntax-tree/.github
302
362
 
303
363
  [health-coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
@@ -1,6 +1,10 @@
1
1
  /**
2
+ * Color a string.
3
+ *
2
4
  * @param {string} d
5
+ * Value.
3
6
  * @returns {string}
7
+ * Colored value.
4
8
  */
5
9
  export function color(d) {
6
10
  return d
@@ -1,6 +1,10 @@
1
1
  /**
2
+ * Color a string.
3
+ *
2
4
  * @param {string} d
5
+ * Value.
3
6
  * @returns {string}
7
+ * Colored value.
4
8
  */
5
9
  export function color(d) {
6
10
  return '\u001B[33m' + d + '\u001B[39m'
@@ -167,46 +167,98 @@
167
167
  * List with one or two values, the first an action, the second an index.
168
168
  *
169
169
  * @typedef {Action | ActionTuple | Index | null | undefined | void} VisitorResult
170
- * Any value that can be returned from a visitor.
170
+ * Any value that can be returned from an enter visitor.
171
+ *
172
+ * Like `VisitorResultExit`,
173
+ * but `SKIP` can also be returned,
174
+ * to not walk the children of the node,
175
+ * and `ActionTuple` can be used to combine an action with an index.
176
+ *
177
+ * @typedef {Index | boolean | null | undefined | void} VisitorResultExit
178
+ * Any value that can be returned from an exit visitor.
171
179
  */
172
180
 
173
181
  /**
174
182
  * @callback Visitor
175
- * Handle a node (matching `test`, if given).
176
- *
177
- * Visitors are free to transform `node`.
178
- * They can also transform the parent of node (the last of `ancestors`).
179
- *
180
- * Replacing `node` itself, if `SKIP` is not returned, still causes its
181
- * descendants to be walked (which is a bug).
182
- *
183
- * When adding or removing previous siblings of `node` (or next siblings, in
184
- * case of reverse), the `Visitor` should return a new `Index` to specify the
185
- * sibling to traverse after `node` is traversed.
186
- * Adding or removing next siblings of `node` (or previous siblings, in case
187
- * of reverse) is handled as expected without needing to return a new `Index`.
183
+ * Handle a node (matching `test`, if given) before its children.
188
184
  *
189
- * Removing the children property of an ancestor still results in them being
190
- * traversed.
185
+ * Changes inside `node` are walked next.
186
+ * Replacing or removing it is easier in `exit`.
187
+ * When replacing or removing it here,
188
+ * return `SKIP` (as otherwise its old children are walked),
189
+ * or `[SKIP, index]` to continue with the node now at `index`.
190
+ * Changing next siblings (in walk order) is fine;
191
+ * changing previous siblings needs a new `Index`.
192
+ * Removing an ancestor or changing its previous siblings is not supported.
191
193
  * @param {Visited} node
192
194
  * Found node.
193
195
  * @param {Array<VisitedParents>} ancestors
194
196
  * Ancestors of `node`.
197
+ * @param {number | undefined} index
198
+ * Index of `node` in its parent (the last of `ancestors`),
199
+ * or `undefined` for the tree itself.
195
200
  * @returns {VisitorResult}
196
201
  * What to do next.
197
202
  *
198
- * An `Index` is treated as a tuple of `[CONTINUE, Index]`.
199
- * An `Action` is treated as a tuple of `[Action]`.
203
+ * Like `VisitorExit`,
204
+ * but `SKIP` can also be returned,
205
+ * to not walk the children of `node`.
206
+ * Passing a tuple back (`ActionTuple`) only makes sense as `[SKIP, Index]`.
207
+ * @template {UnistNode} [Visited=UnistNode]
208
+ * Visited node type.
209
+ * @template {UnistParent} [VisitedParents=UnistParent]
210
+ * Ancestor type.
211
+ */
212
+
213
+ /**
214
+ * @callback VisitorExit
215
+ * Handle a node (matching `test`, if given) after its children.
216
+ *
217
+ * Changes inside `node` are not walked,
218
+ * which makes this a good place to change `node` based on its children,
219
+ * or to replace or remove it.
220
+ * When removing `node` or changing previous siblings (in walk order),
221
+ * return a new `Index`.
222
+ * Removing an ancestor or changing its previous siblings is not supported.
223
+ * @param {Visited} node
224
+ * Found node.
225
+ * @param {Array<VisitedParents>} ancestors
226
+ * Ancestors of `node`.
227
+ * @param {number | undefined} index
228
+ * Index of `node` in its parent (the last of `ancestors`),
229
+ * or `undefined` for the tree itself.
230
+ * @returns {VisitorResultExit}
231
+ * What to do next.
200
232
  *
201
- * Passing a tuple back only makes sense if the `Action` is `SKIP`.
202
- * When the `Action` is `EXIT`, that action can be returned.
203
- * When the `Action` is `CONTINUE`, `Index` can be returned.
233
+ * `CONTINUE` (`true`, nothing) continues as normal,
234
+ * `EXIT` (`false`) stops traversing,
235
+ * and `Index` continues at that sibling.
204
236
  * @template {UnistNode} [Visited=UnistNode]
205
237
  * Visited node type.
206
238
  * @template {UnistParent} [VisitedParents=UnistParent]
207
239
  * Ancestor type.
208
240
  */
209
241
 
242
+ /**
243
+ * @template {UnistNode} [Visited=UnistNode]
244
+ * Visited node type.
245
+ * @template {UnistParent} [VisitedParents=UnistParent]
246
+ * Ancestor type.
247
+ * @typedef Visitors
248
+ * Handle nodes when entering and exiting them.
249
+ * The `enter` visitor is optional while `exit` is required.
250
+ *
251
+ * Pass an `enter` visitor directly to only handle nodes when entering them.
252
+ *
253
+ * `enter` is called before children (if any) are walked and `exit` after that.
254
+ * `exit` is still called when `enter` returns `SKIP`.
255
+ * Both `enter` and `exit` are only called for nodes that match an optional `test`.
256
+ * @property {Visitor<Visited, VisitedParents> | null | undefined} [enter]
257
+ * Handle a node before its children (optional).
258
+ * @property {VisitorExit<Visited, VisitedParents>} exit
259
+ * Handle a node after its children.
260
+ */
261
+
210
262
  /**
211
263
  * @typedef {Visitor<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitor
212
264
  * Build a typed `Visitor` function from a tree and a test.
@@ -218,6 +270,17 @@
218
270
  * Test type.
219
271
  */
220
272
 
273
+ /**
274
+ * @typedef {Visitors<Matches<InclusiveDescendant<Tree>, Check>, Ancestor<Tree, Matches<InclusiveDescendant<Tree>, Check>>>} BuildVisitors
275
+ * Build typed `Visitors` from a tree and a test.
276
+ *
277
+ * It will infer which values are passed as `node` and which as `parents`.
278
+ * @template {UnistNode} [Tree=UnistNode]
279
+ * Tree type.
280
+ * @template {Test} [Check=Test]
281
+ * Test type.
282
+ */
283
+
221
284
  import {convert} from 'unist-util-is'
222
285
  import {color} from 'unist-util-visit-parents/do-not-use-color'
223
286
 
@@ -236,6 +299,7 @@ export const EXIT = false
236
299
 
237
300
  /**
238
301
  * Do not traverse this node’s children.
302
+ * Only works when entering a node.
239
303
  */
240
304
  export const SKIP = 'skip'
241
305
 
@@ -243,10 +307,13 @@ export const SKIP = 'skip'
243
307
  * Visit nodes, with ancestral information.
244
308
  *
245
309
  * This algorithm performs *depth-first* *tree traversal* in *preorder*
246
- * (**NLR**) or if `reverse` is given, in *reverse preorder* (**NRL**).
310
+ * (**NLR**) or if `reverse` is given,
311
+ * in *reverse preorder* (**NRL**).
312
+ * An `exit` visitor is called in *postorder* (**LRN**)
313
+ * or if `reverse` is given, in *reverse postorder* (**RLN**).
247
314
  *
248
- * You can choose for which nodes `visitor` is called by passing a `test`.
249
- * For complex tests, you should test yourself in `visitor`, as it will be
315
+ * You can choose which nodes are visited by passing a `test`.
316
+ * For complex tests, you should test yourself in a visitor, as it will be
250
317
  * faster and will have improved type information.
251
318
  *
252
319
  * Walking the tree is an intensive task.
@@ -255,29 +322,72 @@ export const SKIP = 'skip'
255
322
  * to check if a node matches, and then perform different operations.
256
323
  *
257
324
  * You can change the tree.
258
- * See `Visitor` for more info.
325
+ * Changes inside a node made when entering it are walked,
326
+ * changes made when exiting it are not.
327
+ * See `Visitor` and `VisitorExit` for more info.
259
328
  *
260
329
  * @overload
261
330
  * @param {Tree} tree
331
+ * Tree to traverse.
332
+ * @param {Check} check
333
+ * `unist-util-is`-compatible test.
334
+ * @param {BuildVisitor<Tree, Check>} enter
335
+ * Handle each node when entering it.
336
+ * @param {boolean | null | undefined} [reverse]
337
+ * Walk children from last to first;
338
+ * for `enter`, reverse preorder (NRL) instead of preorder (NLR);
339
+ * for `exit`, reverse postorder (RLN) instead of postorder (LRN).
340
+ * @returns {undefined}
341
+ * Nothing.
342
+ *
343
+ * @overload
344
+ * @param {Tree} tree
345
+ * Tree to traverse.
262
346
  * @param {Check} check
263
- * @param {BuildVisitor<Tree, Check>} visitor
347
+ * `unist-util-is`-compatible test.
348
+ * @param {BuildVisitors<Tree, Check>} visitors
349
+ * Handle each node when exiting it, and optionally when entering it.
350
+ * @param {boolean | null | undefined} [reverse]
351
+ * Walk children from last to first;
352
+ * for `enter`, reverse preorder (NRL) instead of preorder (NLR);
353
+ * for `exit`, reverse postorder (RLN) instead of postorder (LRN).
354
+ * @returns {undefined}
355
+ * Nothing.
356
+ *
357
+ * @overload
358
+ * @param {Tree} tree
359
+ * Tree to traverse.
360
+ * @param {BuildVisitor<Tree>} enter
361
+ * Handle each node when entering it.
264
362
  * @param {boolean | null | undefined} [reverse]
363
+ * Walk children from last to first;
364
+ * for `enter`, reverse preorder (NRL) instead of preorder (NLR);
365
+ * for `exit`, reverse postorder (RLN) instead of postorder (LRN).
265
366
  * @returns {undefined}
367
+ * Nothing.
266
368
  *
267
369
  * @overload
268
370
  * @param {Tree} tree
269
- * @param {BuildVisitor<Tree>} visitor
371
+ * Tree to traverse.
372
+ * @param {BuildVisitors<Tree>} visitors
373
+ * Handle each node when exiting it, and optionally when entering it.
270
374
  * @param {boolean | null | undefined} [reverse]
375
+ * Walk children from last to first;
376
+ * for `enter`, reverse preorder (NRL) instead of preorder (NLR);
377
+ * for `exit`, reverse postorder (RLN) instead of postorder (LRN).
271
378
  * @returns {undefined}
379
+ * Nothing.
272
380
  *
273
381
  * @param {UnistNode} tree
274
382
  * Tree to traverse.
275
- * @param {Visitor | Test} test
276
- * `unist-util-is`-compatible test
277
- * @param {Visitor | boolean | null | undefined} [visitor]
278
- * Handle each node.
383
+ * @param {Visitor | Visitors | Test} testOrEnterOrVisitors
384
+ * `unist-util-is`-compatible test, `enter`, or `visitors`.
385
+ * @param {Visitor | Visitors | boolean | null | undefined} [enterOrVisitorsOrReverse]
386
+ * `enter`, `visitors`, or `reverse` after a test.
279
387
  * @param {boolean | null | undefined} [reverse]
280
- * Traverse in reverse preorder (NRL) instead of the default preorder (NLR).
388
+ * Walk children from last to first;
389
+ * for `enter`, reverse preorder (NRL) instead of preorder (NLR);
390
+ * for `exit`, reverse postorder (RLN) instead of postorder (LRN).
281
391
  * @returns {undefined}
282
392
  * Nothing.
283
393
  *
@@ -286,28 +396,89 @@ export const SKIP = 'skip'
286
396
  * @template {Test} Check
287
397
  * `unist-util-is`-compatible test.
288
398
  */
289
- export function visitParents(tree, test, visitor, reverse) {
399
+ export function visitParents(
400
+ tree,
401
+ testOrEnterOrVisitors,
402
+ enterOrVisitorsOrReverse,
403
+ reverse
404
+ ) {
405
+ if (!tree || typeof tree !== 'object') {
406
+ throw new Error('Unexpected value `' + tree + '` for `tree`, expected node')
407
+ }
408
+
409
+ let preorder = true
290
410
  /** @type {Test} */
291
411
  let check
412
+ /** @type {Visitors | Visitor} */
413
+ let visitors
414
+ /** @type {Visitor | null | undefined} */
415
+ let enter
416
+ /** @type {VisitorExit | null | undefined} */
417
+ let exit
418
+
419
+ // Third parameter is an enter visitor or a visitors object, so expect a `test` too.
420
+ if (
421
+ typeof enterOrVisitorsOrReverse === 'function' ||
422
+ (enterOrVisitorsOrReverse && typeof enterOrVisitorsOrReverse === 'object')
423
+ ) {
424
+ check = /** @type {Test} */ (testOrEnterOrVisitors)
425
+ visitors = enterOrVisitorsOrReverse
426
+ if (reverse) preorder = false
427
+ }
428
+ // Assume no test.
429
+ else {
430
+ visitors = /** @type {Visitors | Visitor} */ (testOrEnterOrVisitors)
431
+ if (enterOrVisitorsOrReverse) preorder = false
432
+ }
433
+
434
+ if (typeof visitors === 'function') {
435
+ enter = visitors
436
+ } else if (visitors && typeof visitors === 'object') {
437
+ if (visitors.enter === null || visitors.enter === undefined) {
438
+ // Empty.
439
+ } else if (typeof visitors.enter === 'function') {
440
+ enter = visitors.enter
441
+ } else {
442
+ throw new Error(
443
+ 'Unexpected value `' +
444
+ visitors.enter +
445
+ '` for `visitors.enter`, expected optional visitor'
446
+ )
447
+ }
292
448
 
293
- if (typeof test === 'function' && typeof visitor !== 'function') {
294
- reverse = visitor
295
- // @ts-expect-error no visitor given, so `visitor` is test.
296
- visitor = test
449
+ if (typeof visitors.exit === 'function') {
450
+ exit = visitors.exit
451
+ } else {
452
+ throw new Error(
453
+ 'Unexpected value `' +
454
+ visitors.exit +
455
+ '` for `visitors.exit`, expected visitor (pass a visitor directly if you only want an enter visitor)'
456
+ )
457
+ }
297
458
  } else {
298
- // @ts-expect-error visitor given, so `test` isn’t a visitor.
299
- check = test
459
+ throw new Error(
460
+ 'Unexpected value `' +
461
+ visitors +
462
+ '` for `enter` or `visitors`, expected visitor or object with visitors'
463
+ )
300
464
  }
301
465
 
302
466
  const is = convert(check)
303
- const step = reverse ? -1 : 1
467
+ const step = preorder ? 1 : -1
304
468
 
305
469
  factory(tree, undefined, [])()
306
470
 
307
471
  /**
472
+ * Create a function to visit a node.
473
+ *
308
474
  * @param {UnistNode} node
475
+ * Node to visit.
309
476
  * @param {number | undefined} index
477
+ * Index of `node` in its parent.
310
478
  * @param {Array<UnistParent>} parents
479
+ * Ancestors of `node`.
480
+ * @returns {() => Readonly<ActionTuple>}
481
+ * Visit `node`.
311
482
  */
312
483
  function factory(node, index, parents) {
313
484
  const value = /** @type {Record<string, unknown>} */ (
@@ -333,18 +504,13 @@ export function visitParents(tree, test, visitor, reverse) {
333
504
  return visit
334
505
 
335
506
  function visit() {
507
+ const matches =
508
+ !check || is(node, index, parents[parents.length - 1] || undefined)
336
509
  /** @type {Readonly<ActionTuple>} */
337
510
  let result = empty
338
- /** @type {Readonly<ActionTuple>} */
339
- let subresult
340
- /** @type {number} */
341
- let offset
342
- /** @type {Array<UnistParent>} */
343
- let grandparents
344
511
 
345
- if (!test || is(node, index, parents[parents.length - 1] || undefined)) {
346
- // @ts-expect-error: `visitor` is now a visitor.
347
- result = toResult(visitor(node, parents))
512
+ if (enter && matches) {
513
+ result = toResult(enter(node, parents, index))
348
514
 
349
515
  if (result[0] === EXIT) {
350
516
  return result
@@ -355,13 +521,13 @@ export function visitParents(tree, test, visitor, reverse) {
355
521
  const nodeAsParent = /** @type {UnistParent} */ (node)
356
522
 
357
523
  if (nodeAsParent.children && result[0] !== SKIP) {
358
- offset = (reverse ? nodeAsParent.children.length : -1) + step
359
- grandparents = parents.concat(nodeAsParent)
524
+ let offset = (preorder ? -1 : nodeAsParent.children.length) + step
525
+ const grandparents = [...parents, nodeAsParent]
360
526
 
361
527
  while (offset > -1 && offset < nodeAsParent.children.length) {
362
528
  const child = nodeAsParent.children[offset]
363
529
 
364
- subresult = factory(child, offset, grandparents)()
530
+ const subresult = factory(child, offset, grandparents)()
365
531
 
366
532
  if (subresult[0] === EXIT) {
367
533
  return subresult
@@ -373,6 +539,23 @@ export function visitParents(tree, test, visitor, reverse) {
373
539
  }
374
540
  }
375
541
 
542
+ if (exit && matches) {
543
+ const exitResult = toResult(exit(node, parents, index))
544
+
545
+ if (exitResult[0] === SKIP) {
546
+ throw new Error(
547
+ 'Unexpected value `' +
548
+ exitResult[0] +
549
+ '` from `exit`, expected `CONTINUE`, `EXIT`, index, or nothing'
550
+ )
551
+ }
552
+
553
+ // Prefer a result from `exit` over `enter`.
554
+ if (exitResult[0] === EXIT || typeof exitResult[1] === 'number') {
555
+ return exitResult
556
+ }
557
+ }
558
+
376
559
  return result
377
560
  }
378
561
  }
@@ -1,6 +1,6 @@
1
1
  (The MIT License)
2
2
 
3
- Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
3
+ Copyright (c) Titus Wormer <tituswormer@gmail.com>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the