-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ns-asyncapi-2): simplify the replace empty elements refracto…
…r plugin (#3414)
- Loading branch information
Showing
1 changed file
with
29 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
@@ -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; |