Skip to content

Commit 9600041

Browse files
committed
fix bugs
1 parent f7d1592 commit 9600041

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lib/utils/get-element-type.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ function getElementType(context, node, lazyElementCheck = false) {
1717
// check if the node contains a polymorphic prop
1818
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
1919

20-
const rawElement = elementType(node)
21-
22-
if (getProp(node.attributes, polymorphicPropName)) {
23-
return getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? rawElement
24-
} else if (settings?.github?.components?.[rawElement]) {
25-
return settings.github.components[rawElement]
26-
} else {
27-
return rawElement
20+
let checkConditionalMap = true
21+
22+
const prop = getProp(node.attributes, polymorphicPropName)
23+
const literalPropValue = getLiteralPropValue(getProp(node.attributes, polymorphicPropName))
24+
if (prop && !literalPropValue) {
25+
checkConditionalMap = false
2826
}
27+
const rawElement = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node)
28+
29+
// if a component configuration does not exists, return the raw element
30+
if (!settings?.github?.components?.[rawElement]) return rawElement
31+
32+
// check if the default component is also defined in the configuration
33+
return checkConditionalMap ? settings.github.components[rawElement] : rawElement
2934
}
3035

3136
module.exports = {getElementType}

tests/utils/get-element-type.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {expect} from 'chai'
22
import {getElementType} from '../../lib/utils/get-element-type.js'
33
import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js'
4-
54
import {describe, it} from 'mocha'
65

76
function mockSetting(componentSetting = {}) {
@@ -62,12 +61,11 @@ describe('getElementType', function () {
6261
expect(getElementType({}, node)).to.equal('Box')
6362
})
6463

65-
it('returns raw type when polymorphic prop is set to component even when there is a mapped value', function () {
64+
it('returns raw type when polymorphic prop is set to non-literal expression even with component setting', function () {
6665
// <Box as={isNavigationOpen ? 'generic' : 'navigation'} />
6766
const setting = mockSetting({
6867
Box: 'div',
6968
})
70-
7169
const node = mockJSXOpeningElement('Box', [
7270
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'),
7371
])

0 commit comments

Comments
 (0)