diff --git a/lib/utils/get-element-type.js b/lib/utils/get-element-type.js index f4c62ff4..41488a15 100644 --- a/lib/utils/get-element-type.js +++ b/lib/utils/get-element-type.js @@ -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} diff --git a/tests/utils/get-element-type.mjs b/tests/utils/get-element-type.mjs index 059af41f..de8b9b96 100644 --- a/tests/utils/get-element-type.mjs +++ b/tests/utils/get-element-type.mjs @@ -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 = {}) { @@ -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 () { // const setting = mockSetting({ Box: 'div', }) - const node = mockJSXOpeningElement('Box', [ mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'), ])