-
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-json-schema-draft-6): simplify the replace empty elements…
… refractor plugin (#3416)
- Loading branch information
Showing
1 changed file
with
33 additions
and
55 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,11 +1,12 @@ | ||
import { | ||
MemberElement, | ||
ArrayElement, | ||
ObjectElement, | ||
StringElement, | ||
isStringElement, | ||
includesClasses, | ||
isArrayElement, | ||
isElement, | ||
isMemberElement, | ||
includesClasses, | ||
cloneDeep, | ||
toValue, | ||
} from '@swagger-api/apidom-core'; | ||
|
@@ -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; |