Skip to content

Commit

Permalink
Merge pull request #464 from github/fix-a11y-no-title-attribute
Browse files Browse the repository at this point in the history
[Fix] Only look at semantic elements for `a11y-no-title-attribute`
  • Loading branch information
kendallgassner authored Jul 17, 2023
2 parents 8abef65 + ee9e16d commit 125ac51
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/rules/a11y-no-title-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {
create(context) {
return {
JSXElement: node => {
const elementType = getElementType(context, node.openingElement)
const elementType = getElementType(context, node.openingElement, true)
if (elementType !== `iframe` && ifSemanticElement(context, node)) {
const titleProp = getPropValue(getProp(node.openingElement.attributes, `title`))
if (titleProp) {
Expand Down
8 changes: 6 additions & 2 deletions lib/utils/get-element-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ 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.
*/
function getElementType(context, node, ignoreMap = false) {
function getElementType(context, node, lazyElementCheck = false) {
const {settings} = context

if (lazyElementCheck) {
return elementType(node)
}

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

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

const defaultComponent = settings.github.components[rawElement]

Expand Down
15 changes: 4 additions & 11 deletions tests/a11y-no-title-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,13 @@ ruleTester.run('a11y-no-title-attribute', rule, {
},
},
},
{
// Note: we are only checking semantic elements. We cannot make assumptions about how a React Components is using the title prop.
code: '<Link as="a" title="some title">Submit</Link>',
},
],
invalid: [
{code: '<a title="some title" href="github.com">GitHub</a>', errors: [{message: errorMessage}]},
{code: '<span><button title="some title">submit</button></span>', errors: [{message: errorMessage}]},
{
code: '<Component as="a" title="some title">Submit</Component>',
errors: [{message: errorMessage}],
settings: {
github: {
components: {
Component: 'iframe',
},
},
},
},
],
})

0 comments on commit 125ac51

Please sign in to comment.