@swagger-api/apidom-core 0.91.0 → 0.93.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.
@@ -11,7 +11,7 @@ class SourceMap extends ArrayElement {
11
11
  return this.children.filter(item => item.classes.contains('position')).get(1);
12
12
  }
13
13
  set position(position) {
14
- if (position === null) {
14
+ if (typeof position === 'undefined') {
15
15
  return;
16
16
  }
17
17
  const start = new ArrayElement([position.start.row, position.start.column, position.start.char]);
@@ -3,6 +3,7 @@ import stampit from 'stampit';
3
3
  import ShortUniqueId from 'short-unique-id';
4
4
  import ElementIdentityError from "./errors/ElementIdentityError.mjs";
5
5
  import { isElement, isStringElement } from "../predicates/index.mjs";
6
+ // @TODO(oliwia.rogala@smartbear.com): transforming this stamp to class will break backward compatibility
6
7
  export const IdentityManager = stampit({
7
8
  props: {
8
9
  uuid: null,
@@ -1,4 +1,3 @@
1
- import stampit from 'stampit';
2
1
  import { isUndefined } from 'ramda-adjunct';
3
2
  import { isObjectElement, isArrayElement, isMemberElement } from "../predicates/index.mjs";
4
3
  const computeEdges = (element, edges = new WeakMap()) => {
@@ -69,18 +68,20 @@ const transcludeChildOfArrayElement = (search, replace, edges) => {
69
68
  * clone in before passing it to initializer of this stamp.
70
69
  */
71
70
 
72
- const Transcluder = stampit.init(function TranscluderConstructor({
73
- element
74
- }) {
75
- let edges;
76
- this.transclude = function transclude(search, replace) {
77
- var _edges;
71
+ class Transcluder {
72
+ constructor({
73
+ element
74
+ }) {
75
+ this.element = element;
76
+ }
77
+ transclude(search, replace) {
78
+ var _this$edges;
78
79
  // shortcut 1. - replacing entire ApiDOM tree
79
- if (search === element) return replace;
80
+ if (search === this.element) return replace;
80
81
  // shortcut 2. - replacing nothing
81
- if (search === replace) return element;
82
- edges = (_edges = edges) !== null && _edges !== void 0 ? _edges : computeEdges(element);
83
- const parent = edges.get(search);
82
+ if (search === replace) return this.element;
83
+ this.edges = (_this$edges = this.edges) !== null && _this$edges !== void 0 ? _this$edges : computeEdges(this.element);
84
+ const parent = this.edges.get(search);
84
85
  if (isUndefined(parent)) {
85
86
  return undefined;
86
87
  }
@@ -92,13 +93,13 @@ const Transcluder = stampit.init(function TranscluderConstructor({
92
93
  */
93
94
  if (isObjectElement(parent)) {
94
95
  // @ts-ignore
95
- transcludeChildOfObjectElement(search, replace, edges);
96
+ transcludeChildOfObjectElement(search, replace, this.edges);
96
97
  } else if (isArrayElement(parent)) {
97
- transcludeChildOfArrayElement(search, replace, edges);
98
+ transcludeChildOfArrayElement(search, replace, this.edges);
98
99
  } else if (isMemberElement(parent)) {
99
- transcludeChildOfMemberElement(search, replace, edges);
100
+ transcludeChildOfMemberElement(search, replace, this.edges);
100
101
  }
101
- return element;
102
- };
103
- });
102
+ return this.element;
103
+ }
104
+ }
104
105
  export default Transcluder;
@@ -4,7 +4,7 @@ import Transcluder from "./Transcluder.mjs";
4
4
  * clone in before passing it to this function.
5
5
  */
6
6
  export const transclude = (search, replace, element) => {
7
- const transcluder = Transcluder({
7
+ const transcluder = new Transcluder({
8
8
  element
9
9
  });
10
10
  return transcluder.transclude(search, replace);
@@ -1,61 +1,58 @@
1
- import stampit from 'stampit';
2
1
  import { visit } from "./visitor.mjs";
3
2
  import EphemeralArray from "./ast/ephemeral-array.mjs";
4
3
  import EphemeralObject from "./ast/ephemeral-object.mjs";
5
4
  import { isElement, isBooleanElement, isNumberElement, isStringElement, isNullElement } from "../../../predicates/index.mjs";
6
- /* eslint-disable @typescript-eslint/naming-convention */
7
- const Visitor = stampit.init(function _Visitor() {
8
- const references = new WeakMap();
9
- this.BooleanElement = function _BooleanElement(element) {
10
- return element.toValue();
11
- };
12
- this.NumberElement = function _NumberElement(element) {
13
- return element.toValue();
14
- };
15
- this.StringElement = function _StringElement(element) {
16
- return element.toValue();
17
- };
18
- this.NullElement = function _NullElement() {
19
- return null;
20
- };
21
- this.ObjectElement = {
22
- enter(element) {
23
- if (references.has(element)) {
24
- return references.get(element).toReference();
5
+ /* eslint-disable class-methods-use-this */
6
+ class Visitor {
7
+ ObjectElement = {
8
+ enter: element => {
9
+ if (this.references.has(element)) {
10
+ return this.references.get(element).toReference();
25
11
  }
26
12
  const ephemeral = new EphemeralObject(element.content);
27
- references.set(element, ephemeral);
13
+ this.references.set(element, ephemeral);
28
14
  return ephemeral;
29
15
  }
30
16
  };
31
- this.EphemeralObject = {
32
- leave(ephemeral) {
17
+ EphemeralObject = {
18
+ leave: ephemeral => {
33
19
  return ephemeral.toObject();
34
20
  }
35
21
  };
36
- this.MemberElement = {
37
- enter(element) {
22
+ MemberElement = {
23
+ enter: element => {
38
24
  return [element.key, element.value];
39
25
  }
40
26
  };
41
- this.ArrayElement = {
42
- enter(element) {
43
- if (references.has(element)) {
44
- return references.get(element).toReference();
27
+ ArrayElement = {
28
+ enter: element => {
29
+ if (this.references.has(element)) {
30
+ return this.references.get(element).toReference();
45
31
  }
46
32
  const ephemeral = new EphemeralArray(element.content);
47
- references.set(element, ephemeral);
33
+ this.references.set(element, ephemeral);
48
34
  return ephemeral;
49
35
  }
50
36
  };
51
- this.EphemeralArray = {
52
- leave(ephemeral) {
37
+ EphemeralArray = {
38
+ leave: ephemeral => {
53
39
  return ephemeral.toArray();
54
40
  }
55
41
  };
56
- });
57
- /* eslint-enable */
58
-
42
+ references = new WeakMap();
43
+ BooleanElement(element) {
44
+ return element.toValue();
45
+ }
46
+ NumberElement(element) {
47
+ return element.toValue();
48
+ }
49
+ StringElement(element) {
50
+ return element.toValue();
51
+ }
52
+ NullElement() {
53
+ return null;
54
+ }
55
+ }
59
56
  const serializer = element => {
60
57
  if (!isElement(element)) return element;
61
58
 
@@ -63,6 +60,6 @@ const serializer = element => {
63
60
  if (isStringElement(element) || isNumberElement(element) || isBooleanElement(element) || isNullElement(element)) {
64
61
  return element.toValue();
65
62
  }
66
- return visit(element, Visitor());
63
+ return visit(element, new Visitor());
67
64
  };
68
65
  export default serializer;
@@ -1,83 +1,76 @@
1
- import stampit from 'stampit';
2
1
  import { visit } from "../../traversal/visitor.mjs";
3
2
  import serializeValue from "./value/index.mjs";
4
- const YamlVisitor = stampit({
5
- props: {
6
- result: '',
7
- indent: 0,
8
- indentChar: ' '
9
- },
10
- init({
3
+ class YamlVisitor {
4
+ static indentChar = ' ';
5
+ constructor({
11
6
  directive = false,
12
7
  indent = 0
13
8
  } = {}) {
14
9
  this.result = directive ? '%YAML 1.2\n---\n' : '';
15
10
  this.indent = indent;
16
- },
17
- methods: {
18
- NumberElement(element) {
19
- this.result += serializeValue(element);
20
- },
21
- BooleanElement(element) {
22
- const value = serializeValue(element);
23
- this.result += value ? 'true' : 'false';
24
- },
25
- StringElement(element) {
26
- // for simplicity and avoiding ambiguity we always wrap strings in quotes
27
- this.result += JSON.stringify(serializeValue(element));
28
- },
29
- NullElement() {
30
- this.result += 'null';
31
- },
32
- ArrayElement(element) {
33
- if (element.length === 0) {
34
- this.result += '[]';
35
- return false;
36
- }
37
- element.forEach(item => {
38
- const visitor = YamlVisitor({
39
- indent: this.indent + 1
40
- });
41
- const indent = this.indentChar.repeat(this.indent);
42
- visit(item, visitor);
43
- const {
44
- result
45
- } = visitor;
46
- this.result += result.startsWith('\n') ? `\n${indent}-${result}` : `\n${indent}- ${result}`;
47
- });
11
+ }
12
+ NumberElement(element) {
13
+ this.result += serializeValue(element);
14
+ }
15
+ BooleanElement(element) {
16
+ const value = serializeValue(element);
17
+ this.result += value ? 'true' : 'false';
18
+ }
19
+ StringElement(element) {
20
+ // for simplicity and avoiding ambiguity we always wrap strings in quotes
21
+ this.result += JSON.stringify(serializeValue(element));
22
+ }
23
+ NullElement() {
24
+ this.result += 'null';
25
+ }
26
+ ArrayElement(element) {
27
+ if (element.length === 0) {
28
+ this.result += '[]';
48
29
  return false;
49
- },
50
- ObjectElement(element) {
51
- if (element.length === 0) {
52
- this.result += '{}';
53
- return false;
54
- }
55
- element.forEach((value, key) => {
56
- const keyVisitor = YamlVisitor({
57
- indent: this.indent + 1
58
- });
59
- const valueVisitor = YamlVisitor({
60
- indent: this.indent + 1
61
- });
62
- const indent = this.indentChar.repeat(this.indent);
63
- visit(key, keyVisitor);
64
- visit(value, valueVisitor);
65
- const {
66
- result: keyResult
67
- } = keyVisitor;
68
- const {
69
- result: valueResult
70
- } = valueVisitor;
71
- this.result += valueResult.startsWith('\n') ? `\n${indent}${keyResult}:${valueResult}` : `\n${indent}${keyResult}: ${valueResult}`;
30
+ }
31
+ element.forEach(item => {
32
+ const visitor = new YamlVisitor({
33
+ indent: this.indent + 1
72
34
  });
35
+ const indent = YamlVisitor.indentChar.repeat(this.indent);
36
+ visit(item, visitor);
37
+ const {
38
+ result
39
+ } = visitor;
40
+ this.result += result.startsWith('\n') ? `\n${indent}-${result}` : `\n${indent}- ${result}`;
41
+ });
42
+ return false;
43
+ }
44
+ ObjectElement(element) {
45
+ if (element.length === 0) {
46
+ this.result += '{}';
73
47
  return false;
74
48
  }
49
+ element.forEach((value, key) => {
50
+ const keyVisitor = new YamlVisitor({
51
+ indent: this.indent + 1
52
+ });
53
+ const valueVisitor = new YamlVisitor({
54
+ indent: this.indent + 1
55
+ });
56
+ const indent = YamlVisitor.indentChar.repeat(this.indent);
57
+ visit(key, keyVisitor);
58
+ visit(value, valueVisitor);
59
+ const {
60
+ result: keyResult
61
+ } = keyVisitor;
62
+ const {
63
+ result: valueResult
64
+ } = valueVisitor;
65
+ this.result += valueResult.startsWith('\n') ? `\n${indent}${keyResult}:${valueResult}` : `\n${indent}${keyResult}: ${valueResult}`;
66
+ });
67
+ return false;
75
68
  }
76
- });
69
+ }
77
70
  const serializer = (element, {
78
71
  directive = false
79
72
  } = {}) => {
80
- const visitor = YamlVisitor({
73
+ const visitor = new YamlVisitor({
81
74
  directive
82
75
  });
83
76
  visit(element, visitor);
@@ -1,31 +1,26 @@
1
- import stampit from 'stampit';
2
1
  import { visit } from "../traversal/visitor.mjs";
3
- const SymbolicExpressionsVisitor = stampit({
4
- props: {
5
- nestingLevel: 0,
6
- result: ''
7
- },
8
- methods: {
9
- enter(element) {
10
- const {
11
- element: elementName
12
- } = element;
13
- const capitalizedElementName = elementName.charAt(0).toUpperCase() + elementName.slice(1);
14
- const indent = ' '.repeat(this.nestingLevel);
15
- this.result += this.nestingLevel > 0 ? '\n' : '';
16
- this.result += `${indent}(${capitalizedElementName}Element`;
17
- this.nestingLevel += 1;
18
- },
19
- leave() {
20
- this.nestingLevel -= 1;
21
- this.result += ')';
22
- }
2
+ class SymbolicExpressionsVisitor {
3
+ result = '';
4
+ nestingLevel = 0;
5
+ enter(element) {
6
+ const {
7
+ element: elementName
8
+ } = element;
9
+ const capitalizedElementName = elementName.charAt(0).toUpperCase() + elementName.slice(1);
10
+ const indent = ' '.repeat(this.nestingLevel);
11
+ this.result += this.nestingLevel > 0 ? '\n' : '';
12
+ this.result += `${indent}(${capitalizedElementName}Element`;
13
+ this.nestingLevel += 1;
23
14
  }
24
- });
15
+ leave() {
16
+ this.nestingLevel -= 1;
17
+ this.result += ')';
18
+ }
19
+ }
25
20
 
26
21
  // transforms ApiDOM into S-expressions (Symbolic Expressions)
27
22
  const sexprs = element => {
28
- const visitor = SymbolicExpressionsVisitor();
23
+ const visitor = new SymbolicExpressionsVisitor();
29
24
  visit(element, visitor);
30
25
  return visitor.result;
31
26
  };
@@ -1,7 +1,7 @@
1
1
  import { ArraySlice } from 'minim';
2
2
  import { PredicateVisitor, visit } from "./visitor.mjs"; // finds all elements matching the predicate
3
3
  const filter = (predicate, element) => {
4
- const visitor = PredicateVisitor({
4
+ const visitor = new PredicateVisitor({
5
5
  predicate
6
6
  });
7
7
  visit(element, visitor);
@@ -1,7 +1,7 @@
1
1
  import { pathOr } from 'ramda';
2
2
  import { PredicateVisitor, BREAK, visit } from "./visitor.mjs"; // find first element that satisfies the provided predicate
3
3
  const find = (predicate, element) => {
4
- const visitor = PredicateVisitor({
4
+ const visitor = new PredicateVisitor({
5
5
  predicate,
6
6
  returnOnTrue: BREAK
7
7
  });
@@ -1,41 +1,32 @@
1
- import stampit from 'stampit';
2
1
  import { last, pathOr } from 'ramda';
3
2
  import { isNumber } from 'ramda-adjunct';
4
3
  import { hasElementSourceMap } from "../predicates/index.mjs";
5
4
  import { visit } from "./visitor.mjs";
6
5
  import toValue from "../transformers/serializers/value/index.mjs";
7
- const Visitor = stampit({
8
- props: {
9
- result: [],
10
- offset: 0,
11
- includeRightBound: false
12
- },
13
- // @ts-ignore
14
- init({
15
- offset = this.offset,
16
- includeRightBound = this.includeRightBound
17
- }) {
6
+ class Visitor {
7
+ constructor({
8
+ offset = 0,
9
+ includeRightBound = false
10
+ } = {}) {
18
11
  this.result = [];
19
12
  this.offset = offset;
20
13
  this.includeRightBound = includeRightBound;
21
- },
22
- methods: {
23
- enter(element) {
24
- if (!hasElementSourceMap(element)) {
25
- return undefined; // dive in
26
- }
27
- const sourceMapElement = element.getMetaProperty('sourceMap');
28
- const charStart = toValue(sourceMapElement.positionStart.get(2));
29
- const charEnd = toValue(sourceMapElement.positionEnd.get(2));
30
- const isWithinOffsetRange = this.offset >= charStart && (this.offset < charEnd || this.includeRightBound && this.offset <= charEnd);
31
- if (isWithinOffsetRange) {
32
- this.result.push(element);
33
- return undefined; // push to stack and dive in
34
- }
35
- return false; // skip entire sub-tree
14
+ }
15
+ enter(element) {
16
+ if (!hasElementSourceMap(element)) {
17
+ return undefined; // dive in
18
+ }
19
+ const sourceMapElement = element.getMetaProperty('sourceMap');
20
+ const charStart = toValue(sourceMapElement.positionStart.get(2));
21
+ const charEnd = toValue(sourceMapElement.positionEnd.get(2));
22
+ const isWithinOffsetRange = this.offset >= charStart && (this.offset < charEnd || this.includeRightBound && this.offset <= charEnd);
23
+ if (isWithinOffsetRange) {
24
+ this.result.push(element);
25
+ return undefined; // push to stack and dive in
36
26
  }
27
+ return false; // skip entire sub-tree
37
28
  }
38
- });
29
+ }
39
30
  // Finds the most inner node at the given offset.
40
31
  // If includeRightBound is set, also finds nodes that end at the given offset.
41
32
  // findAtOffset :: Number -> Element -> Element | Undefined
@@ -49,7 +40,7 @@ const findAtOffset = (options, element) => {
49
40
  offset = pathOr(0, ['offset'], options);
50
41
  includeRightBound = pathOr(false, ['includeRightBound'], options);
51
42
  }
52
- const visitor = Visitor({
43
+ const visitor = new Visitor({
53
44
  offset,
54
45
  includeRightBound
55
46
  });
@@ -1,38 +1,28 @@
1
- import stampit from 'stampit';
2
1
  import { visit } from "./visitor.mjs";
3
- /* eslint-disable no-param-reassign */
4
- const Visitor = stampit({
5
- props: {
6
- parent: null,
7
- parentEdges: null
8
- },
9
- init() {
2
+ class Visitor {
3
+ constructor() {
10
4
  this.parentEdges = new WeakMap();
11
- },
12
- methods: {
13
- ObjectElement(objectElement) {
14
- this.parentEdges.set(objectElement, this.parent);
15
- this.parent = objectElement;
16
- },
17
- ArrayElement(arrayElement) {
18
- this.parentEdges.set(arrayElement, this.parent);
19
- this.parent = arrayElement;
20
- },
21
- MemberElement(memberElement) {
22
- this.parentEdges.set(memberElement, this.parent);
23
- this.parent = memberElement;
24
- },
25
- enter(element) {
26
- this.parentEdges.set(element, this.parent);
27
- }
28
5
  }
29
- });
30
-
31
- /* eslint-enable */
6
+ ObjectElement(objectElement) {
7
+ this.parentEdges.set(objectElement, this.parent);
8
+ this.parent = objectElement;
9
+ }
10
+ ArrayElement(arrayElement) {
11
+ this.parentEdges.set(arrayElement, this.parent);
12
+ this.parent = arrayElement;
13
+ }
14
+ MemberElement(memberElement) {
15
+ this.parentEdges.set(memberElement, this.parent);
16
+ this.parent = memberElement;
17
+ }
18
+ enter(element) {
19
+ this.parentEdges.set(element, this.parent);
20
+ }
21
+ }
32
22
 
33
23
  // computes upwards edges from every child to its parent
34
24
  const parents = element => {
35
- const visitor = Visitor();
25
+ const visitor = new Visitor();
36
26
  visit(element, visitor);
37
27
  return visitor.parentEdges;
38
28
  };
@@ -1,28 +1,25 @@
1
- import stampit from 'stampit';
2
1
  import { pathOr } from 'ramda';
3
2
  import { isFunction, noop } from 'ramda-adjunct';
4
3
  import { visit, PredicateVisitor } from "./visitor.mjs";
5
4
  import { isElement } from "../predicates/index.mjs";
6
- export const CallbackVisitor = stampit(PredicateVisitor, {
7
- props: {
8
- callback: noop
9
- },
10
- // @ts-ignore
11
- init({
12
- callback = this.callback
5
+ export class CallbackVisitor extends PredicateVisitor {
6
+ constructor({
7
+ callback = noop,
8
+ ...rest
13
9
  } = {}) {
10
+ super({
11
+ ...rest
12
+ });
14
13
  this.callback = callback;
15
- },
16
- methods: {
17
- enter(element) {
18
- if (this.predicate(element)) {
19
- this.callback(element);
20
- return this.returnOnTrue;
21
- }
22
- return this.returnOnFalse;
14
+ }
15
+ enter(element) {
16
+ if (this.predicate(element)) {
17
+ this.callback(element);
18
+ return this.returnOnTrue;
23
19
  }
20
+ return this.returnOnFalse;
24
21
  }
25
- });
22
+ }
26
23
 
27
24
  // executes the callback on this element and all descendants
28
25
  const traverse = (options, element) => {
@@ -35,7 +32,7 @@ const traverse = (options, element) => {
35
32
  callback = pathOr(noop, ['callback'], options);
36
33
  predicate = pathOr(isElement, ['predicate'], options);
37
34
  }
38
- const visitor = CallbackVisitor({
35
+ const visitor = new CallbackVisitor({
39
36
  callback,
40
37
  predicate
41
38
  });
@@ -1,4 +1,3 @@
1
- import stampit from 'stampit';
2
1
  import { F as stubFalse, pipe } from 'ramda';
3
2
  import { isString } from 'ramda-adjunct';
4
3
  import { visit as astVisit, BREAK, mergeAllVisitors, cloneNode as cloneNodeDefault } from '@swagger-api/apidom-ast';
@@ -43,38 +42,25 @@ export const keyMapDefault = {
43
42
  ParseResultElement: ['content'],
44
43
  SourceMap: ['content']
45
44
  };
46
- export const PredicateVisitor = stampit({
47
- props: {
48
- result: [],
49
- predicate: stubFalse,
50
- returnOnTrue: undefined,
51
- returnOnFalse: undefined
52
- },
53
- init({
54
- // @ts-ignore
55
- predicate = this.predicate,
56
- // @ts-ignore
57
- returnOnTrue = this.returnOnTrue,
58
- // @ts-ignore
59
- returnOnFalse = this.returnOnFalse
45
+ export class PredicateVisitor {
46
+ constructor({
47
+ predicate = stubFalse,
48
+ returnOnTrue,
49
+ returnOnFalse
60
50
  } = {}) {
61
51
  this.result = [];
62
52
  this.predicate = predicate;
63
53
  this.returnOnTrue = returnOnTrue;
64
54
  this.returnOnFalse = returnOnFalse;
65
- },
66
- methods: {
67
- enter(element) {
68
- if (this.predicate(element)) {
69
- this.result.push(element);
70
- return this.returnOnTrue;
71
- }
72
- return this.returnOnFalse;
55
+ }
56
+ enter(element) {
57
+ if (this.predicate(element)) {
58
+ this.result.push(element);
59
+ return this.returnOnTrue;
73
60
  }
61
+ return this.returnOnFalse;
74
62
  }
75
- });
76
-
77
- // @ts-ignore
63
+ }
78
64
  export const visit = (root,
79
65
  // @ts-ignore
80
66
  visitor, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swagger-api/apidom-core",
3
- "version": "0.91.0",
3
+ "version": "0.93.0",
4
4
  "description": "Tools for manipulating ApiDOM structures.",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -42,8 +42,8 @@
42
42
  "license": "Apache-2.0",
43
43
  "dependencies": {
44
44
  "@babel/runtime-corejs3": "^7.20.7",
45
- "@swagger-api/apidom-ast": "^0.91.0",
46
- "@swagger-api/apidom-error": "^0.91.0",
45
+ "@swagger-api/apidom-ast": "^0.93.0",
46
+ "@swagger-api/apidom-error": "^0.93.0",
47
47
  "@types/ramda": "~0.29.6",
48
48
  "minim": "~0.23.8",
49
49
  "ramda": "~0.29.1",
@@ -62,5 +62,5 @@
62
62
  "README.md",
63
63
  "CHANGELOG.md"
64
64
  ],
65
- "gitHead": "a3b88484e7a38bbf82b04458cda81e4dc0b53519"
65
+ "gitHead": "65ea9068f5d8b144293c2f91eb5fe5ebacfd947c"
66
66
  }