-
Notifications
You must be signed in to change notification settings - Fork 51
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
[spike] Allow custom component to be mapped to element type #283
Changes from 4 commits
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const {elementType, getProp, getPropValue} = require('jsx-ast-utils') | ||
|
||
/* | ||
Allows custom component to be mapped to an element type. | ||
When a default is set, all instances of the component will be mapped to the default. | ||
If a prop determines the type, it can be specified with `props`. | ||
|
||
For now, we only support the mapping of one prop type to an element type, rather than combinations of props. | ||
*/ | ||
function getElementType(context, node) { | ||
const {settings} = context | ||
const rawElement = elementType(node) | ||
if (!settings) return rawElement | ||
|
||
const componentMap = settings['github']?.components | ||
khiga8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!componentMap) return rawElement | ||
const component = componentMap[rawElement] | ||
if (!component) return rawElement | ||
let element = component.default ? component.default : rawElement | ||
|
||
if (component.props) { | ||
const props = Object.entries(component.props) | ||
for (const [key, value] of props) { | ||
const propMap = value | ||
const propValue = getPropValue(getProp(node.attributes, key)) | ||
const mapValue = propMap[propValue] | ||
|
||
if (mapValue) { | ||
element = mapValue | ||
} | ||
} | ||
} | ||
return element | ||
} | ||
|
||
module.exports = {getElementType} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
"scripts": { | ||
"pretest": "mkdir -p node_modules/ && ln -fs $(pwd) node_modules/", | ||
"eslint-check": "eslint-config-prettier .eslintrc.js", | ||
"test": "npm run eslint-check && eslint . && mocha tests/" | ||
"test": "npm run eslint-check && eslint . && mocha tests/**/*.js tests/" | ||
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 thought |
||
}, | ||
"repository": { | ||
"type": "git", | ||
|
@@ -51,6 +51,7 @@ | |
], | ||
"devDependencies": { | ||
"@github/prettier-config": "0.0.4", | ||
"chai": "^4.3.6", | ||
"eslint": "^8.0.1", | ||
"eslint-plugin-eslint-plugin": "^5.0.0", | ||
"mocha": "^10.0.0" | ||
|
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.
This eslint custom component mapping approach was inspired by,
jsx-a11y
. however,jsx-a11y
only supports 1:1 mapping whereas we want to be able to associate a prop with a component.(read more about jsx-a11y custom component mapping)