Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
khiga8 committed May 16, 2024
1 parent ca7a3c3 commit 87a0e9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 8 additions & 7 deletions lib/utils/get-element-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ function getElementType(context, node, lazyElementCheck = false) {

// check if the node contains a polymorphic prop
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
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
const rawElement = elementType(node)

const defaultComponent = settings.github.components[rawElement]

// check if the default component is also defined in the configuration
return defaultComponent ? defaultComponent : defaultComponent
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
}
}

module.exports = {getElementType}
6 changes: 4 additions & 2 deletions tests/utils/get-element-type.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ describe('getElementType', function () {
expect(getElementType({}, node)).to.equal('Box')
})

it('returns raw type when polymorphic prop is set to component, and not the mapped value', function () {
it('returns raw type when polymorphic prop is set to com, and not the mapped value', function () {
// <Box as={Link} />
const setting = mockSetting({
Box: 'div',
})

// eslint-disable-next-line no-undef
const node = mockJSXOpeningElement('Box', [mockJSXAttribute('as', Link)])
const node = mockJSXOpeningElement('Box', [
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'),
])
expect(getElementType(setting, node)).to.equal('Box')
})
})

0 comments on commit 87a0e9f

Please sign in to comment.