@swagger-api/apidom-parser-adapter-json 0.68.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/CHANGELOG.md +373 -0
- package/LICENSES/Apache-2.0.txt +202 -0
- package/LICENSES/MIT.txt +9 -0
- package/NOTICE +57 -0
- package/README.md +125 -0
- package/cjs/adapter-browser.cjs +46 -0
- package/cjs/adapter-node.cjs +46 -0
- package/cjs/adapter.cjs +14 -0
- package/cjs/lexical-analysis/browser-patch.cjs +20 -0
- package/cjs/lexical-analysis/browser.cjs +42 -0
- package/cjs/lexical-analysis/node.cjs +21 -0
- package/cjs/media-types.cjs +14 -0
- package/cjs/syntactic-analysis/PreOrderCursorChildrenIterator.cjs +33 -0
- package/cjs/syntactic-analysis/PreOrderCusrorIterator.cjs +31 -0
- package/cjs/syntactic-analysis/direct/CursorIterator.cjs +110 -0
- package/cjs/syntactic-analysis/direct/index.cjs +228 -0
- package/cjs/syntactic-analysis/indirect/index.cjs +41 -0
- package/cjs/syntactic-analysis/indirect/visitors/CstVisitor.cjs +190 -0
- package/cjs/syntactic-analysis/indirect/visitors/JsonAstVisitor.cjs +186 -0
- package/dist/7c7ca323880d9fa6e48d1d1b2e78e140.wasm +0 -0
- package/dist/apidom-parser-adapter-json.browser.js +31014 -0
- package/dist/apidom-parser-adapter-json.browser.min.js +1 -0
- package/dist/fba0b3cc0d7ee926ea482deee298a5fe.wasm +0 -0
- package/es/adapter-browser.js +35 -0
- package/es/adapter-node.js +35 -0
- package/es/adapter.js +6 -0
- package/es/lexical-analysis/browser-patch.js +17 -0
- package/es/lexical-analysis/browser.js +36 -0
- package/es/lexical-analysis/node.js +14 -0
- package/es/media-types.js +8 -0
- package/es/syntactic-analysis/PreOrderCursorChildrenIterator.js +32 -0
- package/es/syntactic-analysis/PreOrderCusrorIterator.js +30 -0
- package/es/syntactic-analysis/direct/CursorIterator.js +104 -0
- package/es/syntactic-analysis/direct/index.js +227 -0
- package/es/syntactic-analysis/indirect/index.js +34 -0
- package/es/syntactic-analysis/indirect/visitors/CstVisitor.js +183 -0
- package/es/syntactic-analysis/indirect/visitors/JsonAstVisitor.js +180 -0
- package/package.json +81 -0
- package/types/dist.d.ts +53 -0
- package/wasm/tree-sitter-json.wasm +0 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.default = void 0;
|
|
6
|
+
var _stampit = _interopRequireDefault(require("stampit"));
|
|
7
|
+
var _apidomAst = require("@swagger-api/apidom-ast");
|
|
8
|
+
var _apidomCore = require("@swagger-api/apidom-core");
|
|
9
|
+
const keyMap = {
|
|
10
|
+
document: ['children'],
|
|
11
|
+
object: ['children'],
|
|
12
|
+
array: ['children'],
|
|
13
|
+
string: ['children'],
|
|
14
|
+
property: ['children'],
|
|
15
|
+
key: ['children'],
|
|
16
|
+
error: ['children'],
|
|
17
|
+
..._apidomCore.keyMap
|
|
18
|
+
};
|
|
19
|
+
const getNodeType = node => {
|
|
20
|
+
if ((0, _apidomCore.isParseResultElement)(node)) {
|
|
21
|
+
return 'ParseResultElement';
|
|
22
|
+
}
|
|
23
|
+
if ((0, _apidomCore.isElement)(node)) {
|
|
24
|
+
return (0, _apidomCore.getNodeType)(node);
|
|
25
|
+
}
|
|
26
|
+
return (0, _apidomAst.getNodeType)(node);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
const isNode = element => (0, _apidomCore.isElement)(element) || (0, _apidomAst.isNode)(element);
|
|
31
|
+
const Visitor = (0, _stampit.default)({
|
|
32
|
+
props: {
|
|
33
|
+
sourceMap: false,
|
|
34
|
+
annotations: []
|
|
35
|
+
},
|
|
36
|
+
init() {
|
|
37
|
+
/**
|
|
38
|
+
* Private API.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
this.annotations = [];
|
|
42
|
+
const toPosition = node => {
|
|
43
|
+
if (node === null) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const start = new _apidomCore.ArrayElement([node.startPosition.row, node.startPosition.column, node.startIndex]);
|
|
47
|
+
const end = new _apidomCore.ArrayElement([node.endPosition.row, node.endPosition.column, node.endIndex]);
|
|
48
|
+
start.classes.push('position');
|
|
49
|
+
end.classes.push('position');
|
|
50
|
+
return [start, end];
|
|
51
|
+
};
|
|
52
|
+
const maybeAddSourceMap = (node, element) => {
|
|
53
|
+
if (!this.sourceMap) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const sourceMap = new _apidomCore.SourceMapElement();
|
|
57
|
+
const position = toPosition(node);
|
|
58
|
+
if (position !== null) {
|
|
59
|
+
const [start, end] = position;
|
|
60
|
+
sourceMap.push(start);
|
|
61
|
+
sourceMap.push(end);
|
|
62
|
+
}
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
sourceMap.astNode = node;
|
|
65
|
+
element.meta.set('sourceMap', sourceMap);
|
|
66
|
+
};
|
|
67
|
+
const getFieldFromNode = (fieldName, node) => {
|
|
68
|
+
var _node$childForFieldNa;
|
|
69
|
+
return `${fieldName}Node` in node ?
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
node[`${fieldName}Node`] : 'childForFieldName' in node ? (_node$childForFieldNa = node.childForFieldName) === null || _node$childForFieldNa === void 0 ? void 0 : _node$childForFieldNa.call(node, fieldName) : null;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Public API.
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
this.enter = function enter(node) {
|
|
79
|
+
// missing anonymous literals from CST transformed into AnnotationElements.
|
|
80
|
+
// WARNING: be aware that web-tree-sitter and tree-sitter node bindings have inconsistency
|
|
81
|
+
// in `SyntaxNode.isNamed` property. web-tree-sitter has it defined as method
|
|
82
|
+
// whether tree-sitter node binding has it defined as a boolean property.
|
|
83
|
+
if ((typeof node.isNamed === 'function' && !node.isNamed() || node.isNamed === false) && node.isMissing()) {
|
|
84
|
+
// collect annotations from missing literals
|
|
85
|
+
const value = node.type || node.text;
|
|
86
|
+
const message = `(Missing ${value})`;
|
|
87
|
+
const element = new _apidomCore.AnnotationElement(message);
|
|
88
|
+
element.classes.push('warning');
|
|
89
|
+
maybeAddSourceMap(node, element);
|
|
90
|
+
this.annotations.push(element);
|
|
91
|
+
}
|
|
92
|
+
return null; // remove everything unrecognized
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
this.document = function document(node) {
|
|
96
|
+
const element = new _apidomCore.ParseResultElement();
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
element._content = node.children;
|
|
99
|
+
maybeAddSourceMap(node, element);
|
|
100
|
+
return element;
|
|
101
|
+
};
|
|
102
|
+
this.ParseResultElement = {
|
|
103
|
+
leave(element) {
|
|
104
|
+
// mark first-non Annotation element as result
|
|
105
|
+
// @ts-ignore
|
|
106
|
+
const elements = element.findElements(_apidomCore.isPrimitiveElement);
|
|
107
|
+
if (elements.length > 0) {
|
|
108
|
+
const resultElement = elements[0];
|
|
109
|
+
resultElement.classes.push('result');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// provide annotations
|
|
113
|
+
this.annotations.forEach(annotationElement => {
|
|
114
|
+
element.push(annotationElement);
|
|
115
|
+
});
|
|
116
|
+
this.annotations = [];
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
this.object = function object(node) {
|
|
120
|
+
const element = new _apidomCore.ObjectElement();
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
element._content = node.children;
|
|
123
|
+
maybeAddSourceMap(node, element);
|
|
124
|
+
return element;
|
|
125
|
+
};
|
|
126
|
+
this.array = function array(node) {
|
|
127
|
+
const element = new _apidomCore.ArrayElement();
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
element._content = node.children;
|
|
130
|
+
maybeAddSourceMap(node, element);
|
|
131
|
+
return element;
|
|
132
|
+
};
|
|
133
|
+
this.pair = function pair(node) {
|
|
134
|
+
const element = new _apidomCore.MemberElement();
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
element.content.key = getFieldFromNode('key', node);
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
element.content.value = getFieldFromNode('value', node);
|
|
139
|
+
maybeAddSourceMap(node, element);
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Process possible errors here that may be present in pair node children as we're using direct field access.
|
|
143
|
+
* There are usually 3 children here found: "key", ":", "value".
|
|
144
|
+
*/
|
|
145
|
+
if (node.children.length > 3) {
|
|
146
|
+
node.children
|
|
147
|
+
// @ts-ignore
|
|
148
|
+
.filter(child => child.type === 'ERROR').forEach(errorNode => {
|
|
149
|
+
this.ERROR(errorNode, node, [], [node]);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return element;
|
|
153
|
+
};
|
|
154
|
+
this.string = function string(node) {
|
|
155
|
+
const element = new _apidomCore.StringElement(node.text.slice(1, -1));
|
|
156
|
+
maybeAddSourceMap(node, element);
|
|
157
|
+
return element;
|
|
158
|
+
};
|
|
159
|
+
this.number = function number(node) {
|
|
160
|
+
const element = new _apidomCore.NumberElement(Number(node.text));
|
|
161
|
+
maybeAddSourceMap(node, element);
|
|
162
|
+
return element;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
166
|
+
this.null = function _null(node) {
|
|
167
|
+
const element = new _apidomCore.NullElement();
|
|
168
|
+
maybeAddSourceMap(node, element);
|
|
169
|
+
return element;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
173
|
+
this.true = function _true(node) {
|
|
174
|
+
const element = new _apidomCore.BooleanElement(true);
|
|
175
|
+
maybeAddSourceMap(node, element);
|
|
176
|
+
return element;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
180
|
+
this.false = function _false(node) {
|
|
181
|
+
const element = new _apidomCore.BooleanElement(false);
|
|
182
|
+
maybeAddSourceMap(node, element);
|
|
183
|
+
return element;
|
|
184
|
+
};
|
|
185
|
+
this.ERROR = function ERROR(node, key, parent, path) {
|
|
186
|
+
// collect errors as annotations
|
|
187
|
+
const isUnexpected = !node.hasError();
|
|
188
|
+
const value = node.text;
|
|
189
|
+
const message = isUnexpected ? `(Unexpected ${value})` : `(Error ${value})`;
|
|
190
|
+
const element = new _apidomCore.AnnotationElement(message);
|
|
191
|
+
element.classes.push('error');
|
|
192
|
+
maybeAddSourceMap(node, element);
|
|
193
|
+
if (path.length === 0) {
|
|
194
|
+
// no document to visit, only error is present in CST
|
|
195
|
+
const parseResultElement = new _apidomCore.ParseResultElement();
|
|
196
|
+
parseResultElement.push(element);
|
|
197
|
+
return parseResultElement;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// we have CST node for document
|
|
201
|
+
this.annotations.push(element);
|
|
202
|
+
return null;
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* This version of syntactic analysis translates TreeSitter CTS into ApiDOM.
|
|
209
|
+
* Single traversal pass is needed to get from CST to ApiDOM.
|
|
210
|
+
*/
|
|
211
|
+
const analyze = (cst, {
|
|
212
|
+
sourceMap = false
|
|
213
|
+
} = {}) => {
|
|
214
|
+
const visitor = Visitor();
|
|
215
|
+
return (0, _apidomAst.visit)(cst.rootNode, visitor, {
|
|
216
|
+
// @ts-ignore
|
|
217
|
+
keyMap,
|
|
218
|
+
// @ts-ignore
|
|
219
|
+
nodeTypeGetter: getNodeType,
|
|
220
|
+
// @ts-ignore
|
|
221
|
+
nodePredicate: isNode,
|
|
222
|
+
state: {
|
|
223
|
+
sourceMap
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
var _default = analyze;
|
|
228
|
+
exports.default = _default;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _apidomAst = require("@swagger-api/apidom-ast");
|
|
6
|
+
var _CstVisitor = _interopRequireWildcard(require("./visitors/CstVisitor.cjs"));
|
|
7
|
+
var _JsonAstVisitor = _interopRequireWildcard(require("./visitors/JsonAstVisitor.cjs"));
|
|
8
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
9
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
10
|
+
/**
|
|
11
|
+
* This version of syntactic analysis does following transformations:
|
|
12
|
+
* TreeSitter CST -> JSON AST -> ApiDOM
|
|
13
|
+
* Two traversals passes are needed to get from CST to ApiDOM.
|
|
14
|
+
* This analysis is much slower than the direct one, but allows
|
|
15
|
+
* to do additional analysis magic on JSON AST.
|
|
16
|
+
*/
|
|
17
|
+
const analyze = (cst, {
|
|
18
|
+
sourceMap = false
|
|
19
|
+
} = {}) => {
|
|
20
|
+
const cstVisitor = (0, _CstVisitor.default)();
|
|
21
|
+
const astVisitor = (0, _JsonAstVisitor.default)();
|
|
22
|
+
const jsonAst = (0, _apidomAst.visit)(cst.rootNode, cstVisitor, {
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
keyMap: _CstVisitor.keyMap,
|
|
25
|
+
state: {
|
|
26
|
+
sourceMap
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return (0, _apidomAst.visit)(jsonAst.rootNode, astVisitor, {
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
keyMap: _JsonAstVisitor.keyMap,
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
nodeTypeGetter: _JsonAstVisitor.getNodeType,
|
|
34
|
+
nodePredicate: _JsonAstVisitor.isNode,
|
|
35
|
+
state: {
|
|
36
|
+
sourceMap
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
var _default = analyze;
|
|
41
|
+
exports.default = _default;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.keyMap = exports.default = void 0;
|
|
6
|
+
var _stampit = _interopRequireDefault(require("stampit"));
|
|
7
|
+
var _apidomAst = require("@swagger-api/apidom-ast");
|
|
8
|
+
const keyMap = {
|
|
9
|
+
document: ['children'],
|
|
10
|
+
object: ['children'],
|
|
11
|
+
array: ['children'],
|
|
12
|
+
string: ['children'],
|
|
13
|
+
property: ['children'],
|
|
14
|
+
key: ['children'],
|
|
15
|
+
error: ['children']
|
|
16
|
+
};
|
|
17
|
+
exports.keyMap = keyMap;
|
|
18
|
+
const CstVisitor = (0, _stampit.default)({
|
|
19
|
+
init() {
|
|
20
|
+
/**
|
|
21
|
+
* Private API.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const toPosition = node => {
|
|
25
|
+
if (node === null) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const start = (0, _apidomAst.Point)({
|
|
29
|
+
row: node.startPosition.row,
|
|
30
|
+
column: node.startPosition.column,
|
|
31
|
+
char: node.startIndex
|
|
32
|
+
});
|
|
33
|
+
const end = (0, _apidomAst.Point)({
|
|
34
|
+
row: node.endPosition.row,
|
|
35
|
+
column: node.endPosition.column,
|
|
36
|
+
char: node.endIndex
|
|
37
|
+
});
|
|
38
|
+
return (0, _apidomAst.Position)({
|
|
39
|
+
start,
|
|
40
|
+
end
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
const getFieldFromNode = (fieldName, node) => {
|
|
44
|
+
var _node$childForFieldNa;
|
|
45
|
+
return `${fieldName}Node` in node ?
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
node[`${fieldName}Node`] : 'childForFieldName' in node ? (_node$childForFieldNa = node.childForFieldName) === null || _node$childForFieldNa === void 0 ? void 0 : _node$childForFieldNa.call(node, fieldName) : null;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Public API.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
this.enter = function enter(node) {
|
|
55
|
+
// missing anonymous literals from CST transformed into AST literal nodes
|
|
56
|
+
// WARNING: be aware that web-tree-sitter and tree-sitter node bindings have inconsistency
|
|
57
|
+
// in `SyntaxNode.isNamed` property. web-tree-sitter has it defined as method
|
|
58
|
+
// whether tree-sitter node binding has it defined as a boolean property.
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
if (typeof node.isNamed === 'function' && !node.isNamed() || node.isNamed === false) {
|
|
61
|
+
const position = toPosition(node);
|
|
62
|
+
const value = node.type || node.text;
|
|
63
|
+
const isMissing = node.isMissing();
|
|
64
|
+
return (0, _apidomAst.Literal)({
|
|
65
|
+
value,
|
|
66
|
+
position,
|
|
67
|
+
isMissing
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
};
|
|
72
|
+
this.document = {
|
|
73
|
+
enter(node) {
|
|
74
|
+
const position = toPosition(node);
|
|
75
|
+
return (0, _apidomAst.JsonDocument)({
|
|
76
|
+
children: node.children,
|
|
77
|
+
position,
|
|
78
|
+
isMissing: node.isMissing()
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
leave(document) {
|
|
82
|
+
return (0, _apidomAst.ParseResult)({
|
|
83
|
+
children: [document]
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
this.object = function object(node) {
|
|
88
|
+
const position = toPosition(node);
|
|
89
|
+
return (0, _apidomAst.JsonObject)({
|
|
90
|
+
children: node.children,
|
|
91
|
+
position,
|
|
92
|
+
isMissing: node.isMissing()
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
this.pair = function pair(node) {
|
|
96
|
+
const position = toPosition(node);
|
|
97
|
+
const children = node.children.slice(1);
|
|
98
|
+
const keyNode = getFieldFromNode('key', node);
|
|
99
|
+
const key = (0, _apidomAst.JsonKey)({
|
|
100
|
+
children: (keyNode === null || keyNode === void 0 ? void 0 : keyNode.children) || [],
|
|
101
|
+
position: toPosition(keyNode),
|
|
102
|
+
isMissing: (keyNode === null || keyNode === void 0 ? void 0 : keyNode.isMissing()) || false
|
|
103
|
+
});
|
|
104
|
+
return (0, _apidomAst.JsonProperty)({
|
|
105
|
+
children: [key, ...children],
|
|
106
|
+
position,
|
|
107
|
+
isMissing: node.isMissing()
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
this.array = function array(node) {
|
|
111
|
+
const position = toPosition(node);
|
|
112
|
+
return (0, _apidomAst.JsonArray)({
|
|
113
|
+
children: node.children,
|
|
114
|
+
position,
|
|
115
|
+
isMissing: node.isMissing()
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
this.string = function string(node) {
|
|
119
|
+
const position = toPosition(node);
|
|
120
|
+
const content = (0, _apidomAst.JsonStringContent)({
|
|
121
|
+
value: node.text.slice(1, -1)
|
|
122
|
+
});
|
|
123
|
+
return (0, _apidomAst.JsonString)({
|
|
124
|
+
children: [content],
|
|
125
|
+
position,
|
|
126
|
+
isMissing: node.isMissing()
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
this.number = function number(node) {
|
|
130
|
+
const position = toPosition(node);
|
|
131
|
+
const value = node.text;
|
|
132
|
+
return (0, _apidomAst.JsonNumber)({
|
|
133
|
+
value,
|
|
134
|
+
position,
|
|
135
|
+
isMissing: node.isMissing()
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
140
|
+
this.null = function _null(node) {
|
|
141
|
+
const position = toPosition(node);
|
|
142
|
+
const value = node.text;
|
|
143
|
+
return (0, _apidomAst.JsonNull)({
|
|
144
|
+
value,
|
|
145
|
+
position,
|
|
146
|
+
isMissing: node.isMissing()
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
151
|
+
this.true = function _true(node) {
|
|
152
|
+
const position = toPosition(node);
|
|
153
|
+
const value = node.text;
|
|
154
|
+
return (0, _apidomAst.JsonTrue)({
|
|
155
|
+
value,
|
|
156
|
+
position,
|
|
157
|
+
isMissing: node.isMissing()
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
162
|
+
this.false = function _false(node) {
|
|
163
|
+
const position = toPosition(node);
|
|
164
|
+
const value = node.text;
|
|
165
|
+
return (0, _apidomAst.JsonFalse)({
|
|
166
|
+
value,
|
|
167
|
+
position,
|
|
168
|
+
isMissing: node.isMissing()
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
this.ERROR = function ERROR(node, key, parent, path) {
|
|
172
|
+
const position = toPosition(node);
|
|
173
|
+
const errorNode = (0, _apidomAst.Error)({
|
|
174
|
+
children: node.children,
|
|
175
|
+
position,
|
|
176
|
+
isUnexpected: !node.hasError(),
|
|
177
|
+
isMissing: node.isMissing(),
|
|
178
|
+
value: node.text
|
|
179
|
+
});
|
|
180
|
+
if (path.length === 0) {
|
|
181
|
+
return (0, _apidomAst.ParseResult)({
|
|
182
|
+
children: [errorNode]
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return errorNode;
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
var _default = CstVisitor;
|
|
190
|
+
exports.default = _default;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.keyMap = exports.isNode = exports.getNodeType = exports.default = void 0;
|
|
6
|
+
var _stampit = _interopRequireDefault(require("stampit"));
|
|
7
|
+
var _apidomAst = require("@swagger-api/apidom-ast");
|
|
8
|
+
var _apidomCore = require("@swagger-api/apidom-core");
|
|
9
|
+
const keyMap = {
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
[_apidomAst.ParseResult.type]: ['children'],
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
[_apidomAst.JsonDocument.type]: ['children'],
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
[_apidomAst.JsonObject.type]: ['children'],
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
[_apidomAst.JsonProperty.type]: ['children'],
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
[_apidomAst.JsonArray.type]: ['children'],
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
[_apidomAst.Error.type]: ['children'],
|
|
22
|
+
..._apidomCore.keyMap
|
|
23
|
+
};
|
|
24
|
+
exports.keyMap = keyMap;
|
|
25
|
+
const getNodeType = node => {
|
|
26
|
+
if ((0, _apidomCore.isParseResultElement)(node)) {
|
|
27
|
+
return 'ParseResultElement';
|
|
28
|
+
}
|
|
29
|
+
if ((0, _apidomCore.isElement)(node)) {
|
|
30
|
+
return (0, _apidomCore.getNodeType)(node);
|
|
31
|
+
}
|
|
32
|
+
return (0, _apidomAst.getNodeType)(node);
|
|
33
|
+
};
|
|
34
|
+
exports.getNodeType = getNodeType;
|
|
35
|
+
const isNode = element => (0, _apidomCore.isElement)(element) || (0, _apidomAst.isNode)(element);
|
|
36
|
+
|
|
37
|
+
/* eslint-disable no-underscore-dangle */
|
|
38
|
+
exports.isNode = isNode;
|
|
39
|
+
const JsonAstVisitor = (0, _stampit.default)({
|
|
40
|
+
props: {
|
|
41
|
+
sourceMap: false,
|
|
42
|
+
annotations: []
|
|
43
|
+
},
|
|
44
|
+
init() {
|
|
45
|
+
/**
|
|
46
|
+
* Private API.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
this.annotation = [];
|
|
50
|
+
const maybeAddSourceMap = (node, element) => {
|
|
51
|
+
if (!this.sourceMap) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const sourceMap = new _apidomCore.SourceMapElement();
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
sourceMap.position = node.position;
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
sourceMap.astNode = node;
|
|
59
|
+
element.meta.set('sourceMap', sourceMap);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Public API.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
this.document = function document(node) {
|
|
67
|
+
const element = new _apidomCore.ParseResultElement();
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
element._content = node.children;
|
|
70
|
+
return element;
|
|
71
|
+
};
|
|
72
|
+
this.ParseResultElement = {
|
|
73
|
+
leave(element) {
|
|
74
|
+
// mark first-non Annotation element as result
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
const elements = element.findElements(_apidomCore.isPrimitiveElement);
|
|
77
|
+
if (elements.length > 0) {
|
|
78
|
+
const resultElement = elements[0];
|
|
79
|
+
resultElement.classes.push('result');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// provide annotations
|
|
83
|
+
this.annotations.forEach(annotationElement => {
|
|
84
|
+
element.push(annotationElement);
|
|
85
|
+
});
|
|
86
|
+
this.annotations = [];
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
this.object = function object(node) {
|
|
90
|
+
const element = new _apidomCore.ObjectElement();
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
element._content = node.children;
|
|
93
|
+
maybeAddSourceMap(node, element);
|
|
94
|
+
return element;
|
|
95
|
+
};
|
|
96
|
+
this.property = function property(node) {
|
|
97
|
+
const element = new _apidomCore.MemberElement();
|
|
98
|
+
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
element.content.key = node.key;
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
element.content.value = node.value;
|
|
103
|
+
maybeAddSourceMap(node, element);
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Process possible errors here that may be present in pair node children as we're using direct field access.
|
|
107
|
+
* There are usually 3 children here found: "key", ":", "value".
|
|
108
|
+
*/
|
|
109
|
+
if (node.children.length > 3) {
|
|
110
|
+
node.children.filter(child => child.type === 'error').forEach(errorNode => {
|
|
111
|
+
this.error(errorNode, node, [], [node]);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return element;
|
|
115
|
+
};
|
|
116
|
+
this.key = function key(node) {
|
|
117
|
+
const element = new _apidomCore.StringElement(node.value);
|
|
118
|
+
maybeAddSourceMap(node, element);
|
|
119
|
+
return element;
|
|
120
|
+
};
|
|
121
|
+
this.array = function array(node) {
|
|
122
|
+
const element = new _apidomCore.ArrayElement();
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
element._content = node.children;
|
|
125
|
+
maybeAddSourceMap(node, element);
|
|
126
|
+
return element;
|
|
127
|
+
};
|
|
128
|
+
this.string = function string(node) {
|
|
129
|
+
const element = new _apidomCore.StringElement(node.value);
|
|
130
|
+
maybeAddSourceMap(node, element);
|
|
131
|
+
return element;
|
|
132
|
+
};
|
|
133
|
+
this.number = function number(node) {
|
|
134
|
+
const element = new _apidomCore.NumberElement(Number(node.value));
|
|
135
|
+
maybeAddSourceMap(node, element);
|
|
136
|
+
return element;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
140
|
+
this.null = function _null(node) {
|
|
141
|
+
const element = new _apidomCore.NullElement();
|
|
142
|
+
maybeAddSourceMap(node, element);
|
|
143
|
+
return element;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
147
|
+
this.true = function _true(node) {
|
|
148
|
+
const element = new _apidomCore.BooleanElement(true);
|
|
149
|
+
maybeAddSourceMap(node, element);
|
|
150
|
+
return element;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
154
|
+
this.false = function _false(node) {
|
|
155
|
+
const element = new _apidomCore.BooleanElement(false);
|
|
156
|
+
maybeAddSourceMap(node, element);
|
|
157
|
+
return element;
|
|
158
|
+
};
|
|
159
|
+
this.literal = function literal(node) {
|
|
160
|
+
if (node.isMissing) {
|
|
161
|
+
const message = `(Missing ${node.value})`;
|
|
162
|
+
const element = new _apidomCore.AnnotationElement(message);
|
|
163
|
+
element.classes.push('warning');
|
|
164
|
+
maybeAddSourceMap(node, element);
|
|
165
|
+
this.annotations.push(element);
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
};
|
|
169
|
+
this.error = function error(node, key, parent, path) {
|
|
170
|
+
const message = node.isUnexpected ? `(Unexpected ${node.value})` : `(Error ${node.value})`;
|
|
171
|
+
const element = new _apidomCore.AnnotationElement(message);
|
|
172
|
+
element.classes.push('error');
|
|
173
|
+
maybeAddSourceMap(node, element);
|
|
174
|
+
if (path.length === 0) {
|
|
175
|
+
// no document to visit, only error is present in CST
|
|
176
|
+
const parseResultElement = new _apidomCore.ParseResultElement();
|
|
177
|
+
parseResultElement.push(element);
|
|
178
|
+
return parseResultElement;
|
|
179
|
+
}
|
|
180
|
+
this.annotations.push(element);
|
|
181
|
+
return null;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
var _default = JsonAstVisitor;
|
|
186
|
+
exports.default = _default;
|
|
Binary file
|