source: node_modules/@swagger-api/apidom-ns-json-schema-draft-4/es/refractor/visitors/SpecificationVisitor.mjs

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

Initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1import stampit from 'stampit';
2import { pathSatisfies, path, pick } from 'ramda';
3import { isFunction, isUndefined } from 'ramda-adjunct';
4import { visit, cloneDeep } from '@swagger-api/apidom-core';
5import Visitor from "./Visitor.mjs";
6/**
7 * This is a base Type for every visitor that does
8 * internal look-ups to retrieve other child visitors.
9 */
10const SpecificationVisitor = stampit(Visitor, {
11 props: {
12 specObj: null,
13 passingOptionsNames: ['specObj']
14 },
15 init({
16 // @ts-ignore
17 specObj = this.specObj
18 }) {
19 this.specObj = specObj;
20 },
21 methods: {
22 retrievePassingOptions() {
23 return pick(this.passingOptionsNames, this);
24 },
25 retrieveFixedFields(specPath) {
26 const fixedFields = path(['visitors', ...specPath, 'fixedFields'], this.specObj);
27 if (typeof fixedFields === 'object' && fixedFields !== null) {
28 return Object.keys(fixedFields);
29 }
30 return [];
31 },
32 retrieveVisitor(specPath) {
33 if (pathSatisfies(isFunction, ['visitors', ...specPath], this.specObj)) {
34 return path(['visitors', ...specPath], this.specObj);
35 }
36 return path(['visitors', ...specPath, '$visitor'], this.specObj);
37 },
38 retrieveVisitorInstance(specPath, options = {}) {
39 const passingOpts = this.retrievePassingOptions();
40 const VisitorClz = this.retrieveVisitor(specPath);
41 const visitorOpts = {
42 ...passingOpts,
43 ...options
44 };
45
46 // @ts-ignore
47 return new VisitorClz(visitorOpts);
48 },
49 toRefractedElement(specPath, element, options = {}) {
50 /**
51 * This is `Visitor shortcut`: mechanism for short circuiting the traversal and replacing
52 * it by basic node cloning.
53 *
54 * Visiting the element is equivalent to cloning it if the prototype of a visitor
55 * is the same as the prototype of FallbackVisitor. If that's the case, we can avoid
56 * bootstrapping the traversal cycle for fields that don't require any special visiting.
57 */
58 const visitor = this.retrieveVisitorInstance(specPath, options);
59 const visitorPrototype = Object.getPrototypeOf(visitor);
60 if (isUndefined(this.fallbackVisitorPrototype)) {
61 this.fallbackVisitorPrototype = Object.getPrototypeOf(this.retrieveVisitorInstance(['value']));
62 }
63 if (this.fallbackVisitorPrototype === visitorPrototype) {
64 return cloneDeep(element);
65 }
66 visit(element, visitor, options);
67 return visitor.element;
68 }
69 }
70});
71export default SpecificationVisitor;
Note: See TracBrowser for help on using the repository browser.