Skip to content

Commit ce529f9

Browse files
authored
Merge pull request #525 from github/kh-fix-bug-in-get-element-type-logic
Fix bug in `getElementType` logic
2 parents 79ec251 + 3979a24 commit ce529f9

File tree

7 files changed

+29
-14
lines changed

7 files changed

+29
-14
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
extends: [require.resolve('./lib/configs/recommended'), 'plugin:eslint-plugin/all'],
1111
plugins: ['eslint-plugin'],
1212
rules: {
13+
'import/extensions': 'off',
1314
'import/no-commonjs': 'off',
1415
'filenames/match-regex': 'off',
1516
'i18n-text/no-en': 'off',

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [14, 16, 18]
15+
node-version: [18, 20]
1616

1717
steps:
1818
- uses: actions/checkout@v4

lib/utils/get-element-type.js

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

1717
// check if the node contains a polymorphic prop
1818
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
19+
20+
const prop = getProp(node.attributes, polymorphicPropName)
21+
const literalPropValue = getLiteralPropValue(getProp(node.attributes, polymorphicPropName))
22+
let checkConditionalMap = true
23+
24+
// If the prop is not a literal and we cannot determine it, don't fall back to the conditional map value, if it exists
25+
if (prop && !literalPropValue) {
26+
checkConditionalMap = false
27+
}
1928
const rawElement = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node)
2029

2130
// if a component configuration does not exists, return the raw element
2231
if (!settings?.github?.components?.[rawElement]) return rawElement
2332

24-
const defaultComponent = settings.github.components[rawElement]
25-
2633
// check if the default component is also defined in the configuration
27-
return defaultComponent ? defaultComponent : defaultComponent
34+
return checkConditionalMap ? settings.github.components[rawElement] : rawElement
2835
}
2936

3037
module.exports = {getElementType}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"lint:eslint-docs": "npm run update:eslint-docs -- --check",
1616
"lint:js": "eslint .",
1717
"pretest": "mkdir -p node_modules/ && ln -fs $(pwd) node_modules/",
18-
"test": "npm run eslint-check && npm run lint && mocha tests/**/*.js tests/",
18+
"test": "npm run eslint-check && npm run lint && mocha tests/**/*.js tests/**/*.mjs",
1919
"update:eslint-docs": "eslint-doc-generator"
2020
},
2121
"repository": {

tests/utils/get-element-type.mjs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import {expect} from 'chai'
2-
import {getElementType} from '../../lib/utils/get-element-type'
3-
import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks'
4-
5-
const mocha = require('mocha')
6-
const describe = mocha.describe
7-
const it = mocha.it
2+
import {getElementType} from '../../lib/utils/get-element-type.js'
3+
import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js'
4+
import {describe, it} from 'mocha'
85

96
function mockSetting(componentSetting = {}) {
107
return {
@@ -63,4 +60,14 @@ describe('getElementType', function () {
6360
])
6461
expect(getElementType({}, node)).to.equal('Box')
6562
})
63+
64+
it('returns raw type when polymorphic prop is set to non-literal expression even with component setting', function () {
65+
const setting = mockSetting({
66+
Box: 'div',
67+
})
68+
const node = mockJSXOpeningElement('Box', [
69+
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'),
70+
])
71+
expect(getElementType(setting, node)).to.equal('Box')
72+
})
6673
})

tests/utils/get-role.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {expect} from 'chai'
2-
import {getRole} from '../../lib/utils/get-role'
3-
import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks'
2+
import {getRole} from '../../lib/utils/get-role.js'
3+
import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js'
44
import {describe, it} from 'mocha'
55

66
describe('getRole', function () {

tests/utils/object-map.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
import {expect} from 'chai'
3-
import ObjectMap from '../../lib/utils/object-map'
3+
import ObjectMap from '../../lib/utils/object-map.js'
44
import {describe, it} from 'mocha'
55

66
describe('ObjectMap', function () {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy