@swagger-api/apidom-ast 0.76.2 → 0.78.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.
Files changed (59) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/cjs/Error.cjs +1 -2
  3. package/cjs/Literal.cjs +1 -2
  4. package/cjs/Node.cjs +1 -2
  5. package/cjs/ParseResult.cjs +1 -2
  6. package/cjs/Position.cjs +2 -4
  7. package/cjs/index.cjs +3 -2
  8. package/cjs/json/nodes/JsonArray.cjs +1 -2
  9. package/cjs/json/nodes/JsonDocument.cjs +1 -2
  10. package/cjs/json/nodes/JsonEscapeSequence.cjs +1 -2
  11. package/cjs/json/nodes/JsonFalse.cjs +1 -2
  12. package/cjs/json/nodes/JsonKey.cjs +1 -2
  13. package/cjs/json/nodes/JsonNode.cjs +1 -2
  14. package/cjs/json/nodes/JsonNull.cjs +1 -2
  15. package/cjs/json/nodes/JsonNumber.cjs +1 -2
  16. package/cjs/json/nodes/JsonObject.cjs +1 -2
  17. package/cjs/json/nodes/JsonProperty.cjs +1 -2
  18. package/cjs/json/nodes/JsonString.cjs +1 -2
  19. package/cjs/json/nodes/JsonStringContent.cjs +1 -2
  20. package/cjs/json/nodes/JsonTrue.cjs +1 -2
  21. package/cjs/json/nodes/JsonValue.cjs +1 -2
  22. package/cjs/json/nodes/predicates.cjs +12 -24
  23. package/cjs/predicates.cjs +4 -8
  24. package/cjs/traversal/visitor.cjs +92 -89
  25. package/cjs/yaml/errors/YamlSchemaError.cjs +1 -2
  26. package/cjs/yaml/errors/YamlTagError.cjs +1 -2
  27. package/cjs/yaml/nodes/YamlAlias.cjs +1 -2
  28. package/cjs/yaml/nodes/YamlAnchor.cjs +1 -2
  29. package/cjs/yaml/nodes/YamlCollection.cjs +1 -2
  30. package/cjs/yaml/nodes/YamlComment.cjs +1 -2
  31. package/cjs/yaml/nodes/YamlDirective.cjs +1 -2
  32. package/cjs/yaml/nodes/YamlDocument.cjs +1 -2
  33. package/cjs/yaml/nodes/YamlKeyValuePair.cjs +1 -2
  34. package/cjs/yaml/nodes/YamlMapping.cjs +1 -2
  35. package/cjs/yaml/nodes/YamlNode.cjs +1 -2
  36. package/cjs/yaml/nodes/YamlScalar.cjs +1 -2
  37. package/cjs/yaml/nodes/YamlSequence.cjs +1 -2
  38. package/cjs/yaml/nodes/YamlStream.cjs +1 -2
  39. package/cjs/yaml/nodes/YamlStyle.cjs +3 -6
  40. package/cjs/yaml/nodes/YamlTag.cjs +2 -4
  41. package/cjs/yaml/nodes/predicates.cjs +10 -20
  42. package/cjs/yaml/schemas/ScalarTag.cjs +1 -2
  43. package/cjs/yaml/schemas/Tag.cjs +1 -2
  44. package/cjs/yaml/schemas/canonical-format.cjs +4 -6
  45. package/cjs/yaml/schemas/failsafe/GenericMapping.cjs +1 -2
  46. package/cjs/yaml/schemas/failsafe/GenericSequence.cjs +1 -2
  47. package/cjs/yaml/schemas/failsafe/GenericString.cjs +1 -2
  48. package/cjs/yaml/schemas/failsafe/index.cjs +1 -2
  49. package/cjs/yaml/schemas/json/Boolean.cjs +1 -2
  50. package/cjs/yaml/schemas/json/FloatingPoint.cjs +1 -2
  51. package/cjs/yaml/schemas/json/Integer.cjs +1 -2
  52. package/cjs/yaml/schemas/json/Null.cjs +1 -2
  53. package/cjs/yaml/schemas/json/index.cjs +1 -2
  54. package/dist/apidom-ast.browser.js +3954 -17202
  55. package/dist/apidom-ast.browser.min.js +1 -1
  56. package/es/index.mjs +1 -1
  57. package/es/traversal/visitor.mjs +88 -85
  58. package/package.json +4 -4
  59. package/types/dist.d.ts +4 -2
@@ -43,6 +43,9 @@ export const getNodeType = node => node === null || node === void 0 ? void 0 : n
43
43
  // isNode :: Node -> Boolean
44
44
  export const isNode = node => typeof getNodeType(node) === 'string';
45
45
 
46
+ // cloneNode :: a -> a
47
+ export const cloneNode = node => Object.create(Object.getPrototypeOf(node), Object.getOwnPropertyDescriptors(node));
48
+
46
49
  /**
47
50
  * Creates a new visitor instance which delegates to many visitors to run in
48
51
  * parallel. Each visitor will be visited for each node before moving on.
@@ -53,11 +56,11 @@ export const mergeAll = (visitors, {
53
56
  visitFnGetter = getVisitFn,
54
57
  nodeTypeGetter = getNodeType
55
58
  } = {}) => {
56
- const skipping = new Array(visitors.length);
59
+ const skipping = new Array(visitors.length).fill(null);
57
60
  return {
58
61
  enter(node, ...rest) {
59
62
  for (let i = 0; i < visitors.length; i += 1) {
60
- if (skipping[i] == null) {
63
+ if (skipping[i] === null) {
61
64
  const fn = visitFnGetter(visitors[i], nodeTypeGetter(node), /* isLeaving */false);
62
65
  if (typeof fn === 'function') {
63
66
  const result = fn.call(visitors[i], node, ...rest);
@@ -75,7 +78,7 @@ export const mergeAll = (visitors, {
75
78
  },
76
79
  leave(node, ...rest) {
77
80
  for (let i = 0; i < visitors.length; i += 1) {
78
- if (skipping[i] == null) {
81
+ if (skipping[i] === null) {
79
82
  const fn = visitFnGetter(visitors[i], nodeTypeGetter(node), /* isLeaving */true);
80
83
  if (typeof fn === 'function') {
81
84
  const result = fn.call(visitors[i], node, ...rest);
@@ -197,6 +200,7 @@ visitor, {
197
200
  visitFnGetter = getVisitFn,
198
201
  nodeTypeGetter = getNodeType,
199
202
  nodePredicate = isNode,
203
+ nodeCloneFn = cloneNode,
200
204
  detectCycles = true
201
205
  } = {}) => {
202
206
  const visitorKeys = keyMap || {};
@@ -206,15 +210,14 @@ visitor, {
206
210
  let index = -1;
207
211
  let parent;
208
212
  let edits = [];
213
+ let node = root;
209
214
  const path = [];
210
215
  // @ts-ignore
211
216
  const ancestors = [];
212
- let newRoot = root;
213
217
  do {
214
218
  index += 1;
215
219
  const isLeaving = index === keys.length;
216
220
  let key;
217
- let node;
218
221
  const isEdited = isLeaving && edits.length !== 0;
219
222
  if (isLeaving) {
220
223
  key = ancestors.length === 0 ? undefined : path.pop();
@@ -223,23 +226,22 @@ visitor, {
223
226
  parent = ancestors.pop();
224
227
  if (isEdited) {
225
228
  if (inArray) {
226
- // @ts-ignore
229
+ // @ts-ignore; creating clone
227
230
  node = node.slice();
231
+ let editOffset = 0;
232
+ for (const [editKey, editValue] of edits) {
233
+ const arrayKey = editKey - editOffset;
234
+ if (editValue === deleteNodeSymbol) {
235
+ node.splice(arrayKey, 1);
236
+ editOffset += 1;
237
+ } else {
238
+ node[arrayKey] = editValue;
239
+ }
240
+ }
228
241
  } else {
229
242
  // creating clone
230
- node = Object.create(Object.getPrototypeOf(node), Object.getOwnPropertyDescriptors(node));
231
- }
232
- let editOffset = 0;
233
- for (let ii = 0; ii < edits.length; ii += 1) {
234
- let editKey = edits[ii][0];
235
- const editValue = edits[ii][1];
236
- if (inArray) {
237
- editKey -= editOffset;
238
- }
239
- if (inArray && editValue === deleteNodeSymbol) {
240
- node.splice(editKey, 1);
241
- editOffset += 1;
242
- } else {
243
+ node = nodeCloneFn(node);
244
+ for (const [editKey, editValue] of edits) {
243
245
  node[editKey] = editValue;
244
246
  }
245
247
  }
@@ -252,15 +254,13 @@ visitor, {
252
254
  inArray = stack.inArray;
253
255
  // @ts-ignore
254
256
  stack = stack.prev;
255
- } else {
256
- key = parent ? inArray ? index : keys[index] : undefined;
257
- node = parent ? parent[key] : newRoot;
257
+ } else if (parent !== deleteNodeSymbol && parent !== undefined) {
258
+ key = inArray ? index : keys[index];
259
+ node = parent[key];
258
260
  if (node === deleteNodeSymbol || node === undefined) {
259
261
  continue;
260
262
  }
261
- if (parent) {
262
- path.push(key);
263
- }
263
+ path.push(key);
264
264
  }
265
265
  if (ancestors.includes(node)) {
266
266
  continue;
@@ -270,6 +270,7 @@ visitor, {
270
270
  if (!nodePredicate(node)) {
271
271
  throw new ApiDOMError(`Invalid AST Node: ${JSON.stringify(node)}`);
272
272
  }
273
+
273
274
  // cycle detected; skipping over a sub-tree to avoid recursion
274
275
  if (detectCycles && ancestors.includes(node)) {
275
276
  path.pop();
@@ -282,25 +283,26 @@ visitor, {
282
283
  for (const [stateKey, stateValue] of Object.entries(state)) {
283
284
  visitor[stateKey] = stateValue;
284
285
  }
286
+ // retrieve result
285
287
  result = visitFn.call(visitor, node, key, parent, path, ancestors);
286
- if (result === breakSymbol) {
287
- break;
288
+ }
289
+ if (result === breakSymbol) {
290
+ break;
291
+ }
292
+ if (result === skipVisitingNodeSymbol) {
293
+ if (!isLeaving) {
294
+ path.pop();
295
+ continue;
288
296
  }
289
- if (result === skipVisitingNodeSymbol) {
290
- if (!isLeaving) {
297
+ } else if (result !== undefined) {
298
+ edits.push([key, result]);
299
+ if (!isLeaving) {
300
+ if (nodePredicate(result)) {
301
+ node = result;
302
+ } else {
291
303
  path.pop();
292
304
  continue;
293
305
  }
294
- } else if (result !== undefined) {
295
- edits.push([key, result]);
296
- if (!isLeaving) {
297
- if (nodePredicate(result)) {
298
- node = result;
299
- } else {
300
- path.pop();
301
- continue;
302
- }
303
- }
304
306
  }
305
307
  }
306
308
  }
@@ -308,6 +310,7 @@ visitor, {
308
310
  edits.push([key, node]);
309
311
  }
310
312
  if (!isLeaving) {
313
+ var _visitorKeys$nodeType;
311
314
  stack = {
312
315
  inArray,
313
316
  index,
@@ -317,19 +320,19 @@ visitor, {
317
320
  };
318
321
  inArray = Array.isArray(node);
319
322
  // @ts-ignore
320
- keys = inArray ? node : visitorKeys[nodeTypeGetter(node)] || [];
323
+ keys = inArray ? node : (_visitorKeys$nodeType = visitorKeys[nodeTypeGetter(node)]) !== null && _visitorKeys$nodeType !== void 0 ? _visitorKeys$nodeType : [];
321
324
  index = -1;
322
325
  edits = [];
323
- if (parent) {
326
+ if (parent !== deleteNodeSymbol && parent !== undefined) {
324
327
  ancestors.push(parent);
325
328
  }
326
329
  parent = node;
327
330
  }
328
331
  } while (stack !== undefined);
329
332
  if (edits.length !== 0) {
330
- [, newRoot] = edits[edits.length - 1];
333
+ return edits.at(-1)[1];
331
334
  }
332
- return newRoot;
335
+ return root;
333
336
  };
334
337
 
335
338
  /**
@@ -349,6 +352,7 @@ visitor, {
349
352
  visitFnGetter = getVisitFn,
350
353
  nodeTypeGetter = getNodeType,
351
354
  nodePredicate = isNode,
355
+ nodeCloneFn = cloneNode,
352
356
  detectCycles = true
353
357
  } = {}) => {
354
358
  const visitorKeys = keyMap || {};
@@ -358,15 +362,14 @@ visitor, {
358
362
  let index = -1;
359
363
  let parent;
360
364
  let edits = [];
365
+ let node = root;
361
366
  const path = [];
362
367
  // @ts-ignore
363
368
  const ancestors = [];
364
- let newRoot = root;
365
369
  do {
366
370
  index += 1;
367
371
  const isLeaving = index === keys.length;
368
372
  let key;
369
- let node;
370
373
  const isEdited = isLeaving && edits.length !== 0;
371
374
  if (isLeaving) {
372
375
  key = ancestors.length === 0 ? undefined : path.pop();
@@ -375,23 +378,22 @@ visitor, {
375
378
  parent = ancestors.pop();
376
379
  if (isEdited) {
377
380
  if (inArray) {
378
- // @ts-ignore
381
+ // @ts-ignore; creating clone
379
382
  node = node.slice();
383
+ let editOffset = 0;
384
+ for (const [editKey, editValue] of edits) {
385
+ const arrayKey = editKey - editOffset;
386
+ if (editValue === deleteNodeSymbol) {
387
+ node.splice(arrayKey, 1);
388
+ editOffset += 1;
389
+ } else {
390
+ node[arrayKey] = editValue;
391
+ }
392
+ }
380
393
  } else {
381
394
  // creating clone
382
- node = Object.create(Object.getPrototypeOf(node), Object.getOwnPropertyDescriptors(node));
383
- }
384
- let editOffset = 0;
385
- for (let ii = 0; ii < edits.length; ii += 1) {
386
- let editKey = edits[ii][0];
387
- const editValue = edits[ii][1];
388
- if (inArray) {
389
- editKey -= editOffset;
390
- }
391
- if (inArray && editValue === deleteNodeSymbol) {
392
- node.splice(editKey, 1);
393
- editOffset += 1;
394
- } else {
395
+ node = nodeCloneFn(node);
396
+ for (const [editKey, editValue] of edits) {
395
397
  node[editKey] = editValue;
396
398
  }
397
399
  }
@@ -404,21 +406,20 @@ visitor, {
404
406
  inArray = stack.inArray;
405
407
  // @ts-ignore
406
408
  stack = stack.prev;
407
- } else {
408
- key = parent ? inArray ? index : keys[index] : undefined;
409
- node = parent ? parent[key] : newRoot;
409
+ } else if (parent !== deleteNodeSymbol && parent !== undefined) {
410
+ key = inArray ? index : keys[index];
411
+ node = parent[key];
410
412
  if (node === deleteNodeSymbol || node === undefined) {
411
413
  continue;
412
414
  }
413
- if (parent) {
414
- path.push(key);
415
- }
415
+ path.push(key);
416
416
  }
417
417
  let result;
418
418
  if (!Array.isArray(node)) {
419
419
  if (!nodePredicate(node)) {
420
420
  throw new ApiDOMError(`Invalid AST Node: ${JSON.stringify(node)}`);
421
421
  }
422
+
422
423
  // cycle detected; skipping over a sub-tree to avoid recursion
423
424
  if (detectCycles && ancestors.includes(node)) {
424
425
  path.pop();
@@ -431,26 +432,27 @@ visitor, {
431
432
  visitor[stateKey] = stateValue;
432
433
  }
433
434
 
434
- // eslint-disable-next-line no-await-in-loop
435
- result = await visitFn.call(visitor, node, key, parent, path, ancestors);
436
- if (result === breakSymbol) {
437
- break;
435
+ // retrieve result
436
+ result = await visitFn.call(visitor, node, key, parent, path, ancestors); // eslint-disable-line no-await-in-loop
437
+ }
438
+
439
+ if (result === breakSymbol) {
440
+ break;
441
+ }
442
+ if (result === skipVisitingNodeSymbol) {
443
+ if (!isLeaving) {
444
+ path.pop();
445
+ continue;
438
446
  }
439
- if (result === skipVisitingNodeSymbol) {
440
- if (!isLeaving) {
447
+ } else if (result !== undefined) {
448
+ edits.push([key, result]);
449
+ if (!isLeaving) {
450
+ if (nodePredicate(result)) {
451
+ node = result;
452
+ } else {
441
453
  path.pop();
442
454
  continue;
443
455
  }
444
- } else if (result !== undefined) {
445
- edits.push([key, result]);
446
- if (!isLeaving) {
447
- if (nodePredicate(result)) {
448
- node = result;
449
- } else {
450
- path.pop();
451
- continue;
452
- }
453
- }
454
456
  }
455
457
  }
456
458
  }
@@ -458,6 +460,7 @@ visitor, {
458
460
  edits.push([key, node]);
459
461
  }
460
462
  if (!isLeaving) {
463
+ var _visitorKeys$nodeType2;
461
464
  stack = {
462
465
  inArray,
463
466
  index,
@@ -467,19 +470,19 @@ visitor, {
467
470
  };
468
471
  inArray = Array.isArray(node);
469
472
  // @ts-ignore
470
- keys = inArray ? node : visitorKeys[nodeTypeGetter(node)] || [];
473
+ keys = inArray ? node : (_visitorKeys$nodeType2 = visitorKeys[nodeTypeGetter(node)]) !== null && _visitorKeys$nodeType2 !== void 0 ? _visitorKeys$nodeType2 : [];
471
474
  index = -1;
472
475
  edits = [];
473
- if (parent) {
476
+ if (parent !== deleteNodeSymbol && parent !== undefined) {
474
477
  ancestors.push(parent);
475
478
  }
476
479
  parent = node;
477
480
  }
478
481
  } while (stack !== undefined);
479
482
  if (edits.length !== 0) {
480
- [, newRoot] = edits[edits.length - 1];
483
+ return edits.at(-1)[1];
481
484
  }
482
- return newRoot;
485
+ return root;
483
486
  };
484
487
 
485
488
  /* eslint-enable */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swagger-api/apidom-ast",
3
- "version": "0.76.2",
3
+ "version": "0.78.0",
4
4
  "description": "Tools necessary for parsing stage of ApiDOM, specifically for syntactic analysis.",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -42,8 +42,8 @@
42
42
  "homepage": "https://github.com/swagger-api/apidom#readme",
43
43
  "dependencies": {
44
44
  "@babel/runtime-corejs3": "^7.20.7",
45
- "@swagger-api/apidom-error": "^0.76.2",
46
- "@types/ramda": "~0.29.3",
45
+ "@swagger-api/apidom-error": "^0.78.0",
46
+ "@types/ramda": "~0.29.6",
47
47
  "ramda": "~0.29.0",
48
48
  "ramda-adjunct": "^4.1.1",
49
49
  "stampit": "^4.3.2",
@@ -59,5 +59,5 @@
59
59
  "README.md",
60
60
  "CHANGELOG.md"
61
61
  ],
62
- "gitHead": "8c6c02f813a663cfaf0ac0963f2b4fd25b8dffe8"
62
+ "gitHead": "d6587217f8a7bec5bbc49ca4dabff8d3d66e0913"
63
63
  }
package/types/dist.d.ts CHANGED
@@ -265,6 +265,7 @@ declare const getVisitFn: (visitor: any, type: string, isLeaving: boolean) => an
265
265
  declare const BREAK: {};
266
266
  declare const getNodeType: (node: any) => any;
267
267
  declare const isNode: (node: any) => boolean;
268
+ declare const cloneNode: (node: any) => any;
268
269
  /**
269
270
  * Creates a new visitor instance which delegates to many visitors to run in
270
271
  * parallel. Each visitor will be visited for each node before moving on.
@@ -367,7 +368,7 @@ declare const mergeAll: (visitors: any[], { visitFnGetter, nodeTypeGetter }?: {
367
368
  * @sig visit :: (Node, Visitor, Options)
368
369
  * @sig Options = { keyMap: Object, state: Object }
369
370
  */
370
- declare const visit: (root: any, visitor: any, { keyMap, state, breakSymbol, deleteNodeSymbol, skipVisitingNodeSymbol, visitFnGetter, nodeTypeGetter, nodePredicate, detectCycles, }?: {
371
+ declare const visit: (root: any, visitor: any, { keyMap, state, breakSymbol, deleteNodeSymbol, skipVisitingNodeSymbol, visitFnGetter, nodeTypeGetter, nodePredicate, nodeCloneFn, detectCycles, }?: {
371
372
  keyMap?: null | undefined;
372
373
  state?: {} | undefined;
373
374
  breakSymbol?: {} | undefined;
@@ -376,7 +377,8 @@ declare const visit: (root: any, visitor: any, { keyMap, state, breakSymbol, del
376
377
  visitFnGetter?: ((visitor: any, type: string, isLeaving: boolean) => any) | undefined;
377
378
  nodeTypeGetter?: ((node: any) => any) | undefined;
378
379
  nodePredicate?: ((node: any) => boolean) | undefined;
380
+ nodeCloneFn?: ((node: any) => any) | undefined;
379
381
  detectCycles?: boolean | undefined;
380
382
  }) => any;
381
383
 
382
- export { BREAK, Error, JsonArray, JsonDocument, JsonEscapeSequence, JsonFalse, JsonKey, JsonNode, JsonNull, JsonNumber, JsonObject, JsonProperty, JsonString, JsonStringContent, JsonTrue, JsonValue, Literal, ParseResult, Point, Position, YamlAlias, YamlAnchor, YamlCollection, YamlComment, YamlDirective, YamlDocument, FailsafeSchema as YamlFailsafeSchema, JsonSchema as YamlJsonSchema, YamlKeyValuePair, YamlMapping, YamlNode, YamlNodeKind, YamlScalar, YamlSchemaError, YamlSequence, YamlStream, YamlStyle, YamlStyleGroup, YamlTag, YamlTagError, YamlTagErrorOptions, getNodeType, getVisitFn, isArray as isJsonArray, isDocument$1 as isJsonDocument, isEscapeSequence as isJsonEscapeSequence, isFalse as isJsonFalse, isKey as isJsonKey, isNull as isJsonNull, isNumber as isJsonNumber, isObject as isJsonObject, isProperty as isJsonProperty, isString as isJsonString, isStringContent as isJsonStringContent, isTrue as isJsonTrue, isLiteral, isNode, isParseResult, isPoint, isPosition, isAlias as isYamlAlias, isDirective as isYamlDirective, isDocument as isYamlDocument, isKeyValuePair as isYamlKeyValuePair, isMapping as isYamlMapping, isScalar as isYamlScalar, isSequence as isYamlSequence, isStream as isYamlStream, isTag as isYamlTag, mergeAll as mergeAllVisitors, visit };
384
+ export { BREAK, Error, JsonArray, JsonDocument, JsonEscapeSequence, JsonFalse, JsonKey, JsonNode, JsonNull, JsonNumber, JsonObject, JsonProperty, JsonString, JsonStringContent, JsonTrue, JsonValue, Literal, ParseResult, Point, Position, YamlAlias, YamlAnchor, YamlCollection, YamlComment, YamlDirective, YamlDocument, FailsafeSchema as YamlFailsafeSchema, JsonSchema as YamlJsonSchema, YamlKeyValuePair, YamlMapping, YamlNode, YamlNodeKind, YamlScalar, YamlSchemaError, YamlSequence, YamlStream, YamlStyle, YamlStyleGroup, YamlTag, YamlTagError, type YamlTagErrorOptions, cloneNode, getNodeType, getVisitFn, isArray as isJsonArray, isDocument$1 as isJsonDocument, isEscapeSequence as isJsonEscapeSequence, isFalse as isJsonFalse, isKey as isJsonKey, isNull as isJsonNull, isNumber as isJsonNumber, isObject as isJsonObject, isProperty as isJsonProperty, isString as isJsonString, isStringContent as isJsonStringContent, isTrue as isJsonTrue, isLiteral, isNode, isParseResult, isPoint, isPosition, isAlias as isYamlAlias, isDirective as isYamlDirective, isDocument as isYamlDocument, isKeyValuePair as isYamlKeyValuePair, isMapping as isYamlMapping, isScalar as isYamlScalar, isSequence as isYamlSequence, isStream as isYamlStream, isTag as isYamlTag, mergeAll as mergeAllVisitors, visit };