clarity-pattern-parser 11.4.2 → 11.5.0
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.
- package/dist/ast/Node.d.ts +5 -5
- package/dist/index.browser.js +43 -13
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +43 -13
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +43 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/ast/Node.test.ts +38 -0
- package/src/ast/Node.ts +41 -14
package/dist/ast/Node.d.ts
CHANGED
|
@@ -40,13 +40,13 @@ export declare class Node {
|
|
|
40
40
|
append(...nodes: Node[]): void;
|
|
41
41
|
nextSibling(): Node | null;
|
|
42
42
|
previousSibling(): Node | null;
|
|
43
|
-
find(predicate: (node: Node) => boolean): Node | null;
|
|
44
|
-
findAll(predicate: (node: Node) => boolean): Node[];
|
|
43
|
+
find(predicate: (node: Node) => boolean, breadthFirst?: boolean): Node | null;
|
|
44
|
+
findAll(predicate: (node: Node) => boolean, breadthFirst?: boolean): Node[];
|
|
45
45
|
findRoot(): Node;
|
|
46
46
|
findAncestor(predicate: (node: Node) => boolean): Node | null;
|
|
47
|
-
walkUp(callback: (node: Node) => void):
|
|
48
|
-
walkDown(callback: (node: Node) => void):
|
|
49
|
-
walkBreadthFirst(callback: (node: Node) => void):
|
|
47
|
+
walkUp(callback: (node: Node) => boolean | void): boolean;
|
|
48
|
+
walkDown(callback: (node: Node) => boolean | void): boolean;
|
|
49
|
+
walkBreadthFirst(callback: (node: Node) => boolean | void): boolean;
|
|
50
50
|
transform(visitors: Record<string, (node: Node) => Node>): Node;
|
|
51
51
|
flatten(): Node[];
|
|
52
52
|
compact(): void;
|
package/dist/index.browser.js
CHANGED
|
@@ -130,16 +130,42 @@
|
|
|
130
130
|
}
|
|
131
131
|
return null;
|
|
132
132
|
}
|
|
133
|
-
find(predicate) {
|
|
134
|
-
|
|
133
|
+
find(predicate, breadthFirst = false) {
|
|
134
|
+
let match = null;
|
|
135
|
+
if (breadthFirst) {
|
|
136
|
+
this.walkBreadthFirst(n => {
|
|
137
|
+
if (predicate(n)) {
|
|
138
|
+
match = n;
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
this.walkUp(n => {
|
|
145
|
+
if (predicate(n)) {
|
|
146
|
+
match = n;
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return match;
|
|
135
152
|
}
|
|
136
|
-
findAll(predicate) {
|
|
153
|
+
findAll(predicate, breadthFirst = false) {
|
|
137
154
|
const matches = [];
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
155
|
+
if (breadthFirst) {
|
|
156
|
+
this.walkBreadthFirst(n => {
|
|
157
|
+
if (predicate(n)) {
|
|
158
|
+
matches.push(n);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.walkUp(n => {
|
|
164
|
+
if (predicate(n)) {
|
|
165
|
+
matches.push(n);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
143
169
|
return matches;
|
|
144
170
|
}
|
|
145
171
|
findRoot() {
|
|
@@ -162,22 +188,26 @@
|
|
|
162
188
|
return null;
|
|
163
189
|
}
|
|
164
190
|
walkUp(callback) {
|
|
191
|
+
var _a;
|
|
165
192
|
const childrenCopy = this._children.slice();
|
|
166
|
-
childrenCopy.
|
|
167
|
-
callback(this);
|
|
193
|
+
const result = childrenCopy.every(c => c.walkUp(callback));
|
|
194
|
+
return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && result;
|
|
168
195
|
}
|
|
169
196
|
walkDown(callback) {
|
|
197
|
+
var _a;
|
|
170
198
|
const childrenCopy = this._children.slice();
|
|
171
|
-
callback(this);
|
|
172
|
-
childrenCopy.forEach(c => c.walkDown(callback));
|
|
199
|
+
return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && childrenCopy.every(c => c.walkDown(callback));
|
|
173
200
|
}
|
|
174
201
|
walkBreadthFirst(callback) {
|
|
175
202
|
const queue = [this];
|
|
176
203
|
while (queue.length > 0) {
|
|
177
204
|
const current = queue.shift();
|
|
178
|
-
callback(current)
|
|
205
|
+
if (callback(current) === false) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
179
208
|
queue.push(...current.children);
|
|
180
209
|
}
|
|
210
|
+
return true;
|
|
181
211
|
}
|
|
182
212
|
transform(visitors) {
|
|
183
213
|
const childrenCopy = this._children.slice();
|