-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bug in getElementType
logic
#525
Changes from 8 commits
ca7a3c3
9deac20
f7d1592
9600041
e6f3c9e
d134206
dd592c6
9d2dc5d
3979a24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this intentional @kendallgassner? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. definitely not |
||
return checkConditionalMap ? settings.github.components[rawElement] : rawElement | ||
} | ||
|
||
module.exports = {getElementType} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
"update:eslint-docs": "eslint-doc-generator" | ||
}, | ||
"repository": { | ||
|
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' | ||
Comment on lines
+2
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently I need to add the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I wonder if that's why? 🫤 |
||
import {describe, it} from 'mocha' | ||
|
||
function mockSetting(componentSetting = {}) { | ||
return { | ||
|
@@ -63,4 +60,15 @@ 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 () { | ||
// <Box as={isNavigationOpen ? 'generic' : 'navigation'} /> | ||
khiga8 marked this conversation as resolved.
Show resolved
Hide resolved
khiga8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const setting = mockSetting({ | ||
Box: 'div', | ||
}) | ||
const node = mockJSXOpeningElement('Box', [ | ||
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'), | ||
]) | ||
expect(getElementType(setting, node)).to.equal('Box') | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order for the JS files to be loaded into
.mjs
, we need an extension.This library seems to be using a mix of module JS and common JS now, so we'll want to turn this rule off.