Skip to content

Commit 9deac20

Browse files
committed
Fix bug
1 parent ca7a3c3 commit 9deac20

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

lib/utils/get-element-type.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ function getElementType(context, node, lazyElementCheck = false) {
1616

1717
// check if the node contains a polymorphic prop
1818
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
19-
const rawElement = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node)
2019

21-
// if a component configuration does not exists, return the raw element
22-
if (!settings?.github?.components?.[rawElement]) return rawElement
20+
const rawElement = elementType(node)
2321

24-
const defaultComponent = settings.github.components[rawElement]
25-
26-
// check if the default component is also defined in the configuration
27-
return defaultComponent ? defaultComponent : defaultComponent
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
28+
}
2829
}
2930

3031
module.exports = {getElementType}

tests/utils/get-element-type.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ describe('getElementType', function () {
6464
expect(getElementType({}, node)).to.equal('Box')
6565
})
6666

67-
it('returns raw type when polymorphic prop is set to component, and not the mapped value', function () {
68-
// <Box as={Link} />
67+
it('returns raw type when polymorphic prop is set to component even when there is a mapped value', function () {
68+
// <Box as={isNavigationOpen ? 'generic' : 'navigation'} />
6969
const setting = mockSetting({
7070
Box: 'div',
7171
})
7272

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

0 commit comments

Comments
 (0)