@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,180 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
|
|
2
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
3
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
|
+
import stampit from 'stampit';
|
|
5
|
+
import { JsonArray, JsonDocument, JsonObject, JsonProperty, ParseResult, Error, isNode as isCSTNode, getNodeType as getCSTNodeType } from '@swagger-api/apidom-ast';
|
|
6
|
+
import { ParseResultElement, ObjectElement, SourceMapElement, MemberElement, ArrayElement, BooleanElement, NullElement, NumberElement, StringElement, AnnotationElement, isParseResultElement, isPrimitiveElement, isElement, keyMap as keyMapApiDOM, getNodeType as getNodeTypeApiDOM } from '@swagger-api/apidom-core';
|
|
7
|
+
export const keyMap = _objectSpread({
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
[ParseResult.type]: ['children'],
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
[JsonDocument.type]: ['children'],
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
[JsonObject.type]: ['children'],
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
[JsonProperty.type]: ['children'],
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
[JsonArray.type]: ['children'],
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
[Error.type]: ['children']
|
|
20
|
+
}, keyMapApiDOM);
|
|
21
|
+
export const getNodeType = node => {
|
|
22
|
+
if (isParseResultElement(node)) {
|
|
23
|
+
return 'ParseResultElement';
|
|
24
|
+
}
|
|
25
|
+
if (isElement(node)) {
|
|
26
|
+
return getNodeTypeApiDOM(node);
|
|
27
|
+
}
|
|
28
|
+
return getCSTNodeType(node);
|
|
29
|
+
};
|
|
30
|
+
export const isNode = element => isElement(element) || isCSTNode(element);
|
|
31
|
+
|
|
32
|
+
/* eslint-disable no-underscore-dangle */
|
|
33
|
+
|
|
34
|
+
const JsonAstVisitor = stampit({
|
|
35
|
+
props: {
|
|
36
|
+
sourceMap: false,
|
|
37
|
+
annotations: []
|
|
38
|
+
},
|
|
39
|
+
init() {
|
|
40
|
+
/**
|
|
41
|
+
* Private API.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
this.annotation = [];
|
|
45
|
+
const maybeAddSourceMap = (node, element) => {
|
|
46
|
+
if (!this.sourceMap) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const sourceMap = new SourceMapElement();
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
sourceMap.position = node.position;
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
sourceMap.astNode = node;
|
|
54
|
+
element.meta.set('sourceMap', sourceMap);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Public API.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
this.document = function document(node) {
|
|
62
|
+
const element = new ParseResultElement();
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
element._content = node.children;
|
|
65
|
+
return element;
|
|
66
|
+
};
|
|
67
|
+
this.ParseResultElement = {
|
|
68
|
+
leave(element) {
|
|
69
|
+
// mark first-non Annotation element as result
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
const elements = element.findElements(isPrimitiveElement);
|
|
72
|
+
if (elements.length > 0) {
|
|
73
|
+
const resultElement = elements[0];
|
|
74
|
+
resultElement.classes.push('result');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// provide annotations
|
|
78
|
+
this.annotations.forEach(annotationElement => {
|
|
79
|
+
element.push(annotationElement);
|
|
80
|
+
});
|
|
81
|
+
this.annotations = [];
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
this.object = function object(node) {
|
|
85
|
+
const element = new ObjectElement();
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
element._content = node.children;
|
|
88
|
+
maybeAddSourceMap(node, element);
|
|
89
|
+
return element;
|
|
90
|
+
};
|
|
91
|
+
this.property = function property(node) {
|
|
92
|
+
const element = new MemberElement();
|
|
93
|
+
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
element.content.key = node.key;
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
element.content.value = node.value;
|
|
98
|
+
maybeAddSourceMap(node, element);
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Process possible errors here that may be present in pair node children as we're using direct field access.
|
|
102
|
+
* There are usually 3 children here found: "key", ":", "value".
|
|
103
|
+
*/
|
|
104
|
+
if (node.children.length > 3) {
|
|
105
|
+
node.children.filter(child => child.type === 'error').forEach(errorNode => {
|
|
106
|
+
this.error(errorNode, node, [], [node]);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return element;
|
|
110
|
+
};
|
|
111
|
+
this.key = function key(node) {
|
|
112
|
+
const element = new StringElement(node.value);
|
|
113
|
+
maybeAddSourceMap(node, element);
|
|
114
|
+
return element;
|
|
115
|
+
};
|
|
116
|
+
this.array = function array(node) {
|
|
117
|
+
const element = new ArrayElement();
|
|
118
|
+
// @ts-ignore
|
|
119
|
+
element._content = node.children;
|
|
120
|
+
maybeAddSourceMap(node, element);
|
|
121
|
+
return element;
|
|
122
|
+
};
|
|
123
|
+
this.string = function string(node) {
|
|
124
|
+
const element = new StringElement(node.value);
|
|
125
|
+
maybeAddSourceMap(node, element);
|
|
126
|
+
return element;
|
|
127
|
+
};
|
|
128
|
+
this.number = function number(node) {
|
|
129
|
+
const element = new NumberElement(Number(node.value));
|
|
130
|
+
maybeAddSourceMap(node, element);
|
|
131
|
+
return element;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
135
|
+
this.null = function _null(node) {
|
|
136
|
+
const element = new NullElement();
|
|
137
|
+
maybeAddSourceMap(node, element);
|
|
138
|
+
return element;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
142
|
+
this.true = function _true(node) {
|
|
143
|
+
const element = new BooleanElement(true);
|
|
144
|
+
maybeAddSourceMap(node, element);
|
|
145
|
+
return element;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
149
|
+
this.false = function _false(node) {
|
|
150
|
+
const element = new BooleanElement(false);
|
|
151
|
+
maybeAddSourceMap(node, element);
|
|
152
|
+
return element;
|
|
153
|
+
};
|
|
154
|
+
this.literal = function literal(node) {
|
|
155
|
+
if (node.isMissing) {
|
|
156
|
+
const message = `(Missing ${node.value})`;
|
|
157
|
+
const element = new AnnotationElement(message);
|
|
158
|
+
element.classes.push('warning');
|
|
159
|
+
maybeAddSourceMap(node, element);
|
|
160
|
+
this.annotations.push(element);
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
};
|
|
164
|
+
this.error = function error(node, key, parent, path) {
|
|
165
|
+
const message = node.isUnexpected ? `(Unexpected ${node.value})` : `(Error ${node.value})`;
|
|
166
|
+
const element = new AnnotationElement(message);
|
|
167
|
+
element.classes.push('error');
|
|
168
|
+
maybeAddSourceMap(node, element);
|
|
169
|
+
if (path.length === 0) {
|
|
170
|
+
// no document to visit, only error is present in CST
|
|
171
|
+
const parseResultElement = new ParseResultElement();
|
|
172
|
+
parseResultElement.push(element);
|
|
173
|
+
return parseResultElement;
|
|
174
|
+
}
|
|
175
|
+
this.annotations.push(element);
|
|
176
|
+
return null;
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
export default JsonAstVisitor;
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@swagger-api/apidom-parser-adapter-json",
|
|
3
|
+
"version": "0.68.1",
|
|
4
|
+
"description": "Parser adapter for parsing JSON documents into base namespace.",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public",
|
|
7
|
+
"registry": "https://registry.npmjs.org"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"sideEffects": [
|
|
11
|
+
"./es/lexical-analysis/browser-patch.js",
|
|
12
|
+
"./cjs/lexical-analysis/browser-patch.cjs"
|
|
13
|
+
],
|
|
14
|
+
"unpkg": "./dist/apidom-parser-apdater-json.browser.min.js",
|
|
15
|
+
"main": "./cjs/adapter-node.cjs",
|
|
16
|
+
"exports": {
|
|
17
|
+
"types": "./types/dist.d.ts",
|
|
18
|
+
"node": {
|
|
19
|
+
"import": "./es/adapter-node.js",
|
|
20
|
+
"require": "./cjs/adapter-node.cjs"
|
|
21
|
+
},
|
|
22
|
+
"browser": {
|
|
23
|
+
"import": "./es/adapter-browser.js"
|
|
24
|
+
},
|
|
25
|
+
"default": "./cjs/adapter-node.cjs"
|
|
26
|
+
},
|
|
27
|
+
"types": "./types/dist.d.ts",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "npm run clean && run-p --max-parallel ${CPU_CORES:-2} typescript:declaration build:es build:cjs build:umd:browser",
|
|
30
|
+
"build:es": "npm run build:wasm && npm run build:wasm:copy && cross-env BABEL_ENV=es babel src --out-dir es --extensions '.ts' --root-mode 'upward'",
|
|
31
|
+
"build:cjs": "npm run build:wasm && npm run build:wasm:copy && BABEL_ENV=cjs babel src --out-dir cjs --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'",
|
|
32
|
+
"build:umd:browser": "npm run build:wasm && npm run build:wasm:copy && cross-env BABEL_ENV=browser BROWSERSLIST_ENV=production webpack --config config/webpack/browser.config.js --progress",
|
|
33
|
+
"build:wasm": "node ../../scripts/file-exists.js ../../node_modules/tree-sitter-json/tree-sitter-json.wasm && exit 0 || cd ../../node_modules/tree-sitter-json && tree-sitter generate --abi=13 ./grammar.js && tree-sitter build-wasm && node-gyp rebuild",
|
|
34
|
+
"build:wasm:copy": "copyfiles -u 4 ../../node_modules/tree-sitter-json/tree-sitter-json.wasm wasm",
|
|
35
|
+
"lint": "eslint ./",
|
|
36
|
+
"lint:fix": "eslint ./ --fix",
|
|
37
|
+
"clean": "rimraf ./es ./cjs ./dist ./types",
|
|
38
|
+
"typescript:check-types": "tsc --noEmit",
|
|
39
|
+
"typescript:declaration": "tsc -p declaration.tsconfig.json && rollup -c config/rollup/types.dist.js",
|
|
40
|
+
"test": "cross-env NODE_ENV=test BABEL_ENV=cjs mocha",
|
|
41
|
+
"perf": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/index.cjs",
|
|
42
|
+
"perf:lexical-analysis": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/lexical-analysis.cjs",
|
|
43
|
+
"perf:parse-syntactic-analysis-direct": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/parse-syntactic-analysis-direct.cjs",
|
|
44
|
+
"perf:parse-syntactic-analysis-indirect": "cross-env NODE_ENV=test BABEL_ENV=cjs node ./test/perf/parse-syntactic-analysis-indirect.cjs",
|
|
45
|
+
"prepack": "copyfiles -u 3 ../../LICENSES/* LICENSES && copyfiles -u 2 ../../NOTICE .",
|
|
46
|
+
"postpack": "rimraf NOTICE LICENSES"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/swagger-api/apidom.git"
|
|
51
|
+
},
|
|
52
|
+
"author": "Vladimir Gorej",
|
|
53
|
+
"license": "Apache-2.0",
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@babel/runtime-corejs3": "^7.20.7",
|
|
56
|
+
"@swagger-api/apidom-ast": "^0.68.0",
|
|
57
|
+
"@swagger-api/apidom-core": "^0.68.1",
|
|
58
|
+
"@types/ramda": "=0.28.23",
|
|
59
|
+
"ramda": "=0.28.0",
|
|
60
|
+
"ramda-adjunct": "=3.4.0",
|
|
61
|
+
"stampit": "=4.3.2",
|
|
62
|
+
"tree-sitter": "=0.20.1",
|
|
63
|
+
"tree-sitter-json": "=0.20.0",
|
|
64
|
+
"web-tree-sitter": "=0.20.7"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"tree-sitter-cli": "=0.20.7"
|
|
68
|
+
},
|
|
69
|
+
"files": [
|
|
70
|
+
"cjs/",
|
|
71
|
+
"dist/",
|
|
72
|
+
"es/",
|
|
73
|
+
"types/dist.d.ts",
|
|
74
|
+
"wasm/",
|
|
75
|
+
"LICENSES",
|
|
76
|
+
"NOTICE",
|
|
77
|
+
"README.md",
|
|
78
|
+
"CHANGELOG.md"
|
|
79
|
+
],
|
|
80
|
+
"gitHead": "d2bc706671f1b9a593b8f0c5bca8c501ad0e4cff"
|
|
81
|
+
}
|
package/types/dist.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as _swagger_api_apidom_core from '@swagger-api/apidom-core';
|
|
2
|
+
import { ParseResultElement, MediaTypes } from '@swagger-api/apidom-core';
|
|
3
|
+
import { Tree as Tree$1 } from 'web-tree-sitter';
|
|
4
|
+
import { Tree as Tree$2 } from 'tree-sitter';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Lexical Analysis of source string using WebTreeSitter.
|
|
8
|
+
* This is WebAssembly version of TreeSitters Lexical Analysis.
|
|
9
|
+
*
|
|
10
|
+
* Given JavaScript doesn't support true parallelism, this
|
|
11
|
+
* code should be as lazy as possible and temporal safety should be fine.
|
|
12
|
+
*/
|
|
13
|
+
declare const analyze$2: (source: string) => Promise<Tree$1>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* This version of syntactic analysis translates TreeSitter CTS into ApiDOM.
|
|
17
|
+
* Single traversal pass is needed to get from CST to ApiDOM.
|
|
18
|
+
*/
|
|
19
|
+
declare const analyze$1: (cst: {
|
|
20
|
+
rootNode: unknown;
|
|
21
|
+
}, { sourceMap }?: {
|
|
22
|
+
sourceMap?: boolean | undefined;
|
|
23
|
+
}) => ParseResultElement;
|
|
24
|
+
|
|
25
|
+
type Tree = Tree$1 | Tree$2;
|
|
26
|
+
/**
|
|
27
|
+
* This version of syntactic analysis does following transformations:
|
|
28
|
+
* TreeSitter CST -> JSON AST -> ApiDOM
|
|
29
|
+
* Two traversals passes are needed to get from CST to ApiDOM.
|
|
30
|
+
* This analysis is much slower than the direct one, but allows
|
|
31
|
+
* to do additional analysis magic on JSON AST.
|
|
32
|
+
*/
|
|
33
|
+
declare const analyze: (cst: Tree, { sourceMap }?: {
|
|
34
|
+
sourceMap?: boolean | undefined;
|
|
35
|
+
}) => ParseResultElement;
|
|
36
|
+
|
|
37
|
+
declare class JSONMediaTypes extends MediaTypes<string> {
|
|
38
|
+
latest(): string;
|
|
39
|
+
}
|
|
40
|
+
declare const mediaTypes: JSONMediaTypes;
|
|
41
|
+
|
|
42
|
+
declare const namespace: _swagger_api_apidom_core.Namespace;
|
|
43
|
+
declare const detectionRegExp: RegExp;
|
|
44
|
+
|
|
45
|
+
declare const detect: (source: string) => Promise<boolean>;
|
|
46
|
+
interface ParseFunctionOptions {
|
|
47
|
+
sourceMap?: boolean;
|
|
48
|
+
syntacticAnalysis?: 'direct' | 'indirect';
|
|
49
|
+
}
|
|
50
|
+
type ParseFunction = (source: string, options?: ParseFunctionOptions) => Promise<ParseResultElement>;
|
|
51
|
+
declare const parse: ParseFunction;
|
|
52
|
+
|
|
53
|
+
export { detect, detectionRegExp, analyze$2 as lexicalAnalysis, mediaTypes, namespace, parse, analyze$1 as syntacticAnalysis, analyze$1 as syntacticAnalysisDirect, analyze as syntacticAnalysisIndirect };
|
|
Binary file
|