Skip to content

Commit

Permalink
refactor(ns-asyncapi-2): simplify the replace empty elements refracto…
Browse files Browse the repository at this point in the history
…r plugin (#3414)
  • Loading branch information
char0n authored Nov 20, 2023
1 parent 3aaf92b commit e60194b
Showing 1 changed file with 29 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { defaultTo } from 'ramda';
import {
MemberElement,
StringElement,
ObjectElement,
ArrayElement,
isStringElement,
includesClasses,
isArrayElement,
isMemberElement,
isElement,
includesClasses,
cloneDeep,
toValue,
} from '@swagger-api/apidom-core';
Expand Down Expand Up @@ -1036,58 +1037,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),
);
},
const plugin = () => () => ({
visitor: {
StringElement(element: StringElement, key: any, parent: any, path: any, ancestors: any[]) {
if (!isEmptyElement(element)) return undefined;

StringElement(element: StringElement, ...rest: 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;

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

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

const elementFactory = findElementFactory(ancestor, '<*>');
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 === 'undefined') return undefined;
// no element factory found
if (typeof elementFactory !== 'function') return undefined;

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

export default plugin;

0 comments on commit e60194b

Please sign in to comment.