Skip to content

Commit

Permalink
Merge pull request #525 from github/kh-fix-bug-in-get-element-type-logic
Browse files Browse the repository at this point in the history
Fix bug in `getElementType` logic
  • Loading branch information
khiga8 committed May 20, 2024
2 parents 79ec251 + 3979a24 commit ce529f9
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [14, 16, 18]
node-version: [18, 20]

steps:
- uses: actions/checkout@v4
Expand Down
13 changes: 10 additions & 3 deletions lib/utils/get-element-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
19 changes: 13 additions & 6 deletions tests/utils/get-element-type.mjs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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')
})
})
4 changes: 2 additions & 2 deletions tests/utils/get-role.mjs
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/object-map.mjs
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down

0 comments on commit ce529f9

Please sign in to comment.