@swagger-api/apidom-parser-adapter-json 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.
@@ -41,7 +41,7 @@ const isNode = element => isElement(element) || isCSTNode(element);
41
41
  const analyze = (cst, {
42
42
  sourceMap = false
43
43
  } = {}) => {
44
- const visitor = CstVisitor();
44
+ const visitor = new CstVisitor();
45
45
  const cursor = cst.walk();
46
46
  const iterator = new TreeCursorIterator(cursor);
47
47
  const [rootNode] = Array.from(iterator);
@@ -1,167 +1,154 @@
1
- import stampit from 'stampit';
2
1
  import { BooleanElement, NullElement, NumberElement, ParseResultElement, SourceMapElement, MemberElement, ObjectElement, ArrayElement, StringElement, AnnotationElement, isPrimitiveElement } from '@swagger-api/apidom-core';
3
2
  import TreeCursorSyntaxNode from "../../TreeCursorSyntaxNode.mjs";
4
3
  /* eslint-disable no-underscore-dangle */
5
- const CstVisitor = stampit({
6
- props: {
7
- sourceMap: false,
8
- annotations: []
9
- },
10
- init() {
11
- /**
12
- * Private API.
13
- */
4
+ class CstVisitor {
5
+ static toPosition(node) {
6
+ const start = new ArrayElement([node.startPosition.row, node.startPosition.column, node.startIndex]);
7
+ const end = new ArrayElement([node.endPosition.row, node.endPosition.column, node.endIndex]);
8
+ start.classes.push('position');
9
+ end.classes.push('position');
10
+ return [start, end];
11
+ }
12
+ sourceMap = false;
13
+ ParseResultElement = {
14
+ leave: element => {
15
+ // mark first-non Annotation element as result
16
+ // @ts-ignore
17
+ const elements = element.findElements(isPrimitiveElement);
18
+ if (elements.length > 0) {
19
+ const resultElement = elements[0];
20
+ resultElement.classes.push('result');
21
+ }
14
22
 
23
+ // provide annotations
24
+ this.annotations.forEach(annotationElement => {
25
+ element.push(annotationElement);
26
+ });
27
+ this.annotations = [];
28
+ }
29
+ };
30
+ constructor() {
15
31
  this.annotations = [];
16
- const toPosition = node => {
17
- const start = new ArrayElement([node.startPosition.row, node.startPosition.column, node.startIndex]);
18
- const end = new ArrayElement([node.endPosition.row, node.endPosition.column, node.endIndex]);
19
- start.classes.push('position');
20
- end.classes.push('position');
21
- return [start, end];
22
- };
23
- const maybeAddSourceMap = (node, element) => {
24
- if (!this.sourceMap) {
25
- return;
26
- }
27
- const sourceMap = new SourceMapElement();
28
- const position = toPosition(node);
29
- if (position !== null) {
30
- const [start, end] = position;
31
- sourceMap.push(start);
32
- sourceMap.push(end);
33
- }
34
- // @ts-ignore
35
- sourceMap.astNode = node;
36
- element.meta.set('sourceMap', sourceMap);
37
- };
32
+ }
33
+ enter(node) {
34
+ // missing anonymous literals from CST transformed into AnnotationElements.
35
+ if (node instanceof TreeCursorSyntaxNode && !node.isNamed && node.isMissing) {
36
+ // collect annotations from missing literals
37
+ const value = node.type || node.text;
38
+ const message = `(Missing ${value})`;
39
+ const element = new AnnotationElement(message);
40
+ element.classes.push('warning');
41
+ this.maybeAddSourceMap(node, element);
42
+ this.annotations.push(element);
43
+ }
44
+ return null; // remove everything unrecognized
45
+ }
46
+ document(node) {
47
+ const element = new ParseResultElement();
48
+ // @ts-ignore
49
+ element._content = node.children;
50
+ this.maybeAddSourceMap(node, element);
51
+ return element;
52
+ }
53
+ object(node) {
54
+ const element = new ObjectElement();
55
+ // @ts-ignore
56
+ element._content = node.children;
57
+ this.maybeAddSourceMap(node, element);
58
+ return element;
59
+ }
60
+ array(node) {
61
+ const element = new ArrayElement();
62
+ // @ts-ignore
63
+ element._content = node.children;
64
+ this.maybeAddSourceMap(node, element);
65
+ return element;
66
+ }
67
+ pair(node) {
68
+ const element = new MemberElement();
69
+ // @ts-ignore
70
+ element.content.key = node.keyNode;
71
+ // @ts-ignore
72
+ element.content.value = node.valueNode;
73
+ this.maybeAddSourceMap(node, element);
38
74
 
39
75
  /**
40
- * Public API.
76
+ * Process possible errors here that may be present in pair node children as we're using direct field access.
77
+ * There are usually 3 children here found: "key", ":", "value".
41
78
  */
79
+ if (node.children.length > 3) {
80
+ node.children.filter(child => child.type === 'ERROR').forEach(errorNode => {
81
+ this.ERROR(errorNode, node, [], [node]);
82
+ });
83
+ }
84
+ return element;
85
+ }
86
+ string(node) {
87
+ const element = new StringElement(JSON.parse(node.text));
88
+ this.maybeAddSourceMap(node, element);
89
+ return element;
90
+ }
91
+ number(node) {
92
+ const element = new NumberElement(Number(node.text));
93
+ this.maybeAddSourceMap(node, element);
94
+ return element;
95
+ }
42
96
 
43
- this.enter = function enter(node) {
44
- // missing anonymous literals from CST transformed into AnnotationElements.
45
- if (node instanceof TreeCursorSyntaxNode && !node.isNamed && node.isMissing) {
46
- // collect annotations from missing literals
47
- const value = node.type || node.text;
48
- const message = `(Missing ${value})`;
49
- const element = new AnnotationElement(message);
50
- element.classes.push('warning');
51
- maybeAddSourceMap(node, element);
52
- this.annotations.push(element);
53
- }
54
- return null; // remove everything unrecognized
55
- };
56
- this.document = function document(node) {
57
- const element = new ParseResultElement();
58
- // @ts-ignore
59
- element._content = node.children;
60
- maybeAddSourceMap(node, element);
61
- return element;
62
- };
63
- this.ParseResultElement = {
64
- leave(element) {
65
- // mark first-non Annotation element as result
66
- // @ts-ignore
67
- const elements = element.findElements(isPrimitiveElement);
68
- if (elements.length > 0) {
69
- const resultElement = elements[0];
70
- resultElement.classes.push('result');
71
- }
72
-
73
- // provide annotations
74
- this.annotations.forEach(annotationElement => {
75
- element.push(annotationElement);
76
- });
77
- this.annotations = [];
78
- }
79
- };
80
- this.object = function object(node) {
81
- const element = new ObjectElement();
82
- // @ts-ignore
83
- element._content = node.children;
84
- maybeAddSourceMap(node, element);
85
- return element;
86
- };
87
- this.array = function array(node) {
88
- const element = new ArrayElement();
89
- // @ts-ignore
90
- element._content = node.children;
91
- maybeAddSourceMap(node, element);
92
- return element;
93
- };
94
- this.pair = function pair(node) {
95
- const element = new MemberElement();
96
- // @ts-ignore
97
- element.content.key = node.keyNode;
98
- // @ts-ignore
99
- element.content.value = node.valueNode;
100
- maybeAddSourceMap(node, element);
101
-
102
- /**
103
- * Process possible errors here that may be present in pair node children as we're using direct field access.
104
- * There are usually 3 children here found: "key", ":", "value".
105
- */
106
- if (node.children.length > 3) {
107
- node.children.filter(child => child.type === 'ERROR').forEach(errorNode => {
108
- this.ERROR(errorNode, node, [], [node]);
109
- });
110
- }
111
- return element;
112
- };
113
- this.string = function string(node) {
114
- const element = new StringElement(JSON.parse(node.text));
115
- maybeAddSourceMap(node, element);
116
- return element;
117
- };
118
- this.number = function number(node) {
119
- const element = new NumberElement(Number(node.text));
120
- maybeAddSourceMap(node, element);
121
- return element;
122
- };
123
-
124
- // eslint-disable-next-line @typescript-eslint/naming-convention
125
- this.null = function _null(node) {
126
- const element = new NullElement();
127
- maybeAddSourceMap(node, element);
128
- return element;
129
- };
97
+ // eslint-disable-next-line @typescript-eslint/naming-convention
98
+ null(node) {
99
+ const element = new NullElement();
100
+ this.maybeAddSourceMap(node, element);
101
+ return element;
102
+ }
130
103
 
131
- // eslint-disable-next-line @typescript-eslint/naming-convention
132
- this.true = function _true(node) {
133
- const element = new BooleanElement(true);
134
- maybeAddSourceMap(node, element);
135
- return element;
136
- };
104
+ // eslint-disable-next-line @typescript-eslint/naming-convention
105
+ true(node) {
106
+ const element = new BooleanElement(true);
107
+ this.maybeAddSourceMap(node, element);
108
+ return element;
109
+ }
137
110
 
138
- // eslint-disable-next-line @typescript-eslint/naming-convention
139
- this.false = function _false(node) {
140
- const element = new BooleanElement(false);
141
- maybeAddSourceMap(node, element);
142
- return element;
143
- };
144
- this.ERROR = function ERROR(node, key, parent, path) {
145
- // collect errors as annotations
146
- const isUnexpected = !node.hasError;
147
- const value = node.text;
148
- const message = isUnexpected ? `(Unexpected ${value})` : `(Error ${value})`;
149
- const element = new AnnotationElement(message);
150
- element.classes.push('error');
151
- maybeAddSourceMap(node, element);
152
- if (path.length === 0) {
153
- // no document to visit, only error is present in CST
154
- const parseResultElement = new ParseResultElement();
155
- parseResultElement.push(element);
156
- return parseResultElement;
157
- }
111
+ // eslint-disable-next-line @typescript-eslint/naming-convention
112
+ false(node) {
113
+ const element = new BooleanElement(false);
114
+ this.maybeAddSourceMap(node, element);
115
+ return element;
116
+ }
117
+ ERROR(node, key, parent, path) {
118
+ // collect errors as annotations
119
+ const isUnexpected = !node.hasError;
120
+ const value = node.text;
121
+ const message = isUnexpected ? `(Unexpected ${value})` : `(Error ${value})`;
122
+ const element = new AnnotationElement(message);
123
+ element.classes.push('error');
124
+ this.maybeAddSourceMap(node, element);
125
+ if (path.length === 0) {
126
+ // no document to visit, only error is present in CST
127
+ const parseResultElement = new ParseResultElement();
128
+ parseResultElement.push(element);
129
+ return parseResultElement;
130
+ }
158
131
 
159
- // we have CST node for document
160
- this.annotations.push(element);
161
- return null;
162
- };
132
+ // we have CST node for document
133
+ this.annotations.push(element);
134
+ return null;
135
+ }
136
+ maybeAddSourceMap(node, element) {
137
+ if (!this.sourceMap) {
138
+ return;
139
+ }
140
+ const sourceMap = new SourceMapElement();
141
+ const position = CstVisitor.toPosition(node);
142
+ if (position !== null) {
143
+ const [start, end] = position;
144
+ sourceMap.push(start);
145
+ sourceMap.push(end);
146
+ }
147
+ // @ts-ignore
148
+ sourceMap.astNode = node;
149
+ element.meta.set('sourceMap', sourceMap);
163
150
  }
164
- });
151
+ }
165
152
 
166
153
  /* eslint-enable no-underscore-dangle */
167
154
 
@@ -23,8 +23,8 @@ const analyze = (cst, {
23
23
  const cursor = cst.walk();
24
24
  const iterator = new TreeCursorIterator(cursor);
25
25
  const [rootNode] = Array.from(iterator);
26
- const cstVisitor = CstVisitor();
27
- const astVisitor = JsonAstVisitor();
26
+ const cstVisitor = new CstVisitor();
27
+ const astVisitor = new JsonAstVisitor();
28
28
  const jsonAst = visit(rootNode, cstVisitor, {
29
29
  // @ts-ignore
30
30
  keyMap: cstKeyMap,
@@ -1,4 +1,3 @@
1
- import stampit from 'stampit';
2
1
  import { JsonArray, JsonDocument, JsonFalse, JsonNull, JsonNumber, JsonObject, JsonKey, JsonProperty, JsonString, JsonStringContent, JsonTrue, ParseResult, Position, Point, Literal, Error } from '@swagger-api/apidom-ast';
3
2
  import TreeCursorSyntaxNode from "../../TreeCursorSyntaxNode.mjs";
4
3
  export const keyMap = {
@@ -10,166 +9,158 @@ export const keyMap = {
10
9
  key: ['children'],
11
10
  error: ['children']
12
11
  };
13
- const CstVisitor = stampit({
14
- init() {
15
- /**
16
- * Private API.
17
- */
18
12
 
19
- const toPosition = node => {
20
- const start = Point({
21
- row: node.startPosition.row,
22
- column: node.startPosition.column,
23
- char: node.startIndex
24
- });
25
- const end = Point({
26
- row: node.endPosition.row,
27
- column: node.endPosition.column,
28
- char: node.endIndex
29
- });
30
- return Position({
31
- start,
32
- end
33
- });
34
- };
13
+ /* eslint-disable class-methods-use-this */
35
14
 
36
- /**
37
- * Public API.
38
- */
39
-
40
- this.enter = function enter(node) {
41
- // anonymous literals from CST transformed into AST literal nodes
42
- if (node instanceof TreeCursorSyntaxNode && !node.isNamed) {
43
- const position = toPosition(node);
44
- const value = node.type || node.text;
45
- const {
46
- isMissing
47
- } = node;
48
- return Literal({
49
- value,
50
- position,
51
- isMissing
52
- });
53
- }
54
- return undefined;
55
- };
56
- this.document = {
57
- enter(node) {
58
- const position = toPosition(node);
59
- return JsonDocument({
60
- children: node.children,
61
- position,
62
- isMissing: node.isMissing
63
- });
64
- },
65
- leave(document) {
66
- return ParseResult({
67
- children: [document]
68
- });
69
- }
70
- };
71
- this.object = function object(node) {
72
- const position = toPosition(node);
73
- return JsonObject({
15
+ class CstVisitor {
16
+ static toPosition(node) {
17
+ const start = new Point({
18
+ row: node.startPosition.row,
19
+ column: node.startPosition.column,
20
+ char: node.startIndex
21
+ });
22
+ const end = new Point({
23
+ row: node.endPosition.row,
24
+ column: node.endPosition.column,
25
+ char: node.endIndex
26
+ });
27
+ return new Position({
28
+ start,
29
+ end
30
+ });
31
+ }
32
+ document = {
33
+ enter: node => {
34
+ const position = CstVisitor.toPosition(node);
35
+ return new JsonDocument({
74
36
  children: node.children,
75
37
  position,
76
38
  isMissing: node.isMissing
77
39
  });
78
- };
79
- this.pair = function pair(node) {
80
- const position = toPosition(node);
81
- const children = node.children.slice(1);
40
+ },
41
+ leave: document => {
42
+ return new ParseResult({
43
+ children: [document]
44
+ });
45
+ }
46
+ };
47
+ enter(node) {
48
+ // anonymous literals from CST transformed into AST literal nodes
49
+ if (node instanceof TreeCursorSyntaxNode && !node.isNamed) {
50
+ const position = CstVisitor.toPosition(node);
51
+ const value = node.type || node.text;
82
52
  const {
83
- keyNode
53
+ isMissing
84
54
  } = node;
85
- const key = JsonKey({
86
- children: (keyNode === null || keyNode === void 0 ? void 0 : keyNode.children) || [],
87
- position: keyNode != null ? toPosition(keyNode) : null,
88
- isMissing: keyNode != null ? keyNode.isMissing : false
89
- });
90
- return JsonProperty({
91
- children: [key, ...children],
92
- position,
93
- isMissing: node.isMissing
94
- });
95
- };
96
- this.array = function array(node) {
97
- const position = toPosition(node);
98
- return JsonArray({
99
- children: node.children,
100
- position,
101
- isMissing: node.isMissing
102
- });
103
- };
104
- this.string = function string(node) {
105
- const position = toPosition(node);
106
- const content = JsonStringContent({
107
- value: JSON.parse(node.text)
108
- });
109
- return JsonString({
110
- children: [content],
111
- position,
112
- isMissing: node.isMissing
113
- });
114
- };
115
- this.number = function number(node) {
116
- const position = toPosition(node);
117
- const value = node.text;
118
- return JsonNumber({
55
+ return new Literal({
119
56
  value,
120
57
  position,
121
- isMissing: node.isMissing
58
+ isMissing
122
59
  });
123
- };
60
+ }
61
+ return undefined;
62
+ }
63
+ object(node) {
64
+ const position = CstVisitor.toPosition(node);
65
+ return new JsonObject({
66
+ children: node.children,
67
+ position,
68
+ isMissing: node.isMissing
69
+ });
70
+ }
71
+ pair(node) {
72
+ const position = CstVisitor.toPosition(node);
73
+ const children = node.children.slice(1);
74
+ const {
75
+ keyNode
76
+ } = node;
77
+ const key = new JsonKey({
78
+ children: (keyNode === null || keyNode === void 0 ? void 0 : keyNode.children) || [],
79
+ position: keyNode != null ? CstVisitor.toPosition(keyNode) : undefined,
80
+ isMissing: keyNode != null ? keyNode.isMissing : false
81
+ });
82
+ return new JsonProperty({
83
+ children: [key, ...children],
84
+ position,
85
+ isMissing: node.isMissing
86
+ });
87
+ }
88
+ array(node) {
89
+ const position = CstVisitor.toPosition(node);
90
+ return new JsonArray({
91
+ children: node.children,
92
+ position,
93
+ isMissing: node.isMissing
94
+ });
95
+ }
96
+ string(node) {
97
+ const position = CstVisitor.toPosition(node);
98
+ const content = new JsonStringContent({
99
+ value: JSON.parse(node.text)
100
+ });
101
+ return new JsonString({
102
+ children: [content],
103
+ position,
104
+ isMissing: node.isMissing
105
+ });
106
+ }
107
+ number(node) {
108
+ const position = CstVisitor.toPosition(node);
109
+ const value = node.text;
110
+ return new JsonNumber({
111
+ value,
112
+ position,
113
+ isMissing: node.isMissing
114
+ });
115
+ }
124
116
 
125
- // eslint-disable-next-line @typescript-eslint/naming-convention
126
- this.null = function _null(node) {
127
- const position = toPosition(node);
128
- const value = node.text;
129
- return JsonNull({
130
- value,
131
- position,
132
- isMissing: node.isMissing
133
- });
134
- };
117
+ // eslint-disable-next-line @typescript-eslint/naming-convention
118
+ null(node) {
119
+ const position = CstVisitor.toPosition(node);
120
+ const value = node.text;
121
+ return new JsonNull({
122
+ value,
123
+ position,
124
+ isMissing: node.isMissing
125
+ });
126
+ }
135
127
 
136
- // eslint-disable-next-line @typescript-eslint/naming-convention
137
- this.true = function _true(node) {
138
- const position = toPosition(node);
139
- const value = node.text;
140
- return JsonTrue({
141
- value,
142
- position,
143
- isMissing: node.isMissing
144
- });
145
- };
128
+ // eslint-disable-next-line @typescript-eslint/naming-convention
129
+ true(node) {
130
+ const position = CstVisitor.toPosition(node);
131
+ const value = node.text;
132
+ return new JsonTrue({
133
+ value,
134
+ position,
135
+ isMissing: node.isMissing
136
+ });
137
+ }
146
138
 
147
- // eslint-disable-next-line @typescript-eslint/naming-convention
148
- this.false = function _false(node) {
149
- const position = toPosition(node);
150
- const value = node.text;
151
- return JsonFalse({
152
- value,
153
- position,
154
- isMissing: node.isMissing
155
- });
156
- };
157
- this.ERROR = function ERROR(node, key, parent, path) {
158
- const position = toPosition(node);
159
- const errorNode = Error({
160
- children: node.children,
161
- position,
162
- isUnexpected: !node.hasError,
163
- isMissing: node.isMissing,
164
- value: node.text
139
+ // eslint-disable-next-line @typescript-eslint/naming-convention
140
+ false(node) {
141
+ const position = CstVisitor.toPosition(node);
142
+ const value = node.text;
143
+ return new JsonFalse({
144
+ value,
145
+ position,
146
+ isMissing: node.isMissing
147
+ });
148
+ }
149
+ ERROR(node, key, parent, path) {
150
+ const position = CstVisitor.toPosition(node);
151
+ const errorNode = new Error({
152
+ children: node.children,
153
+ position,
154
+ isUnexpected: !node.hasError,
155
+ isMissing: node.isMissing,
156
+ value: node.text
157
+ });
158
+ if (path.length === 0) {
159
+ return new ParseResult({
160
+ children: [errorNode]
165
161
  });
166
- if (path.length === 0) {
167
- return ParseResult({
168
- children: [errorNode]
169
- });
170
- }
171
- return errorNode;
172
- };
162
+ }
163
+ return errorNode;
173
164
  }
174
- });
165
+ }
175
166
  export default CstVisitor;