Skip to content

Commit ea420af

Browse files
committed
feat(walker): implement restoreWalkerAtNode
1 parent fe6ba60 commit ea420af

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/walker/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { SchemaNode } from '../nodes';
1+
import type { RegularNode, SchemaNode } from '../nodes';
2+
import type { RootNode } from '../nodes/RootNode';
23
import type { SchemaFragment } from '../types';
34

45
export type WalkerRefResolver = (path: string[] | null, $ref: string) => SchemaFragment;
@@ -16,11 +17,12 @@ export type WalkerItem = {
1617
export type WalkerSnapshot = {
1718
readonly fragment: SchemaFragment;
1819
readonly depth: number;
20+
readonly schemaNode: RegularNode | RootNode;
1921
readonly path: string[];
2022
};
2123

2224
export type WalkerHookAction = 'filter' | 'stepIn';
2325
export type WalkerHookHandler = (node: SchemaNode) => boolean;
2426

25-
export type WalkerEvent = 'newNode' | 'enterNode' | 'exitNode';
27+
export type WalkerEvent = 'newNode' | 'acceptNode' | 'enterNode' | 'exitNode';
2628
export type WalkerEventHandler = (node: SchemaNode) => void;

src/walker/walk.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class Walker extends EventEmitter<Dictionary<WalkerEventHandler, WalkerEv
4545
this.path.splice(0, this.path.length, ...snapshot.path);
4646
this.depth = snapshot.depth;
4747
this.fragment = snapshot.fragment;
48+
this.schemaNode = snapshot.schemaNode;
4849

4950
yield* this.walk();
5051
}
@@ -53,6 +54,7 @@ export class Walker extends EventEmitter<Dictionary<WalkerEventHandler, WalkerEv
5354
return {
5455
depth: this.depth,
5556
fragment: this.fragment,
57+
schemaNode: this.schemaNode,
5658
path: this.path.slice(),
5759
};
5860
}
@@ -61,6 +63,13 @@ export class Walker extends EventEmitter<Dictionary<WalkerEventHandler, WalkerEv
6163
this.hooks[action] = handler;
6264
}
6365

66+
public restoreWalkerAtNode(node: RegularNode) {
67+
this.path.splice(0, this.path.length, ...node.path);
68+
this.depth = node.depth;
69+
this.fragment = node.fragment;
70+
this.schemaNode = node;
71+
}
72+
6473
public *walk(): IterableIterator<WalkerItem> {
6574
const {
6675
depth: initialDepth,
@@ -93,13 +102,15 @@ export class Walker extends EventEmitter<Dictionary<WalkerEventHandler, WalkerEv
93102
}
94103
}
95104

96-
if (!(schemaNode instanceof RegularNode)) continue;
105+
super.emit('acceptNode', schemaNode);
97106

98-
this.schemaNode = schemaNode;
107+
if (schemaNode instanceof RegularNode) {
108+
this.schemaNode = schemaNode;
99109

100-
if (this.hooks.stepIn?.(schemaNode) === false) {
101-
super.emit('enterNode', schemaNode);
102-
yield* this.walkNodeChildren();
110+
if (this.hooks.stepIn?.(schemaNode) !== false) {
111+
super.emit('enterNode', schemaNode);
112+
yield* this.walkNodeChildren();
113+
}
103114
}
104115

105116
super.emit('exitNode', schemaNode);

0 commit comments

Comments
 (0)