@speclynx/apidom-traverse 4.0.1 → 4.0.3
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/CHANGELOG.md +10 -0
- package/package.json +7 -8
- package/src/Path.cjs +342 -0
- package/src/Path.mjs +338 -0
- package/src/Path.ts +368 -0
- package/src/index.cjs +43 -0
- package/src/index.mjs +19 -0
- package/src/index.ts +44 -0
- package/src/operations/filter.cjs +21 -0
- package/src/operations/filter.mjs +17 -0
- package/src/operations/filter.ts +27 -0
- package/src/operations/find-at-offset.cjs +45 -0
- package/src/operations/find-at-offset.mjs +40 -0
- package/src/operations/find-at-offset.ts +61 -0
- package/src/operations/find.cjs +22 -0
- package/src/operations/find.mjs +18 -0
- package/src/operations/find.ts +28 -0
- package/src/operations/for-each.cjs +36 -0
- package/src/operations/for-each.mjs +30 -0
- package/src/operations/for-each.ts +44 -0
- package/src/operations/parents.cjs +21 -0
- package/src/operations/parents.mjs +17 -0
- package/src/operations/parents.ts +24 -0
- package/src/operations/reject.cjs +14 -0
- package/src/operations/reject.mjs +9 -0
- package/src/operations/reject.ts +17 -0
- package/src/operations/some.cjs +14 -0
- package/src/operations/some.mjs +9 -0
- package/src/operations/some.ts +17 -0
- package/src/traversal.cjs +313 -0
- package/src/traversal.mjs +305 -0
- package/src/traversal.ts +391 -0
- package/src/visitors.cjs +420 -0
- package/src/visitors.mjs +407 -0
- package/src/visitors.ts +490 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [4.0.3](https://github.com/speclynx/apidom/compare/v4.0.2...v4.0.3) (2026-03-11)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **release:** fix v4.0.2 failed release ([b4dc1c4](https://github.com/speclynx/apidom/commit/b4dc1c48e8d9b2986a70e49b5554eb0a166d7528))
|
|
11
|
+
|
|
12
|
+
## [4.0.2](https://github.com/speclynx/apidom/compare/v4.0.1...v4.0.2) (2026-03-11)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @speclynx/apidom-traverse
|
|
15
|
+
|
|
6
16
|
## [4.0.1](https://github.com/speclynx/apidom/compare/v4.0.0...v4.0.1) (2026-03-11)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @speclynx/apidom-traverse
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@speclynx/apidom-traverse",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.3",
|
|
4
4
|
"description": "Traversal and visitor utilities for walking and transforming ApiDOM structures.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"apidom",
|
|
@@ -61,15 +61,14 @@
|
|
|
61
61
|
"license": "Apache-2.0",
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"@babel/runtime-corejs3": "^7.28.4",
|
|
64
|
-
"@speclynx/apidom-datamodel": "4.0.
|
|
65
|
-
"@speclynx/apidom-error": "4.0.
|
|
66
|
-
"@speclynx/apidom-json-path": "4.0.
|
|
67
|
-
"@speclynx/apidom-json-pointer": "4.0.
|
|
64
|
+
"@speclynx/apidom-datamodel": "4.0.3",
|
|
65
|
+
"@speclynx/apidom-error": "4.0.3",
|
|
66
|
+
"@speclynx/apidom-json-path": "4.0.3",
|
|
67
|
+
"@speclynx/apidom-json-pointer": "4.0.3",
|
|
68
68
|
"ramda-adjunct": "^6.0.0"
|
|
69
69
|
},
|
|
70
70
|
"files": [
|
|
71
|
-
"src
|
|
72
|
-
"src/**/*.cjs",
|
|
71
|
+
"src/",
|
|
73
72
|
"dist/",
|
|
74
73
|
"types/apidom-traverse.d.ts",
|
|
75
74
|
"LICENSES",
|
|
@@ -77,5 +76,5 @@
|
|
|
77
76
|
"README.md",
|
|
78
77
|
"CHANGELOG.md"
|
|
79
78
|
],
|
|
80
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "6ccfa09c02232516215e7de3ead276641957e626"
|
|
81
80
|
}
|
package/src/Path.cjs
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.Path = void 0;
|
|
5
|
+
var _apidomDatamodel = require("@speclynx/apidom-datamodel");
|
|
6
|
+
var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
|
|
7
|
+
var _apidomJsonPath = require("@speclynx/apidom-json-path");
|
|
8
|
+
/**
|
|
9
|
+
* Possible return values from a visitor function.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Visitor function signature - receives a Path object.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Path represents a node's position in the tree during traversal.
|
|
20
|
+
* Inspired by Babel's NodePath API.
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
class Path {
|
|
24
|
+
/**
|
|
25
|
+
* The current AST node.
|
|
26
|
+
*/
|
|
27
|
+
node;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The key of this node in its parent.
|
|
31
|
+
* `undefined` for the root node.
|
|
32
|
+
*/
|
|
33
|
+
key;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The index if this node is in an array.
|
|
37
|
+
* Same as `key` when parent property is an array, `undefined` otherwise.
|
|
38
|
+
*/
|
|
39
|
+
index;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The parent node.
|
|
43
|
+
* `undefined` for the root node.
|
|
44
|
+
*/
|
|
45
|
+
parent;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The parent Path.
|
|
49
|
+
* `null` for the root node.
|
|
50
|
+
*/
|
|
51
|
+
parentPath;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether this node is inside an array in the parent.
|
|
55
|
+
*/
|
|
56
|
+
inList;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Internal state for traversal control.
|
|
60
|
+
*/
|
|
61
|
+
#shouldSkip = false;
|
|
62
|
+
#shouldStop = false;
|
|
63
|
+
#removed = false;
|
|
64
|
+
#replaced = false;
|
|
65
|
+
#replacementNode;
|
|
66
|
+
#stale = false;
|
|
67
|
+
constructor(node, parent, parentPath, key, inList) {
|
|
68
|
+
this.node = node;
|
|
69
|
+
this.parent = parent;
|
|
70
|
+
this.parentPath = parentPath;
|
|
71
|
+
this.key = key;
|
|
72
|
+
this.index = inList && typeof key === 'number' ? key : undefined;
|
|
73
|
+
this.inList = inList;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ===========================================================================
|
|
77
|
+
// Traversal state
|
|
78
|
+
// ===========================================================================
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Whether skip() was called on this path.
|
|
82
|
+
*/
|
|
83
|
+
get shouldSkip() {
|
|
84
|
+
return this.#shouldSkip;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether stop() was called on this path.
|
|
89
|
+
*/
|
|
90
|
+
get shouldStop() {
|
|
91
|
+
return this.#shouldStop;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether this node was removed.
|
|
96
|
+
*/
|
|
97
|
+
get removed() {
|
|
98
|
+
return this.#removed;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ===========================================================================
|
|
102
|
+
// Ancestry
|
|
103
|
+
// ===========================================================================
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns true if this is the root path.
|
|
107
|
+
*/
|
|
108
|
+
isRoot() {
|
|
109
|
+
return this.parentPath === null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Get the depth of this path (0 for root).
|
|
114
|
+
*/
|
|
115
|
+
get depth() {
|
|
116
|
+
let depth = 0;
|
|
117
|
+
let current = this.parentPath;
|
|
118
|
+
while (current !== null) {
|
|
119
|
+
depth += 1;
|
|
120
|
+
current = current.parentPath;
|
|
121
|
+
}
|
|
122
|
+
return depth;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Get all ancestor paths from immediate parent to root.
|
|
127
|
+
*/
|
|
128
|
+
getAncestry() {
|
|
129
|
+
const ancestry = [];
|
|
130
|
+
let current = this.parentPath;
|
|
131
|
+
while (current !== null) {
|
|
132
|
+
ancestry.push(current);
|
|
133
|
+
current = current.parentPath;
|
|
134
|
+
}
|
|
135
|
+
return ancestry;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Get all ancestor nodes from immediate parent to root.
|
|
140
|
+
*/
|
|
141
|
+
getAncestorNodes() {
|
|
142
|
+
return this.getAncestry().map(p => p.node);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Get the semantic path from root as an array of keys.
|
|
147
|
+
* Returns logical document keys (property names, array indices) rather than
|
|
148
|
+
* internal ApiDOM structure keys.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* // For a path to $.paths['/pets'].get in an OpenAPI document:
|
|
152
|
+
* ```
|
|
153
|
+
* path.getPathKeys(); // => ['paths', '/pets', 'get']
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
getPathKeys() {
|
|
157
|
+
const keys = [];
|
|
158
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
159
|
+
let current = this;
|
|
160
|
+
while (current !== null && current.key !== undefined) {
|
|
161
|
+
const {
|
|
162
|
+
key,
|
|
163
|
+
parent,
|
|
164
|
+
parentPath
|
|
165
|
+
} = current;
|
|
166
|
+
if ((0, _apidomDatamodel.isMemberElement)(parent) && key === 'value') {
|
|
167
|
+
// Inside MemberElement.value → push the member's key
|
|
168
|
+
if (!(0, _apidomDatamodel.isStringElement)(parent.key)) {
|
|
169
|
+
throw new TypeError('MemberElement.key must be a StringElement');
|
|
170
|
+
}
|
|
171
|
+
keys.unshift(parent.key.toValue());
|
|
172
|
+
} else if ((0, _apidomDatamodel.isArrayElement)(parentPath?.node) && typeof key === 'number') {
|
|
173
|
+
// Inside ArrayElement → push the numeric index
|
|
174
|
+
keys.unshift(key);
|
|
175
|
+
}
|
|
176
|
+
current = current.parentPath;
|
|
177
|
+
}
|
|
178
|
+
return keys;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Format path as RFC 6901 JSON Pointer or RFC 9535 Normalized JSONPath.
|
|
183
|
+
*
|
|
184
|
+
* @param pathFormat - Output format: "jsonpointer" (default) or "jsonpath"
|
|
185
|
+
* @returns JSONPointer string like "/paths/~1pets/get/responses/200"
|
|
186
|
+
* or Normalized JSONPath like "$['paths']['/pets']['get']['responses']['200']"
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```
|
|
190
|
+
* // JSON Pointer examples:
|
|
191
|
+
* path.formatPath(); // "" (root)
|
|
192
|
+
* path.formatPath(); // "/info"
|
|
193
|
+
* path.formatPath(); // "/paths/~1pets/get"
|
|
194
|
+
* path.formatPath(); // "/paths/~1users~1{id}/parameters/0"
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```
|
|
199
|
+
* // JSONPath examples:
|
|
200
|
+
* path.formatPath('jsonpath'); // "$" (root)
|
|
201
|
+
* path.formatPath('jsonpath'); // "$['info']"
|
|
202
|
+
* path.formatPath('jsonpath'); // "$['paths']['/pets']['get']"
|
|
203
|
+
* path.formatPath('jsonpath'); // "$['paths']['/users/{id}']['parameters'][0]"
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
formatPath(pathFormat = 'jsonpointer') {
|
|
207
|
+
const parts = this.getPathKeys();
|
|
208
|
+
|
|
209
|
+
// Root node
|
|
210
|
+
if (parts.length === 0) {
|
|
211
|
+
return pathFormat === 'jsonpath' ? '$' : '';
|
|
212
|
+
}
|
|
213
|
+
if (pathFormat === 'jsonpath') {
|
|
214
|
+
// RFC 9535 Normalized JSONPath
|
|
215
|
+
return _apidomJsonPath.NormalizedPath.from(parts);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// RFC 6901 JSON Pointer
|
|
219
|
+
return (0, _apidomJsonPointer.compile)(parts);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Find the closest ancestor path that satisfies the predicate.
|
|
224
|
+
*/
|
|
225
|
+
findParent(predicate) {
|
|
226
|
+
let current = this.parentPath;
|
|
227
|
+
while (current !== null) {
|
|
228
|
+
if (predicate(current)) {
|
|
229
|
+
return current;
|
|
230
|
+
}
|
|
231
|
+
current = current.parentPath;
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Find the closest path (including this one) that satisfies the predicate.
|
|
238
|
+
*/
|
|
239
|
+
find(predicate) {
|
|
240
|
+
if (predicate(this)) {
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
return this.findParent(predicate);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// ===========================================================================
|
|
247
|
+
// Nested traversal
|
|
248
|
+
// ===========================================================================
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Traverse into the current node with a new visitor.
|
|
252
|
+
* Populated by the traversal module to avoid circular imports.
|
|
253
|
+
*/
|
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Async version of traverse.
|
|
258
|
+
*/
|
|
259
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
260
|
+
|
|
261
|
+
// ===========================================================================
|
|
262
|
+
// Traversal control
|
|
263
|
+
// ===========================================================================
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Skip traversing the children of this node.
|
|
267
|
+
*/
|
|
268
|
+
skip() {
|
|
269
|
+
this.#shouldSkip = true;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Stop all traversal completely.
|
|
274
|
+
*/
|
|
275
|
+
stop() {
|
|
276
|
+
this.#shouldStop = true;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ===========================================================================
|
|
280
|
+
// Modification
|
|
281
|
+
// ===========================================================================
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Replace this node with a new node.
|
|
285
|
+
*/
|
|
286
|
+
replaceWith(replacement) {
|
|
287
|
+
if (this.#stale) {
|
|
288
|
+
console.warn('Warning: replaceWith() called on a stale Path. ' + 'This path belongs to a node whose visit has already completed. ' + 'The replacement will have no effect. ' + "To replace a parent node, do so from the parent's own visitor.");
|
|
289
|
+
}
|
|
290
|
+
this.#replaced = true;
|
|
291
|
+
this.#replacementNode = replacement;
|
|
292
|
+
this.node = replacement;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Remove this node from the tree.
|
|
297
|
+
*/
|
|
298
|
+
remove() {
|
|
299
|
+
if (this.#stale) {
|
|
300
|
+
console.warn('Warning: remove() called on a stale Path. ' + 'This path belongs to a node whose visit has already completed. ' + 'The removal will have no effect. ' + "To remove a parent node, do so from the parent's own visitor.");
|
|
301
|
+
}
|
|
302
|
+
this.#removed = true;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ===========================================================================
|
|
306
|
+
// Internal methods for traversal engine
|
|
307
|
+
// ===========================================================================
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @internal
|
|
311
|
+
*/
|
|
312
|
+
_getReplacementNode() {
|
|
313
|
+
return this.#replacementNode;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @internal
|
|
318
|
+
*/
|
|
319
|
+
_wasReplaced() {
|
|
320
|
+
return this.#replaced;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* @internal
|
|
325
|
+
*/
|
|
326
|
+
_reset() {
|
|
327
|
+
this.#shouldSkip = false;
|
|
328
|
+
this.#shouldStop = false;
|
|
329
|
+
this.#removed = false;
|
|
330
|
+
this.#replaced = false;
|
|
331
|
+
this.#replacementNode = undefined;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Mark this path as stale (visit completed).
|
|
336
|
+
* @internal
|
|
337
|
+
*/
|
|
338
|
+
_markStale() {
|
|
339
|
+
this.#stale = true;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
exports.Path = Path;
|