Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Only look at semantic elements for a11y-no-title-attribute #464

Merged
merged 3 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.git.luolix.topponents[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',
},
},
},
},
],
})