Skip to content

Fix bug in getElementType logic #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 20, 2024
Prev Previous commit
Next Next commit
fix bugs
  • Loading branch information
khiga8 committed May 16, 2024
commit 96000419462b4d7c60ce3d6b27e0832dd17908a2
21 changes: 13 additions & 8 deletions lib/utils/get-element-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ function getElementType(context, node, lazyElementCheck = false) {
// check if the node contains a polymorphic prop
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'

const rawElement = elementType(node)

if (getProp(node.attributes, polymorphicPropName)) {
return getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? rawElement
} else if (settings?.github?.components?.[rawElement]) {
return settings.github.components[rawElement]
} else {
return rawElement
let checkConditionalMap = true

const prop = getProp(node.attributes, polymorphicPropName)
const literalPropValue = getLiteralPropValue(getProp(node.attributes, polymorphicPropName))
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
}

module.exports = {getElementType}
4 changes: 1 addition & 3 deletions tests/utils/get-element-type.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {expect} from 'chai'
import {getElementType} from '../../lib/utils/get-element-type.js'
import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js'

import {describe, it} from 'mocha'

function mockSetting(componentSetting = {}) {
Expand Down Expand Up @@ -62,12 +61,11 @@ describe('getElementType', function () {
expect(getElementType({}, node)).to.equal('Box')
})

it('returns raw type when polymorphic prop is set to component even when there is a mapped value', function () {
it('returns raw type when polymorphic prop is set to non-literal expression even with component setting', function () {
// <Box as={isNavigationOpen ? 'generic' : 'navigation'} />
const setting = mockSetting({
Box: 'div',
})

const node = mockJSXOpeningElement('Box', [
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'),
])
Expand Down
Loading