diff --git a/.eslintrc.js b/.eslintrc.js index 329ac0be..4d1c4835 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,6 +10,7 @@ module.exports = { extends: [require.resolve('./lib/configs/recommended'), 'plugin:eslint-plugin/all'], plugins: ['eslint-plugin'], rules: { + 'import/extensions': 'off', 'import/no-commonjs': 'off', 'filenames/match-regex': 'off', 'i18n-text/no-en': 'off', diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 200cc642..56d95857 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [14, 16, 18] + node-version: [18, 20] steps: - uses: actions/checkout@v4 diff --git a/lib/utils/get-element-type.js b/lib/utils/get-element-type.js index 16d7c07f..74e6fe39 100644 --- a/lib/utils/get-element-type.js +++ b/lib/utils/get-element-type.js @@ -16,15 +16,22 @@ function getElementType(context, node, lazyElementCheck = false) { // check if the node contains a polymorphic prop const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as' + + const prop = getProp(node.attributes, polymorphicPropName) + const literalPropValue = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) + let checkConditionalMap = true + + // If the prop is not a literal and we cannot determine it, don't fall back to the conditional map value, if it exists + 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 - const defaultComponent = settings.github.components[rawElement] - // check if the default component is also defined in the configuration - return defaultComponent ? defaultComponent : defaultComponent + return checkConditionalMap ? settings.github.components[rawElement] : rawElement } module.exports = {getElementType} diff --git a/package.json b/package.json index 8524b81c..82b51afe 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "lint:eslint-docs": "npm run update:eslint-docs -- --check", "lint:js": "eslint .", "pretest": "mkdir -p node_modules/ && ln -fs $(pwd) node_modules/", - "test": "npm run eslint-check && npm run lint && mocha tests/**/*.js tests/", + "test": "npm run eslint-check && npm run lint && mocha tests/**/*.js tests/**/*.mjs", "update:eslint-docs": "eslint-doc-generator" }, "repository": { diff --git a/tests/utils/get-element-type.mjs b/tests/utils/get-element-type.mjs index 2d496b8a..80215054 100644 --- a/tests/utils/get-element-type.mjs +++ b/tests/utils/get-element-type.mjs @@ -1,10 +1,7 @@ import {expect} from 'chai' -import {getElementType} from '../../lib/utils/get-element-type' -import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks' - -const mocha = require('mocha') -const describe = mocha.describe -const it = mocha.it +import {getElementType} from '../../lib/utils/get-element-type.js' +import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js' +import {describe, it} from 'mocha' function mockSetting(componentSetting = {}) { return { @@ -63,4 +60,14 @@ describe('getElementType', function () { ]) expect(getElementType({}, node)).to.equal('Box') }) + + 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'), + ]) + expect(getElementType(setting, node)).to.equal('Box') + }) }) diff --git a/tests/utils/get-role.mjs b/tests/utils/get-role.mjs index 24986f4e..fc206f40 100644 --- a/tests/utils/get-role.mjs +++ b/tests/utils/get-role.mjs @@ -1,6 +1,6 @@ import {expect} from 'chai' -import {getRole} from '../../lib/utils/get-role' -import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks' +import {getRole} from '../../lib/utils/get-role.js' +import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js' import {describe, it} from 'mocha' describe('getRole', function () { diff --git a/tests/utils/object-map.mjs b/tests/utils/object-map.mjs index 033e6e9b..3d7b4521 100644 --- a/tests/utils/object-map.mjs +++ b/tests/utils/object-map.mjs @@ -1,6 +1,6 @@ // @ts-check import {expect} from 'chai' -import ObjectMap from '../../lib/utils/object-map' +import ObjectMap from '../../lib/utils/object-map.js' import {describe, it} from 'mocha' describe('ObjectMap', function () {