@speclynx/apidom-traverse 2.1.0 → 2.2.1

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/src/Path.cjs CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
  exports.Path = void 0;
5
+ var _apidomDatamodel = require("@speclynx/apidom-datamodel");
5
6
  var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
6
- var _jsonpath = require("@swaggerexpert/jsonpath");
7
+ var _apidomJsonPath = require("@speclynx/apidom-json-path");
7
8
  /**
8
9
  * Possible return values from a visitor function.
9
10
  * @public
@@ -142,14 +143,34 @@ class Path {
142
143
  }
143
144
 
144
145
  /**
145
- * Get the path from root as an array of keys.
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
+ * path.getPathKeys(); // => ['paths', '/pets', 'get']
146
153
  */
147
154
  getPathKeys() {
148
155
  const keys = [];
149
156
  // eslint-disable-next-line @typescript-eslint/no-this-alias
150
157
  let current = this;
151
158
  while (current !== null && current.key !== undefined) {
152
- keys.unshift(current.key);
159
+ const {
160
+ key,
161
+ parent,
162
+ parentPath
163
+ } = current;
164
+ if ((0, _apidomDatamodel.isMemberElement)(parent) && key === 'value') {
165
+ // Inside MemberElement.value → push the member's key
166
+ if (!(0, _apidomDatamodel.isStringElement)(parent.key)) {
167
+ throw new TypeError('MemberElement.key must be a StringElement');
168
+ }
169
+ keys.unshift(parent.key.toValue());
170
+ } else if ((0, _apidomDatamodel.isArrayElement)(parentPath?.node) && typeof key === 'number') {
171
+ // Inside ArrayElement → push the numeric index
172
+ keys.unshift(key);
173
+ }
153
174
  current = current.parentPath;
154
175
  }
155
176
  return keys;
@@ -185,7 +206,7 @@ class Path {
185
206
  }
186
207
  if (pathFormat === 'jsonpath') {
187
208
  // RFC 9535 Normalized JSONPath
188
- return (0, _jsonpath.compile)(parts);
209
+ return _apidomJsonPath.NormalizedPath.from(parts);
189
210
  }
190
211
 
191
212
  // RFC 6901 JSON Pointer
package/src/Path.mjs CHANGED
@@ -1,5 +1,6 @@
1
+ import { isMemberElement, isArrayElement, isStringElement } from '@speclynx/apidom-datamodel';
1
2
  import { compile as compileJSONPointer } from '@speclynx/apidom-json-pointer';
2
- import { compile as compileJSONPath } from '@swaggerexpert/jsonpath';
3
+ import { NormalizedPath } from '@speclynx/apidom-json-path';
3
4
 
4
5
  /**
5
6
  * Possible return values from a visitor function.
@@ -139,14 +140,34 @@ export class Path {
139
140
  }
140
141
 
141
142
  /**
142
- * Get the path from root as an array of keys.
143
+ * Get the semantic path from root as an array of keys.
144
+ * Returns logical document keys (property names, array indices) rather than
145
+ * internal ApiDOM structure keys.
146
+ *
147
+ * @example
148
+ * // For a path to $.paths['/pets'].get in an OpenAPI document:
149
+ * path.getPathKeys(); // => ['paths', '/pets', 'get']
143
150
  */
144
151
  getPathKeys() {
145
152
  const keys = [];
146
153
  // eslint-disable-next-line @typescript-eslint/no-this-alias
147
154
  let current = this;
148
155
  while (current !== null && current.key !== undefined) {
149
- keys.unshift(current.key);
156
+ const {
157
+ key,
158
+ parent,
159
+ parentPath
160
+ } = current;
161
+ if (isMemberElement(parent) && key === 'value') {
162
+ // Inside MemberElement.value → push the member's key
163
+ if (!isStringElement(parent.key)) {
164
+ throw new TypeError('MemberElement.key must be a StringElement');
165
+ }
166
+ keys.unshift(parent.key.toValue());
167
+ } else if (isArrayElement(parentPath?.node) && typeof key === 'number') {
168
+ // Inside ArrayElement → push the numeric index
169
+ keys.unshift(key);
170
+ }
150
171
  current = current.parentPath;
151
172
  }
152
173
  return keys;
@@ -182,7 +203,7 @@ export class Path {
182
203
  }
183
204
  if (pathFormat === 'jsonpath') {
184
205
  // RFC 9535 Normalized JSONPath
185
- return compileJSONPath(parts);
206
+ return NormalizedPath.from(parts);
186
207
  }
187
208
 
188
209
  // RFC 6901 JSON Pointer
@@ -226,7 +226,13 @@ export declare class Path<TNode = Element_2> {
226
226
  */
227
227
  getAncestorNodes(): TNode[];
228
228
  /**
229
- * Get the path from root as an array of keys.
229
+ * Get the semantic path from root as an array of keys.
230
+ * Returns logical document keys (property names, array indices) rather than
231
+ * internal ApiDOM structure keys.
232
+ *
233
+ * @example
234
+ * // For a path to $.paths['/pets'].get in an OpenAPI document:
235
+ * path.getPathKeys(); // => ['paths', '/pets', 'get']
230
236
  */
231
237
  getPathKeys(): PropertyKey[];
232
238
  /**