Skip to content

Commit 9d77cf3

Browse files
authored
Merge pull request #283 from github/kh-add-custom-component-support
[spike] Allow custom component to be mapped to element type
2 parents 70a4fb0 + 7e976bf commit 9d77cf3

File tree

6 files changed

+356
-4
lines changed

6 files changed

+356
-4
lines changed

lib/rules/a11y-no-generic-link-text.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const {elementType, getProp, getPropValue} = require('jsx-ast-utils')
1+
const {getProp, getPropValue} = require('jsx-ast-utils')
2+
const {getElementType} = require('../utils/get-element-type')
23

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

@@ -23,7 +24,9 @@ module.exports = {
2324
create(context) {
2425
return {
2526
JSXOpeningElement: node => {
26-
if (elementType(node) !== 'a') return
27+
const elementType = getElementType(context, node)
28+
29+
if (elementType !== 'a') return
2730
if (getProp(node.attributes, 'aria-labelledby')) return
2831

2932
let cleanTextContent // text content we can reliably fetch

lib/utils/get-element-type.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const {elementType, getProp, getPropValue} = require('jsx-ast-utils')
2+
3+
/*
4+
Allows custom component to be mapped to an element type.
5+
When a default is set, all instances of the component will be mapped to the default.
6+
If a prop determines the type, it can be specified with `props`.
7+
8+
For now, we only support the mapping of one prop type to an element type, rather than combinations of props.
9+
*/
10+
function getElementType(context, node) {
11+
const {settings} = context
12+
const rawElement = elementType(node)
13+
if (!settings) return rawElement
14+
15+
const componentMap = settings.github && settings.github.components
16+
if (!componentMap) return rawElement
17+
const component = componentMap[rawElement]
18+
if (!component) return rawElement
19+
let element = component.default ? component.default : rawElement
20+
21+
if (component.props) {
22+
const props = Object.entries(component.props)
23+
for (const [key, value] of props) {
24+
const propMap = value
25+
const propValue = getPropValue(getProp(node.attributes, key))
26+
const mapValue = propMap[propValue]
27+
28+
if (mapValue) {
29+
element = mapValue
30+
}
31+
}
32+
}
33+
return element
34+
}
35+
36+
module.exports = {getElementType}

package-lock.json

Lines changed: 148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"scripts": {
1313
"pretest": "mkdir -p node_modules/ && ln -fs $(pwd) node_modules/",
1414
"eslint-check": "eslint-config-prettier .eslintrc.js",
15-
"test": "npm run eslint-check && eslint . && mocha tests/"
15+
"test": "npm run eslint-check && eslint . && mocha tests/**/*.js tests/"
1616
},
1717
"repository": {
1818
"type": "git",
@@ -51,6 +51,7 @@
5151
],
5252
"devDependencies": {
5353
"@github/prettier-config": "0.0.4",
54+
"chai": "^4.3.6",
5455
"eslint": "^8.0.1",
5556
"eslint-plugin-eslint-plugin": "^5.0.0",
5657
"mocha": "^10.0.0"

0 commit comments

Comments
 (0)