Skip to content

Commit 1883f0b

Browse files
committed
Add custom element mapping support
1 parent cd9d7eb commit 1883f0b

6 files changed

Lines changed: 351 additions & 4 deletions

File tree

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.js')
23

34
const bannedLinkText = ['read more', 'here', 'click here', 'learn more', 'more', 'here']
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const {elementType, getProp, getPropValue} = require('jsx-ast-utils')
2+
3+
function getElementType(context, node) {
4+
const {settings} = context
5+
const rawElement = elementType(node)
6+
if (!settings) return rawElement
7+
8+
const componentMap = settings['github']?.components
9+
if (!componentMap) return rawElement
10+
const component = componentMap[rawElement]
11+
if (!component) return rawElement
12+
let element = component.default ? component.default : rawElement
13+
14+
if (component.props) {
15+
const props = Object.entries(component.props)
16+
for (const [key, value] of props) {
17+
const propMap = value
18+
let propValue = getPropValue(getProp(node.attributes, key))
19+
let mapValue = propMap[propValue]
20+
21+
if (mapValue) {
22+
element = mapValue
23+
}
24+
}
25+
}
26+
return element
27+
}
28+
29+
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": "mocha ./tests/**/*.js ./tests/*.js"
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"

tests/a11y-no-generic-link-text.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,65 @@ ruleTester.run('a11y-no-generic-link-text', rule, {
1919
{code: "<a href='#'>GitHub Home</a>;"},
2020
{code: "<Box><a href='#'>GitHub Home</a></Box>;"},
2121
{code: "<a aria-label='Read more about our project' href='#'>Read more</a>;"},
22-
{code: "<a aria-labelledby='someId' href='#'>Read more</a>;"}
22+
{code: "<a aria-labelledby='someId' href='#'>Read more</a>;"},
23+
{code: '<summary>Read more</summary>;'},
24+
{
25+
code: '<Link as="button" href="#">Read more</Link>',
26+
settings: {
27+
github: {
28+
components: {
29+
Link: {
30+
props: { as: {undefined: 'a'} }
31+
}
32+
}
33+
}
34+
}
35+
},
2336
],
2437
invalid: [
38+
{
39+
code: '<ButtonLink href="#">Read more</ButtonLink>',
40+
settings: {
41+
github: {
42+
components: {
43+
ButtonLink: {
44+
default: 'a'
45+
}
46+
}
47+
}
48+
},
49+
errors: [{message: errorMessage}]
50+
},
51+
{
52+
code: '<Link href="#">Read more</Link>',
53+
settings: {
54+
github: {
55+
components: {
56+
Link: {
57+
props: { as: {undefined: 'a'} }
58+
}
59+
}
60+
}
61+
},
62+
errors: [{message: errorMessage}]
63+
},
64+
{
65+
code: '<Test as="a" href="#">Read more</Test>',
66+
settings: {
67+
github: {
68+
components: {
69+
Test: {
70+
props: { as: {'a': 'a'} }
71+
}
72+
}
73+
}
74+
},
75+
errors: [{message: errorMessage}]
76+
},
77+
{
78+
code: "<Box><a href='#'>Click here</a></Box>;",
79+
errors: [{message: errorMessage}]
80+
},
2581
{code: '<a>Click here*</a>;', errors: [{message: errorMessage}]},
2682
{code: '<a>Learn more.</a>;', errors: [{message: errorMessage}]},
2783
{code: "<a aria-label='read more!!!'></a>;", errors: [{message: errorMessage}]},

0 commit comments

Comments
 (0)