1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
4 | exports.__esModule = true;
|
---|
5 | exports.default = void 0;
|
---|
6 | var _stampit = _interopRequireDefault(require("stampit"));
|
---|
7 | var _ramda = require("ramda");
|
---|
8 | var _ramdaAdjunct = require("ramda-adjunct");
|
---|
9 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
10 | var _Visitor = _interopRequireDefault(require("./Visitor.cjs"));
|
---|
11 | /**
|
---|
12 | * This is a base Type for every visitor that does
|
---|
13 | * internal look-ups to retrieve other child visitors.
|
---|
14 | */
|
---|
15 | const SpecificationVisitor = (0, _stampit.default)(_Visitor.default, {
|
---|
16 | props: {
|
---|
17 | specObj: null,
|
---|
18 | passingOptionsNames: ['specObj']
|
---|
19 | },
|
---|
20 | init({
|
---|
21 | // @ts-ignore
|
---|
22 | specObj = this.specObj
|
---|
23 | }) {
|
---|
24 | this.specObj = specObj;
|
---|
25 | },
|
---|
26 | methods: {
|
---|
27 | retrievePassingOptions() {
|
---|
28 | return (0, _ramda.pick)(this.passingOptionsNames, this);
|
---|
29 | },
|
---|
30 | retrieveFixedFields(specPath) {
|
---|
31 | const fixedFields = (0, _ramda.path)(['visitors', ...specPath, 'fixedFields'], this.specObj);
|
---|
32 | if (typeof fixedFields === 'object' && fixedFields !== null) {
|
---|
33 | return Object.keys(fixedFields);
|
---|
34 | }
|
---|
35 | return [];
|
---|
36 | },
|
---|
37 | retrieveVisitor(specPath) {
|
---|
38 | if ((0, _ramda.pathSatisfies)(_ramdaAdjunct.isFunction, ['visitors', ...specPath], this.specObj)) {
|
---|
39 | return (0, _ramda.path)(['visitors', ...specPath], this.specObj);
|
---|
40 | }
|
---|
41 | return (0, _ramda.path)(['visitors', ...specPath, '$visitor'], this.specObj);
|
---|
42 | },
|
---|
43 | retrieveVisitorInstance(specPath, options = {}) {
|
---|
44 | const passingOpts = this.retrievePassingOptions();
|
---|
45 | const VisitorClz = this.retrieveVisitor(specPath);
|
---|
46 | const visitorOpts = {
|
---|
47 | ...passingOpts,
|
---|
48 | ...options
|
---|
49 | };
|
---|
50 |
|
---|
51 | // @ts-ignore
|
---|
52 | return new VisitorClz(visitorOpts);
|
---|
53 | },
|
---|
54 | toRefractedElement(specPath, element, options = {}) {
|
---|
55 | /**
|
---|
56 | * This is `Visitor shortcut`: mechanism for short circuiting the traversal and replacing
|
---|
57 | * it by basic node cloning.
|
---|
58 | *
|
---|
59 | * Visiting the element is equivalent to cloning it if the prototype of a visitor
|
---|
60 | * is the same as the prototype of FallbackVisitor. If that's the case, we can avoid
|
---|
61 | * bootstrapping the traversal cycle for fields that don't require any special visiting.
|
---|
62 | */
|
---|
63 | const visitor = this.retrieveVisitorInstance(specPath, options);
|
---|
64 | const visitorPrototype = Object.getPrototypeOf(visitor);
|
---|
65 | if ((0, _ramdaAdjunct.isUndefined)(this.fallbackVisitorPrototype)) {
|
---|
66 | this.fallbackVisitorPrototype = Object.getPrototypeOf(this.retrieveVisitorInstance(['value']));
|
---|
67 | }
|
---|
68 | if (this.fallbackVisitorPrototype === visitorPrototype) {
|
---|
69 | return (0, _apidomCore.cloneDeep)(element);
|
---|
70 | }
|
---|
71 | (0, _apidomCore.visit)(element, visitor, options);
|
---|
72 | return visitor.element;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | });
|
---|
76 | var _default = exports.default = SpecificationVisitor; |
---|