Permalink
Cannot retrieve contributors at this time
41 lines (35 sloc)
1.48 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/node_modules/eslint-plugin-github/lib/rules/a11y-svg-has-accessible-name.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {hasProp} = require('jsx-ast-utils') | |
const {getElementType} = require('../utils/get-element-type') | |
module.exports = { | |
meta: { | |
docs: { | |
description: 'SVGs must have an accessible name', | |
url: require('../url')(module), | |
}, | |
schema: [], | |
}, | |
create(context) { | |
return { | |
JSXOpeningElement: node => { | |
const elementType = getElementType(context, node) | |
if (elementType !== 'svg') return | |
// Check if there is a nested title element that is the first child of the `<svg>` | |
const hasNestedTitleAsFirstChild = | |
node.parent.children?.[0]?.type === 'JSXElement' && | |
node.parent.children?.[0]?.openingElement?.name?.name === 'title' | |
// Check if `aria-label` or `aria-labelledby` is set | |
const hasAccessibleName = hasProp(node.attributes, 'aria-label') || hasProp(node.attributes, 'aria-labelledby') | |
// Check if SVG is decorative | |
const isDecorative = | |
hasProp(node.attributes, 'role', 'presentation') || hasProp(node.attributes, 'aria-hidden', 'true') | |
if (elementType === 'svg' && !hasAccessibleName && !isDecorative && !hasNestedTitleAsFirstChild) { | |
context.report({ | |
node, | |
message: | |
'`<svg>` must have an accessible name. Set `aria-label` or `aria-labelledby`, or nest a `<title>` element. However, if the `<svg>` is purely decorative, hide it with `aria-hidden="true"` or `role="presentation"`.', | |
}) | |
} | |
}, | |
} | |
}, | |
} |