Skip to content

Commit 125ac51

Browse files
Merge pull request #464 from github/fix-a11y-no-title-attribute
[Fix] Only look at semantic elements for `a11y-no-title-attribute`
2 parents 8abef65 + ee9e16d commit 125ac51

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

lib/rules/a11y-no-title-attribute.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
create(context) {
5151
return {
5252
JSXElement: node => {
53-
const elementType = getElementType(context, node.openingElement)
53+
const elementType = getElementType(context, node.openingElement, true)
5454
if (elementType !== `iframe` && ifSemanticElement(context, node)) {
5555
const titleProp = getPropValue(getProp(node.openingElement.attributes, `title`))
5656
if (titleProp) {

lib/utils/get-element-type.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ If a prop determines the type, it can be specified with `props`.
77
88
For now, we only support the mapping of one prop type to an element type, rather than combinations of props.
99
*/
10-
function getElementType(context, node, ignoreMap = false) {
10+
function getElementType(context, node, lazyElementCheck = false) {
1111
const {settings} = context
1212

13+
if (lazyElementCheck) {
14+
return elementType(node)
15+
}
16+
1317
// check if the node contains a polymorphic prop
1418
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
1519
const rawElement = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node)
1620

1721
// if a component configuration does not exists, return the raw element
18-
if (ignoreMap || !settings?.github?.components?.[rawElement]) return rawElement
22+
if (!settings?.github?.components?.[rawElement]) return rawElement
1923

2024
const defaultComponent = settings.github.components[rawElement]
2125

tests/a11y-no-title-attribute.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,13 @@ ruleTester.run('a11y-no-title-attribute', rule, {
4040
},
4141
},
4242
},
43+
{
44+
// Note: we are only checking semantic elements. We cannot make assumptions about how a React Components is using the title prop.
45+
code: '<Link as="a" title="some title">Submit</Link>',
46+
},
4347
],
4448
invalid: [
4549
{code: '<a title="some title" href="github.com">GitHub</a>', errors: [{message: errorMessage}]},
4650
{code: '<span><button title="some title">submit</button></span>', errors: [{message: errorMessage}]},
47-
{
48-
code: '<Component as="a" title="some title">Submit</Component>',
49-
errors: [{message: errorMessage}],
50-
settings: {
51-
github: {
52-
components: {
53-
Component: 'iframe',
54-
},
55-
},
56-
},
57-
},
5851
],
5952
})

0 commit comments

Comments
 (0)