Skip to content

Commit 35ef90c

Browse files
clean
1 parent 92f656d commit 35ef90c

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
const {getProp, getPropValue} = require('jsx-ast-utils')
22
const {getElementType} = require('../utils/get-element-type')
33

4+
const SEMANTIC_ELEMENTS = [
5+
'a',
6+
'button',
7+
'summary',
8+
'select',
9+
'option',
10+
'textarea',
11+
'input',
12+
'span',
13+
'div',
14+
'p',
15+
'h1',
16+
'h2',
17+
'h3',
18+
'h4',
19+
'h5',
20+
'h6',
21+
'details',
22+
'summary',
23+
'dialog',
24+
'tr',
25+
'th',
26+
'td',
27+
'label',
28+
]
29+
30+
const ifSemanticElement = (context, node) => {
31+
const elementType = getElementType(context, node.openingElement, true)
32+
33+
for (const semanticElement of SEMANTIC_ELEMENTS) {
34+
if (elementType === semanticElement) {
35+
return true
36+
}
37+
}
38+
return false
39+
}
40+
441
module.exports = {
542
meta: {
643
docs: {
@@ -14,7 +51,7 @@ module.exports = {
1451
return {
1552
JSXElement: node => {
1653
const elementType = getElementType(context, node.openingElement)
17-
if (elementType !== `iframe`) {
54+
if (elementType !== `iframe` && ifSemanticElement(context, node)) {
1855
const titleProp = getPropValue(getProp(node.openingElement.attributes, `title`))
1956
if (titleProp) {
2057
context.report({

lib/rules/a11y-no-visually-hidden-interactive-element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ const schema = generateObjSchema({
1515
* because a visually hidden input field might cause a false positive.
1616
* (e.g. fileUpload https://github.com/primer/react/pull/3492)
1717
*/
18-
const INTERACTIVELEMENTS = ['a', 'button', 'summary', 'select', 'option', 'textarea']
18+
const INTERACTIVE_ELEMENTS = ['a', 'button', 'summary', 'select', 'option', 'textarea']
1919

2020
const checkIfInteractiveElement = (context, node) => {
2121
const elementType = getElementType(context, node.openingElement)
2222

23-
for (const interactiveElement of INTERACTIVELEMENTS) {
23+
for (const interactiveElement of INTERACTIVE_ELEMENTS) {
2424
if (elementType === interactiveElement) {
2525
return true
2626
}

lib/utils/get-element-type.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ 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) {
10+
function getElementType(context, node, ignoreMap = false) {
1111
const {settings} = context
1212

1313
// check if the node contains a polymorphic prop
1414
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
1515
const rawElement = getPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node)
1616

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

2020
const defaultComponent = settings.github.components[rawElement]
2121

tests/a11y-no-title-attribute.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ ruleTester.run('a11y-no-title-attribute', rule, {
2929
},
3030
},
3131
},
32-
],
33-
invalid: [
34-
{code: '<a title="some title" href="github.com">GitHub</a>', errors: [{message: errorMessage}]},
35-
{code: '<span><button title="some title">submit</button></span>', errors: [{message: errorMessage}]},
3632
{
33+
// Note: we are only checking semantic elements. We cannot make assumptions about how a React Components is using the title prop.
3734
code: '<Link title="some title">Submit</Link>',
38-
errors: [{message: errorMessage}],
3935
settings: {
4036
github: {
4137
components: {
@@ -44,6 +40,10 @@ ruleTester.run('a11y-no-title-attribute', rule, {
4440
},
4541
},
4642
},
43+
],
44+
invalid: [
45+
{code: '<a title="some title" href="github.com">GitHub</a>', errors: [{message: errorMessage}]},
46+
{code: '<span><button title="some title">submit</button></span>', errors: [{message: errorMessage}]},
4747
{
4848
code: '<Component as="a" title="some title">Submit</Component>',
4949
errors: [{message: errorMessage}],

0 commit comments

Comments
 (0)