@speclynx/apidom-parser-adapter-json 1.12.2 → 2.1.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.
- package/CHANGELOG.md +24 -0
- package/NOTICE +16 -7
- package/README.md +36 -25
- package/dist/167.apidom-parser-adapter-json.browser.min.js +1 -1
- package/dist/451.apidom-parser-adapter-json.browser.min.js +1 -1
- package/dist/apidom-parser-adapter-json.browser.js +3101 -12890
- package/dist/apidom-parser-adapter-json.browser.min.js +1 -1
- package/package.json +7 -11
- package/src/adapter.cjs +29 -34
- package/src/adapter.mjs +26 -31
- package/src/native/index.cjs +30 -0
- package/src/native/index.mjs +25 -0
- package/src/tree-sitter/index.cjs +54 -0
- package/src/tree-sitter/index.mjs +48 -0
- package/src/{lexical-analysis → tree-sitter/lexical-analysis}/index.cjs +1 -1
- package/src/{lexical-analysis → tree-sitter/lexical-analysis}/index.mjs +1 -1
- package/src/tree-sitter/syntactic-analysis/index.cjs +223 -0
- package/src/tree-sitter/syntactic-analysis/index.mjs +219 -0
- package/types/apidom-parser-adapter-json.d.ts +14 -33
- package/src/syntactic-analysis/TreeCursorIterator.cjs +0 -62
- package/src/syntactic-analysis/TreeCursorIterator.mjs +0 -57
- package/src/syntactic-analysis/TreeCursorSyntaxNode.cjs +0 -51
- package/src/syntactic-analysis/TreeCursorSyntaxNode.mjs +0 -47
- package/src/syntactic-analysis/direct/index.cjs +0 -64
- package/src/syntactic-analysis/direct/index.mjs +0 -59
- package/src/syntactic-analysis/direct/visitors/CstVisitor.cjs +0 -163
- package/src/syntactic-analysis/direct/visitors/CstVisitor.mjs +0 -158
- package/src/syntactic-analysis/indirect/index.cjs +0 -56
- package/src/syntactic-analysis/indirect/index.mjs +0 -49
- package/src/syntactic-analysis/indirect/visitors/CstVisitor.cjs +0 -177
- package/src/syntactic-analysis/indirect/visitors/CstVisitor.mjs +0 -172
- package/src/syntactic-analysis/indirect/visitors/JsonAstVisitor.cjs +0 -189
- package/src/syntactic-analysis/indirect/visitors/JsonAstVisitor.mjs +0 -183
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
|
4
|
-
exports.__esModule = true;
|
|
5
|
-
exports.keyMap = exports.default = void 0;
|
|
6
|
-
var _apidomAst = require("@speclynx/apidom-ast");
|
|
7
|
-
var _TreeCursorSyntaxNode = _interopRequireDefault(require("../../TreeCursorSyntaxNode.cjs"));
|
|
8
|
-
const keyMap = exports.keyMap = {
|
|
9
|
-
document: ['children'],
|
|
10
|
-
object: ['children'],
|
|
11
|
-
array: ['children'],
|
|
12
|
-
string: ['children'],
|
|
13
|
-
property: ['children'],
|
|
14
|
-
key: ['children'],
|
|
15
|
-
error: ['children']
|
|
16
|
-
};
|
|
17
|
-
class CstVisitor {
|
|
18
|
-
static toPosition(node) {
|
|
19
|
-
const start = new _apidomAst.Point({
|
|
20
|
-
row: node.startPosition.row,
|
|
21
|
-
column: node.startPosition.column,
|
|
22
|
-
char: node.startIndex
|
|
23
|
-
});
|
|
24
|
-
const end = new _apidomAst.Point({
|
|
25
|
-
row: node.endPosition.row,
|
|
26
|
-
column: node.endPosition.column,
|
|
27
|
-
char: node.endIndex
|
|
28
|
-
});
|
|
29
|
-
return new _apidomAst.Position({
|
|
30
|
-
start,
|
|
31
|
-
end
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
document = {
|
|
35
|
-
enter: node => {
|
|
36
|
-
const position = CstVisitor.toPosition(node);
|
|
37
|
-
return new _apidomAst.JsonDocument({
|
|
38
|
-
children: node.children,
|
|
39
|
-
position,
|
|
40
|
-
isMissing: node.isMissing
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
leave: document => {
|
|
44
|
-
return new _apidomAst.ParseResult({
|
|
45
|
-
children: [document]
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
enter(node) {
|
|
50
|
-
// anonymous literals from CST transformed into AST literal nodes
|
|
51
|
-
if (node instanceof _TreeCursorSyntaxNode.default && !node.isNamed) {
|
|
52
|
-
const position = CstVisitor.toPosition(node);
|
|
53
|
-
const value = node.type || node.text;
|
|
54
|
-
const {
|
|
55
|
-
isMissing
|
|
56
|
-
} = node;
|
|
57
|
-
return new _apidomAst.Literal({
|
|
58
|
-
value,
|
|
59
|
-
position,
|
|
60
|
-
isMissing
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
object(node) {
|
|
66
|
-
const position = CstVisitor.toPosition(node);
|
|
67
|
-
return new _apidomAst.JsonObject({
|
|
68
|
-
children: node.children,
|
|
69
|
-
position,
|
|
70
|
-
isMissing: node.isMissing
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
pair(node) {
|
|
74
|
-
const position = CstVisitor.toPosition(node);
|
|
75
|
-
const children = node.children.slice(1);
|
|
76
|
-
const {
|
|
77
|
-
keyNode
|
|
78
|
-
} = node;
|
|
79
|
-
let jsonKeyValue;
|
|
80
|
-
let parseError;
|
|
81
|
-
try {
|
|
82
|
-
jsonKeyValue = JSON.parse(keyNode?.text ?? '""');
|
|
83
|
-
} catch (error) {
|
|
84
|
-
jsonKeyValue = keyNode?.text ?? '';
|
|
85
|
-
parseError = error instanceof Error ? error : undefined;
|
|
86
|
-
}
|
|
87
|
-
const key = new _apidomAst.JsonKey({
|
|
88
|
-
value: jsonKeyValue,
|
|
89
|
-
parseError,
|
|
90
|
-
position: keyNode != null ? CstVisitor.toPosition(keyNode) : undefined,
|
|
91
|
-
isMissing: keyNode != null ? keyNode.isMissing : false
|
|
92
|
-
});
|
|
93
|
-
return new _apidomAst.JsonProperty({
|
|
94
|
-
children: [key, ...children],
|
|
95
|
-
position,
|
|
96
|
-
isMissing: node.isMissing
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
array(node) {
|
|
100
|
-
const position = CstVisitor.toPosition(node);
|
|
101
|
-
return new _apidomAst.JsonArray({
|
|
102
|
-
children: node.children,
|
|
103
|
-
position,
|
|
104
|
-
isMissing: node.isMissing
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
string(node) {
|
|
108
|
-
const position = CstVisitor.toPosition(node);
|
|
109
|
-
let value;
|
|
110
|
-
let parseError;
|
|
111
|
-
try {
|
|
112
|
-
value = JSON.parse(node.text);
|
|
113
|
-
} catch (error) {
|
|
114
|
-
parseError = error instanceof Error ? error : undefined;
|
|
115
|
-
value = node.text;
|
|
116
|
-
}
|
|
117
|
-
return new _apidomAst.JsonString({
|
|
118
|
-
value,
|
|
119
|
-
parseError,
|
|
120
|
-
position,
|
|
121
|
-
isMissing: node.isMissing
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
number(node) {
|
|
125
|
-
const position = CstVisitor.toPosition(node);
|
|
126
|
-
const value = node.text;
|
|
127
|
-
return new _apidomAst.JsonNumber({
|
|
128
|
-
value,
|
|
129
|
-
position,
|
|
130
|
-
isMissing: node.isMissing
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
null(node) {
|
|
134
|
-
const position = CstVisitor.toPosition(node);
|
|
135
|
-
const value = node.text;
|
|
136
|
-
return new _apidomAst.JsonNull({
|
|
137
|
-
value,
|
|
138
|
-
position,
|
|
139
|
-
isMissing: node.isMissing
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
true(node) {
|
|
143
|
-
const position = CstVisitor.toPosition(node);
|
|
144
|
-
const value = node.text;
|
|
145
|
-
return new _apidomAst.JsonTrue({
|
|
146
|
-
value,
|
|
147
|
-
position,
|
|
148
|
-
isMissing: node.isMissing
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
false(node) {
|
|
152
|
-
const position = CstVisitor.toPosition(node);
|
|
153
|
-
const value = node.text;
|
|
154
|
-
return new _apidomAst.JsonFalse({
|
|
155
|
-
value,
|
|
156
|
-
position,
|
|
157
|
-
isMissing: node.isMissing
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
ERROR(node, key, parent, path) {
|
|
161
|
-
const position = CstVisitor.toPosition(node);
|
|
162
|
-
const errorNode = new _apidomAst.Error({
|
|
163
|
-
children: node.children,
|
|
164
|
-
position,
|
|
165
|
-
isUnexpected: !node.hasError,
|
|
166
|
-
isMissing: node.isMissing,
|
|
167
|
-
value: node.text
|
|
168
|
-
});
|
|
169
|
-
if (path.length === 0) {
|
|
170
|
-
return new _apidomAst.ParseResult({
|
|
171
|
-
children: [errorNode]
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
return errorNode;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
var _default = exports.default = CstVisitor;
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { JsonArray, JsonDocument, JsonFalse, JsonNull, JsonNumber, JsonObject, JsonKey, JsonProperty, JsonString, JsonTrue, ParseResult, Position, Point, Literal, Error as ErrorNode } from '@speclynx/apidom-ast';
|
|
2
|
-
import TreeCursorSyntaxNode from "../../TreeCursorSyntaxNode.mjs";
|
|
3
|
-
export const keyMap = {
|
|
4
|
-
document: ['children'],
|
|
5
|
-
object: ['children'],
|
|
6
|
-
array: ['children'],
|
|
7
|
-
string: ['children'],
|
|
8
|
-
property: ['children'],
|
|
9
|
-
key: ['children'],
|
|
10
|
-
error: ['children']
|
|
11
|
-
};
|
|
12
|
-
class CstVisitor {
|
|
13
|
-
static toPosition(node) {
|
|
14
|
-
const start = new Point({
|
|
15
|
-
row: node.startPosition.row,
|
|
16
|
-
column: node.startPosition.column,
|
|
17
|
-
char: node.startIndex
|
|
18
|
-
});
|
|
19
|
-
const end = new Point({
|
|
20
|
-
row: node.endPosition.row,
|
|
21
|
-
column: node.endPosition.column,
|
|
22
|
-
char: node.endIndex
|
|
23
|
-
});
|
|
24
|
-
return new Position({
|
|
25
|
-
start,
|
|
26
|
-
end
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
document = {
|
|
30
|
-
enter: node => {
|
|
31
|
-
const position = CstVisitor.toPosition(node);
|
|
32
|
-
return new JsonDocument({
|
|
33
|
-
children: node.children,
|
|
34
|
-
position,
|
|
35
|
-
isMissing: node.isMissing
|
|
36
|
-
});
|
|
37
|
-
},
|
|
38
|
-
leave: document => {
|
|
39
|
-
return new ParseResult({
|
|
40
|
-
children: [document]
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
enter(node) {
|
|
45
|
-
// anonymous literals from CST transformed into AST literal nodes
|
|
46
|
-
if (node instanceof TreeCursorSyntaxNode && !node.isNamed) {
|
|
47
|
-
const position = CstVisitor.toPosition(node);
|
|
48
|
-
const value = node.type || node.text;
|
|
49
|
-
const {
|
|
50
|
-
isMissing
|
|
51
|
-
} = node;
|
|
52
|
-
return new Literal({
|
|
53
|
-
value,
|
|
54
|
-
position,
|
|
55
|
-
isMissing
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
return undefined;
|
|
59
|
-
}
|
|
60
|
-
object(node) {
|
|
61
|
-
const position = CstVisitor.toPosition(node);
|
|
62
|
-
return new JsonObject({
|
|
63
|
-
children: node.children,
|
|
64
|
-
position,
|
|
65
|
-
isMissing: node.isMissing
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
pair(node) {
|
|
69
|
-
const position = CstVisitor.toPosition(node);
|
|
70
|
-
const children = node.children.slice(1);
|
|
71
|
-
const {
|
|
72
|
-
keyNode
|
|
73
|
-
} = node;
|
|
74
|
-
let jsonKeyValue;
|
|
75
|
-
let parseError;
|
|
76
|
-
try {
|
|
77
|
-
jsonKeyValue = JSON.parse(keyNode?.text ?? '""');
|
|
78
|
-
} catch (error) {
|
|
79
|
-
jsonKeyValue = keyNode?.text ?? '';
|
|
80
|
-
parseError = error instanceof Error ? error : undefined;
|
|
81
|
-
}
|
|
82
|
-
const key = new JsonKey({
|
|
83
|
-
value: jsonKeyValue,
|
|
84
|
-
parseError,
|
|
85
|
-
position: keyNode != null ? CstVisitor.toPosition(keyNode) : undefined,
|
|
86
|
-
isMissing: keyNode != null ? keyNode.isMissing : false
|
|
87
|
-
});
|
|
88
|
-
return new JsonProperty({
|
|
89
|
-
children: [key, ...children],
|
|
90
|
-
position,
|
|
91
|
-
isMissing: node.isMissing
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
array(node) {
|
|
95
|
-
const position = CstVisitor.toPosition(node);
|
|
96
|
-
return new JsonArray({
|
|
97
|
-
children: node.children,
|
|
98
|
-
position,
|
|
99
|
-
isMissing: node.isMissing
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
string(node) {
|
|
103
|
-
const position = CstVisitor.toPosition(node);
|
|
104
|
-
let value;
|
|
105
|
-
let parseError;
|
|
106
|
-
try {
|
|
107
|
-
value = JSON.parse(node.text);
|
|
108
|
-
} catch (error) {
|
|
109
|
-
parseError = error instanceof Error ? error : undefined;
|
|
110
|
-
value = node.text;
|
|
111
|
-
}
|
|
112
|
-
return new JsonString({
|
|
113
|
-
value,
|
|
114
|
-
parseError,
|
|
115
|
-
position,
|
|
116
|
-
isMissing: node.isMissing
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
number(node) {
|
|
120
|
-
const position = CstVisitor.toPosition(node);
|
|
121
|
-
const value = node.text;
|
|
122
|
-
return new JsonNumber({
|
|
123
|
-
value,
|
|
124
|
-
position,
|
|
125
|
-
isMissing: node.isMissing
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
null(node) {
|
|
129
|
-
const position = CstVisitor.toPosition(node);
|
|
130
|
-
const value = node.text;
|
|
131
|
-
return new JsonNull({
|
|
132
|
-
value,
|
|
133
|
-
position,
|
|
134
|
-
isMissing: node.isMissing
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
true(node) {
|
|
138
|
-
const position = CstVisitor.toPosition(node);
|
|
139
|
-
const value = node.text;
|
|
140
|
-
return new JsonTrue({
|
|
141
|
-
value,
|
|
142
|
-
position,
|
|
143
|
-
isMissing: node.isMissing
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
false(node) {
|
|
147
|
-
const position = CstVisitor.toPosition(node);
|
|
148
|
-
const value = node.text;
|
|
149
|
-
return new JsonFalse({
|
|
150
|
-
value,
|
|
151
|
-
position,
|
|
152
|
-
isMissing: node.isMissing
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
ERROR(node, key, parent, path) {
|
|
156
|
-
const position = CstVisitor.toPosition(node);
|
|
157
|
-
const errorNode = new ErrorNode({
|
|
158
|
-
children: node.children,
|
|
159
|
-
position,
|
|
160
|
-
isUnexpected: !node.hasError,
|
|
161
|
-
isMissing: node.isMissing,
|
|
162
|
-
value: node.text
|
|
163
|
-
});
|
|
164
|
-
if (path.length === 0) {
|
|
165
|
-
return new ParseResult({
|
|
166
|
-
children: [errorNode]
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
return errorNode;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
export default CstVisitor;
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.keyMap = exports.isNode = exports.getNodeType = exports.default = void 0;
|
|
5
|
-
var _apidomAst = require("@speclynx/apidom-ast");
|
|
6
|
-
var _apidomCore = require("@speclynx/apidom-core");
|
|
7
|
-
const keyMap = exports.keyMap = {
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
[_apidomAst.ParseResult.type]: ['children'],
|
|
10
|
-
// @ts-ignore
|
|
11
|
-
[_apidomAst.JsonDocument.type]: ['children'],
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
[_apidomAst.JsonObject.type]: ['children'],
|
|
14
|
-
// @ts-ignore
|
|
15
|
-
[_apidomAst.JsonProperty.type]: ['children'],
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
[_apidomAst.JsonArray.type]: ['children'],
|
|
18
|
-
// @ts-ignore
|
|
19
|
-
[_apidomAst.Error.type]: ['children'],
|
|
20
|
-
..._apidomCore.keyMap
|
|
21
|
-
};
|
|
22
|
-
const getNodeType = node => {
|
|
23
|
-
if ((0, _apidomCore.isParseResultElement)(node)) {
|
|
24
|
-
return 'ParseResultElement';
|
|
25
|
-
}
|
|
26
|
-
if ((0, _apidomCore.isElement)(node)) {
|
|
27
|
-
return (0, _apidomCore.getNodeType)(node);
|
|
28
|
-
}
|
|
29
|
-
return (0, _apidomAst.getNodeType)(node);
|
|
30
|
-
};
|
|
31
|
-
exports.getNodeType = getNodeType;
|
|
32
|
-
const isNode = element => (0, _apidomCore.isElement)(element) || (0, _apidomAst.isNode)(element);
|
|
33
|
-
exports.isNode = isNode;
|
|
34
|
-
class JsonAstVisitor {
|
|
35
|
-
sourceMap = false;
|
|
36
|
-
annotations;
|
|
37
|
-
ParseResultElement = {
|
|
38
|
-
leave: element => {
|
|
39
|
-
// mark first-non Annotation element as result
|
|
40
|
-
// @ts-ignore
|
|
41
|
-
const elements = element.findElements(_apidomCore.isPrimitiveElement);
|
|
42
|
-
if (elements.length > 0) {
|
|
43
|
-
const resultElement = elements[0];
|
|
44
|
-
resultElement.classes.push('result');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// provide annotations
|
|
48
|
-
this.annotations.forEach(annotationElement => {
|
|
49
|
-
element.push(annotationElement);
|
|
50
|
-
});
|
|
51
|
-
this.annotations = [];
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
constructor() {
|
|
55
|
-
this.annotations = [];
|
|
56
|
-
}
|
|
57
|
-
document(node) {
|
|
58
|
-
const element = new _apidomCore.ParseResultElement();
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
element._content = node.children;
|
|
61
|
-
return element;
|
|
62
|
-
}
|
|
63
|
-
object(node) {
|
|
64
|
-
const element = new _apidomCore.ObjectElement();
|
|
65
|
-
// @ts-ignore
|
|
66
|
-
element._content = node.children;
|
|
67
|
-
this.maybeAddSourceMap(node, element);
|
|
68
|
-
return element;
|
|
69
|
-
}
|
|
70
|
-
property(node) {
|
|
71
|
-
const element = new _apidomCore.MemberElement();
|
|
72
|
-
|
|
73
|
-
// @ts-ignore
|
|
74
|
-
element.content.key = node.key;
|
|
75
|
-
// @ts-ignore
|
|
76
|
-
element.content.value = node.value;
|
|
77
|
-
this.maybeAddSourceMap(node, element);
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Process possible errors here that may be present in pair node children as we're using direct field access.
|
|
81
|
-
* There are usually 3 children here found: "key", ":", "value".
|
|
82
|
-
*/
|
|
83
|
-
if (node.children.length > 3) {
|
|
84
|
-
node.children
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
|
-
.filter(child => child.type === 'error')
|
|
87
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
88
|
-
.forEach(errorNode => {
|
|
89
|
-
this.error(errorNode, node, [], [node]);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
return element;
|
|
93
|
-
}
|
|
94
|
-
key(node) {
|
|
95
|
-
const {
|
|
96
|
-
value,
|
|
97
|
-
parseError
|
|
98
|
-
} = node;
|
|
99
|
-
const element = new _apidomCore.StringElement(value);
|
|
100
|
-
if (parseError instanceof _apidomAst.Error) {
|
|
101
|
-
element.setMetaProperty('jsonParse', {
|
|
102
|
-
isError: true,
|
|
103
|
-
errorType: parseError.name,
|
|
104
|
-
errorMessage: parseError.message
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
this.maybeAddSourceMap(node, element);
|
|
108
|
-
return element;
|
|
109
|
-
}
|
|
110
|
-
array(node) {
|
|
111
|
-
const element = new _apidomCore.ArrayElement();
|
|
112
|
-
// @ts-ignore
|
|
113
|
-
element._content = node.children;
|
|
114
|
-
this.maybeAddSourceMap(node, element);
|
|
115
|
-
return element;
|
|
116
|
-
}
|
|
117
|
-
string(node) {
|
|
118
|
-
const {
|
|
119
|
-
value,
|
|
120
|
-
parseError
|
|
121
|
-
} = node;
|
|
122
|
-
const element = new _apidomCore.StringElement(value);
|
|
123
|
-
if (parseError instanceof _apidomAst.Error) {
|
|
124
|
-
element.setMetaProperty('jsonParse', {
|
|
125
|
-
isError: true,
|
|
126
|
-
errorType: parseError.name,
|
|
127
|
-
errorMessage: parseError.message
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
this.maybeAddSourceMap(node, element);
|
|
131
|
-
return element;
|
|
132
|
-
}
|
|
133
|
-
number(node) {
|
|
134
|
-
const element = new _apidomCore.NumberElement(Number(node.value));
|
|
135
|
-
this.maybeAddSourceMap(node, element);
|
|
136
|
-
return element;
|
|
137
|
-
}
|
|
138
|
-
null(node) {
|
|
139
|
-
const element = new _apidomCore.NullElement();
|
|
140
|
-
this.maybeAddSourceMap(node, element);
|
|
141
|
-
return element;
|
|
142
|
-
}
|
|
143
|
-
true(node) {
|
|
144
|
-
const element = new _apidomCore.BooleanElement(true);
|
|
145
|
-
this.maybeAddSourceMap(node, element);
|
|
146
|
-
return element;
|
|
147
|
-
}
|
|
148
|
-
false(node) {
|
|
149
|
-
const element = new _apidomCore.BooleanElement(false);
|
|
150
|
-
this.maybeAddSourceMap(node, element);
|
|
151
|
-
return element;
|
|
152
|
-
}
|
|
153
|
-
literal(node) {
|
|
154
|
-
if (node.isMissing) {
|
|
155
|
-
const message = `(Missing ${node.value})`;
|
|
156
|
-
const element = new _apidomCore.AnnotationElement(message);
|
|
157
|
-
element.classes.push('warning');
|
|
158
|
-
this.maybeAddSourceMap(node, element);
|
|
159
|
-
this.annotations.push(element);
|
|
160
|
-
}
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
error(node, key, parent, path) {
|
|
164
|
-
const message = node.isUnexpected ? `(Unexpected ${node.value})` : `(Error ${node.value})`;
|
|
165
|
-
const element = new _apidomCore.AnnotationElement(message);
|
|
166
|
-
element.classes.push('error');
|
|
167
|
-
this.maybeAddSourceMap(node, element);
|
|
168
|
-
if (path.length === 0) {
|
|
169
|
-
// no document to visit, only error is present in CST
|
|
170
|
-
const parseResultElement = new _apidomCore.ParseResultElement();
|
|
171
|
-
parseResultElement.push(element);
|
|
172
|
-
return parseResultElement;
|
|
173
|
-
}
|
|
174
|
-
this.annotations.push(element);
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
maybeAddSourceMap(node, element) {
|
|
178
|
-
if (!this.sourceMap) {
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
const sourceMap = new _apidomCore.SourceMapElement();
|
|
182
|
-
// @ts-ignore
|
|
183
|
-
sourceMap.position = node.position;
|
|
184
|
-
// @ts-ignore
|
|
185
|
-
sourceMap.astNode = node;
|
|
186
|
-
element.meta.set('sourceMap', sourceMap);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
var _default = exports.default = JsonAstVisitor;
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import { JsonArray, JsonDocument, JsonObject, JsonProperty, ParseResult, Error, isNode as isCSTNode, getNodeType as getCSTNodeType } from '@speclynx/apidom-ast';
|
|
2
|
-
import { ParseResultElement, ObjectElement, SourceMapElement, MemberElement, ArrayElement, BooleanElement, NullElement, NumberElement, StringElement, AnnotationElement, isParseResultElement, isPrimitiveElement, isElement, keyMap as keyMapApiDOM, getNodeType as getNodeTypeApiDOM } from '@speclynx/apidom-core';
|
|
3
|
-
export const keyMap = {
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
[ParseResult.type]: ['children'],
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
[JsonDocument.type]: ['children'],
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
[JsonObject.type]: ['children'],
|
|
10
|
-
// @ts-ignore
|
|
11
|
-
[JsonProperty.type]: ['children'],
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
[JsonArray.type]: ['children'],
|
|
14
|
-
// @ts-ignore
|
|
15
|
-
[Error.type]: ['children'],
|
|
16
|
-
...keyMapApiDOM
|
|
17
|
-
};
|
|
18
|
-
export const getNodeType = node => {
|
|
19
|
-
if (isParseResultElement(node)) {
|
|
20
|
-
return 'ParseResultElement';
|
|
21
|
-
}
|
|
22
|
-
if (isElement(node)) {
|
|
23
|
-
return getNodeTypeApiDOM(node);
|
|
24
|
-
}
|
|
25
|
-
return getCSTNodeType(node);
|
|
26
|
-
};
|
|
27
|
-
export const isNode = element => isElement(element) || isCSTNode(element);
|
|
28
|
-
class JsonAstVisitor {
|
|
29
|
-
sourceMap = false;
|
|
30
|
-
annotations;
|
|
31
|
-
ParseResultElement = {
|
|
32
|
-
leave: element => {
|
|
33
|
-
// mark first-non Annotation element as result
|
|
34
|
-
// @ts-ignore
|
|
35
|
-
const elements = element.findElements(isPrimitiveElement);
|
|
36
|
-
if (elements.length > 0) {
|
|
37
|
-
const resultElement = elements[0];
|
|
38
|
-
resultElement.classes.push('result');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// provide annotations
|
|
42
|
-
this.annotations.forEach(annotationElement => {
|
|
43
|
-
element.push(annotationElement);
|
|
44
|
-
});
|
|
45
|
-
this.annotations = [];
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
constructor() {
|
|
49
|
-
this.annotations = [];
|
|
50
|
-
}
|
|
51
|
-
document(node) {
|
|
52
|
-
const element = new ParseResultElement();
|
|
53
|
-
// @ts-ignore
|
|
54
|
-
element._content = node.children;
|
|
55
|
-
return element;
|
|
56
|
-
}
|
|
57
|
-
object(node) {
|
|
58
|
-
const element = new ObjectElement();
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
element._content = node.children;
|
|
61
|
-
this.maybeAddSourceMap(node, element);
|
|
62
|
-
return element;
|
|
63
|
-
}
|
|
64
|
-
property(node) {
|
|
65
|
-
const element = new MemberElement();
|
|
66
|
-
|
|
67
|
-
// @ts-ignore
|
|
68
|
-
element.content.key = node.key;
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
element.content.value = node.value;
|
|
71
|
-
this.maybeAddSourceMap(node, element);
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Process possible errors here that may be present in pair node children as we're using direct field access.
|
|
75
|
-
* There are usually 3 children here found: "key", ":", "value".
|
|
76
|
-
*/
|
|
77
|
-
if (node.children.length > 3) {
|
|
78
|
-
node.children
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
-
.filter(child => child.type === 'error')
|
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
82
|
-
.forEach(errorNode => {
|
|
83
|
-
this.error(errorNode, node, [], [node]);
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
return element;
|
|
87
|
-
}
|
|
88
|
-
key(node) {
|
|
89
|
-
const {
|
|
90
|
-
value,
|
|
91
|
-
parseError
|
|
92
|
-
} = node;
|
|
93
|
-
const element = new StringElement(value);
|
|
94
|
-
if (parseError instanceof Error) {
|
|
95
|
-
element.setMetaProperty('jsonParse', {
|
|
96
|
-
isError: true,
|
|
97
|
-
errorType: parseError.name,
|
|
98
|
-
errorMessage: parseError.message
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
this.maybeAddSourceMap(node, element);
|
|
102
|
-
return element;
|
|
103
|
-
}
|
|
104
|
-
array(node) {
|
|
105
|
-
const element = new ArrayElement();
|
|
106
|
-
// @ts-ignore
|
|
107
|
-
element._content = node.children;
|
|
108
|
-
this.maybeAddSourceMap(node, element);
|
|
109
|
-
return element;
|
|
110
|
-
}
|
|
111
|
-
string(node) {
|
|
112
|
-
const {
|
|
113
|
-
value,
|
|
114
|
-
parseError
|
|
115
|
-
} = node;
|
|
116
|
-
const element = new StringElement(value);
|
|
117
|
-
if (parseError instanceof Error) {
|
|
118
|
-
element.setMetaProperty('jsonParse', {
|
|
119
|
-
isError: true,
|
|
120
|
-
errorType: parseError.name,
|
|
121
|
-
errorMessage: parseError.message
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
this.maybeAddSourceMap(node, element);
|
|
125
|
-
return element;
|
|
126
|
-
}
|
|
127
|
-
number(node) {
|
|
128
|
-
const element = new NumberElement(Number(node.value));
|
|
129
|
-
this.maybeAddSourceMap(node, element);
|
|
130
|
-
return element;
|
|
131
|
-
}
|
|
132
|
-
null(node) {
|
|
133
|
-
const element = new NullElement();
|
|
134
|
-
this.maybeAddSourceMap(node, element);
|
|
135
|
-
return element;
|
|
136
|
-
}
|
|
137
|
-
true(node) {
|
|
138
|
-
const element = new BooleanElement(true);
|
|
139
|
-
this.maybeAddSourceMap(node, element);
|
|
140
|
-
return element;
|
|
141
|
-
}
|
|
142
|
-
false(node) {
|
|
143
|
-
const element = new BooleanElement(false);
|
|
144
|
-
this.maybeAddSourceMap(node, element);
|
|
145
|
-
return element;
|
|
146
|
-
}
|
|
147
|
-
literal(node) {
|
|
148
|
-
if (node.isMissing) {
|
|
149
|
-
const message = `(Missing ${node.value})`;
|
|
150
|
-
const element = new AnnotationElement(message);
|
|
151
|
-
element.classes.push('warning');
|
|
152
|
-
this.maybeAddSourceMap(node, element);
|
|
153
|
-
this.annotations.push(element);
|
|
154
|
-
}
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
error(node, key, parent, path) {
|
|
158
|
-
const message = node.isUnexpected ? `(Unexpected ${node.value})` : `(Error ${node.value})`;
|
|
159
|
-
const element = new AnnotationElement(message);
|
|
160
|
-
element.classes.push('error');
|
|
161
|
-
this.maybeAddSourceMap(node, element);
|
|
162
|
-
if (path.length === 0) {
|
|
163
|
-
// no document to visit, only error is present in CST
|
|
164
|
-
const parseResultElement = new ParseResultElement();
|
|
165
|
-
parseResultElement.push(element);
|
|
166
|
-
return parseResultElement;
|
|
167
|
-
}
|
|
168
|
-
this.annotations.push(element);
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
maybeAddSourceMap(node, element) {
|
|
172
|
-
if (!this.sourceMap) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
const sourceMap = new SourceMapElement();
|
|
176
|
-
// @ts-ignore
|
|
177
|
-
sourceMap.position = node.position;
|
|
178
|
-
// @ts-ignore
|
|
179
|
-
sourceMap.astNode = node;
|
|
180
|
-
element.meta.set('sourceMap', sourceMap);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
export default JsonAstVisitor;
|