Skip to content

Commit

Permalink
refactor(ns-json-schema-draft-6): simplify the replace empty elements…
Browse files Browse the repository at this point in the history
… refractor plugin (#3416)
  • Loading branch information
char0n authored Nov 20, 2023
1 parent 91310b6 commit 15d297a
Showing 1 changed file with 33 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
MemberElement,
ArrayElement,
ObjectElement,
StringElement,
isStringElement,
includesClasses,
isArrayElement,
isElement,
isMemberElement,
includesClasses,
cloneDeep,
toValue,
} from '@swagger-api/apidom-core';
Expand Down Expand Up @@ -198,58 +199,35 @@ const findElementFactory = (ancestor: any, keyName: string) => {
: keyMapping[keyName];
};

const plugin = () => () => {
return {
visitor: {
MemberElement(element: MemberElement, ...rest: any) {
// no empty Element, continue with next one
if (!isEmptyElement(element.value)) return undefined;

const [, , , ancestors] = rest;
const ancestor = ancestors[ancestors.length - 1]; // @TODO([email protected]): can be replaced by Array.prototype.at in future
const elementFactory = findElementFactory(ancestor, toValue(element.key));

// no element factory found
if (typeof elementFactory === 'undefined') return undefined;

const originalValue = element.value as StringElement;

return new MemberElement(
element.key,
elementFactory.call(
{ context: ancestor },
undefined,
cloneDeep(originalValue.meta),
cloneDeep(originalValue.attributes),
),
cloneDeep(element.meta),
cloneDeep(element.attributes),
);
},

StringElement(element: StringElement, ...rest: any) {
if (!isEmptyElement(element)) return undefined;

const [, , , ancestors] = rest;
const ancestor = ancestors[ancestors.length - 1];

// we're only interested in empty elements in ArrayElements
if (!isArrayElement(ancestor)) return undefined;

const elementFactory = findElementFactory(ancestor, '<*>');

// no element factory found
if (typeof elementFactory === 'undefined') return undefined;

return elementFactory.call(
{ context: element },
undefined,
cloneDeep(element.meta),
cloneDeep(element.attributes),
);
},
},
};
};
const plugin = () => () => ({
visitor: {
StringElement(element: StringElement, key: any, parent: any, path: any, ancestors: any[]) {
if (!isEmptyElement(element)) return undefined;

const lineage = [...ancestors, parent].filter(isElement);
const parentElement = lineage[lineage.length - 1]; // @TODO([email protected]): can be replaced by Array.prototype.at in future
let elementFactory;
let context;

if (isArrayElement(parentElement)) {
context = element;
elementFactory = findElementFactory(parentElement, '<*>');
} else if (isMemberElement(parentElement)) {
context = lineage[lineage.length - 2]; // @TODO([email protected]): can be replaced by Array.prototype.at in future
elementFactory = findElementFactory(context, toValue(parentElement.key));
}

// no element factory found
if (typeof elementFactory !== 'function') return undefined;

return elementFactory.call(
{ context },
undefined,
cloneDeep(element.meta),
cloneDeep(element.attributes),
);
},
},
});

export default plugin;

0 comments on commit 15d297a

Please sign in to comment.