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 bug
  • Loading branch information
khiga8 committed May 16, 2024
commit 9deac20a7c1704a93ea7537e169c535eb5131774
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional @kendallgassner?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely not

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}
9 changes: 5 additions & 4 deletions tests/utils/get-element-type.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ 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 () {
// <Box as={Link} />
it('returns raw type when polymorphic prop is set to component even when there is a mapped value', function () {
// <Box as={isNavigationOpen ? 'generic' : 'navigation'} />
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')
})
})
Loading