Skip to content
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

Merged
merged 5 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/rules/a11y-no-generic-link-text.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {elementType, getProp, getPropValue} = require('jsx-ast-utils')
const {getProp, getPropValue} = require('jsx-ast-utils')
const {getElementType} = require('../utils/get-element-type')

const bannedLinkText = ['read more', 'here', 'click here', 'learn more', 'more', 'here']

Expand All @@ -23,7 +24,9 @@ module.exports = {
create(context) {
return {
JSXOpeningElement: node => {
if (elementType(node) !== 'a') return
const elementType = getElementType(context, node)

if (elementType !== 'a') return
if (getProp(node.attributes, 'aria-labelledby')) return

let cleanTextContent // text content we can reliably fetch
Expand Down
36 changes: 36 additions & 0 deletions lib/utils/get-element-type.js
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.
Copy link
Contributor Author

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)

*/
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}
148 changes: 148 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought tests/**/*.js will run all tests including ones in nested folder but it only ran the nested tests. Therefore I had to specify tests/ too. Open to suggestion

},
"repository": {
"type": "git",
Expand Down Expand Up @@ -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"
Expand Down