-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathget-element-type.js
37 lines (28 loc) · 1.48 KB
/
get-element-type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import jsxAstUtils from 'jsx-ast-utils'
const {elementType, getProp, getLiteralPropValue} = jsxAstUtils
/*
Allows custom component to be mapped to an element type.
When a default is set, all instances of the component will be mapped to the default.
If a prop determines the type, it can be specified with `props`.
For now, we only support the mapping of one prop type to an element type, rather than combinations of props.
*/
export function getElementType(context, node, lazyElementCheck = false) {
const {settings} = context
if (lazyElementCheck) {
return elementType(node)
}
// check if the node contains a polymorphic prop
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
const prop = getProp(node.attributes, polymorphicPropName)
const literalPropValue = getLiteralPropValue(getProp(node.attributes, polymorphicPropName))
let checkConditionalMap = true
// If the prop is not a literal and we cannot determine it, don't fall back to the conditional map value, if it exists
if (prop && !literalPropValue) {
checkConditionalMap = false
}
const rawElement = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node)
// if a component configuration does not exists, return the raw element
if (!settings?.github?.components?.[rawElement]) return rawElement
// check if the default component is also defined in the configuration
return checkConditionalMap ? settings.github.components[rawElement] : rawElement
}