source: node_modules/@swagger-api/apidom-ns-json-schema-draft-4/cjs/refractor/plugins/replace-empty-element.cjs

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 6.4 KB
Line 
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4exports.__esModule = true;
5exports.default = void 0;
6var _apidomCore = require("@swagger-api/apidom-core");
7var _JSONSchema = _interopRequireDefault(require("../../elements/JSONSchema.cjs"));
8var _LinkDescription = _interopRequireDefault(require("../../elements/LinkDescription.cjs"));
9var _Media = _interopRequireDefault(require("../../elements/Media.cjs"));
10var _visitor = require("../../traversal/visitor.cjs");
11/**
12 * JSON Schema Draft 4 specification elements.
13 */
14
15/**
16 * This plugin is specific to YAML 1.2 format, which allows defining key-value pairs
17 * with empty key, empty value, or both. If the value is not provided in YAML format,
18 * this plugin compensates for this missing value with the most appropriate semantic element type.
19 *
20 * https://yaml.org/spec/1.2.2/#72-empty-nodes
21 *
22 * @example
23 *
24 * ```yaml
25 * $schema: http://json-schema.org/draft-04/schema#
26 * items:
27 * ```
28 * Refracting result without this plugin:
29 *
30 * (JSONSchemaElement
31 * (MemberElement
32 * (StringElement)
33 * (StringElement))
34 * (MemberElement
35 * (StringElement)
36 * (StringElement))
37 *
38 * Refracting result with this plugin:
39 *
40 * (JSONSchemaElement
41 * (MemberElement
42 * (StringElement)
43 * (StringElement))
44 * (MemberElement
45 * (StringElement)
46 * (JSONSchemaElement))
47 */
48
49const isEmptyElement = element => (0, _apidomCore.isStringElement)(element) && (0, _apidomCore.includesClasses)(['yaml-e-node', 'yaml-e-scalar'], element);
50const schema = {
51 JSONSchemaDraft4Element: {
52 additionalItems(...args) {
53 return new _JSONSchema.default(...args);
54 },
55 items(...args) {
56 return new _JSONSchema.default(...args);
57 },
58 required(...args) {
59 const element = new _apidomCore.ArrayElement(...args);
60 element.classes.push('json-schema-required');
61 return element;
62 },
63 properties(...args) {
64 const element = new _apidomCore.ObjectElement(...args);
65 element.classes.push('json-schema-properties');
66 return element;
67 },
68 additionalProperties(...args) {
69 return new _JSONSchema.default(...args);
70 },
71 patternProperties(...args) {
72 const element = new _apidomCore.ObjectElement(...args);
73 element.classes.push('json-schema-patternProperties');
74 return element;
75 },
76 dependencies(...args) {
77 const element = new _apidomCore.ObjectElement(...args);
78 element.classes.push('json-schema-dependencies');
79 return element;
80 },
81 enum(...args) {
82 const element = new _apidomCore.ArrayElement(...args);
83 element.classes.push('json-schema-enum');
84 return element;
85 },
86 allOf(...args) {
87 const element = new _apidomCore.ArrayElement(...args);
88 element.classes.push('json-schema-allOf');
89 return element;
90 },
91 anyOf(...args) {
92 const element = new _apidomCore.ArrayElement(...args);
93 element.classes.push('json-schema-anyOf');
94 return element;
95 },
96 oneOf(...args) {
97 const element = new _apidomCore.ArrayElement(...args);
98 element.classes.push('json-schema-oneOf');
99 return element;
100 },
101 not(...args) {
102 return new _JSONSchema.default(...args);
103 },
104 definitions(...args) {
105 const element = new _apidomCore.ObjectElement(...args);
106 element.classes.push('json-schema-definitions');
107 return element;
108 },
109 links(...args) {
110 const element = new _apidomCore.ArrayElement(...args);
111 element.classes.push('json-schema-links');
112 return element;
113 },
114 media(...args) {
115 return new _Media.default(...args);
116 }
117 },
118 LinkDescriptionElement: {
119 targetSchema(...args) {
120 return new _JSONSchema.default(...args);
121 },
122 schema(...args) {
123 return new _JSONSchema.default(...args);
124 }
125 },
126 'json-schema-properties': {
127 '[key: *]': function key(...args) {
128 return new _JSONSchema.default(...args);
129 }
130 },
131 'json-schema-patternProperties': {
132 '[key: *]': function key(...args) {
133 return new _JSONSchema.default(...args);
134 }
135 },
136 'json-schema-dependencies': {
137 '[key: *]': function key(...args) {
138 return new _JSONSchema.default(...args);
139 }
140 },
141 'json-schema-allOf': {
142 '<*>': function asterisk(...args) {
143 return new _JSONSchema.default(...args);
144 }
145 },
146 'json-schema-anyOf': {
147 '<*>': function asterisk(...args) {
148 return new _JSONSchema.default(...args);
149 }
150 },
151 'json-schema-oneOf': {
152 '<*>': function asterisk(...args) {
153 return new _JSONSchema.default(...args);
154 }
155 },
156 'json-schema-definitions': {
157 '[key: *]': function key(...args) {
158 return new _JSONSchema.default(...args);
159 }
160 },
161 'json-schema-links': {
162 '<*>': function asterisk(...args) {
163 return new _LinkDescription.default(...args);
164 }
165 }
166};
167const findElementFactory = (ancestor, keyName) => {
168 const elementType = (0, _visitor.getNodeType)(ancestor); // @ts-ignore
169 const keyMapping = schema[elementType] || schema[(0, _apidomCore.toValue)(ancestor.classes.first)];
170 return typeof keyMapping === 'undefined' ? undefined : Object.prototype.hasOwnProperty.call(keyMapping, '[key: *]') ? keyMapping['[key: *]'] : keyMapping[keyName];
171};
172const plugin = () => () => ({
173 visitor: {
174 StringElement(element, key, parent, path, ancestors) {
175 if (!isEmptyElement(element)) return undefined;
176 const lineage = [...ancestors, parent].filter(_apidomCore.isElement);
177 const parentElement = lineage[lineage.length - 1]; // @TODO(vladimir.gorej@gmail.com): can be replaced by Array.prototype.at in future
178 let elementFactory;
179 let context;
180 if ((0, _apidomCore.isArrayElement)(parentElement)) {
181 context = element;
182 elementFactory = findElementFactory(parentElement, '<*>');
183 } else if ((0, _apidomCore.isMemberElement)(parentElement)) {
184 context = lineage[lineage.length - 2]; // @TODO(vladimir.gorej@gmail.com): can be replaced by Array.prototype.at in future
185 elementFactory = findElementFactory(context, (0, _apidomCore.toValue)(parentElement.key));
186 }
187
188 // no element factory found
189 if (typeof elementFactory !== 'function') return undefined;
190 return elementFactory.call({
191 context
192 }, undefined, (0, _apidomCore.cloneDeep)(element.meta), (0, _apidomCore.cloneDeep)(element.attributes));
193 }
194 }
195});
196var _default = exports.default = plugin;
Note: See TracBrowser for help on using the repository browser.