Skip to content
Closed
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
5 changes: 3 additions & 2 deletions lib/rules/input-components-require-accessible-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { ESLintUtils, TSESTree } from "@typescript-eslint/utils";
import { elementType } from "jsx-ast-utils";
import { isInsideLabelTag, hasAssociatedLabelViaHtmlFor, hasAssociatedLabelViaAriaLabelledBy } from "../util/labelUtils";
import { isInsideLabelTag, hasAssociatedLabelViaHtmlFor, hasAssociatedLabelViaAriaLabelledBy, hasAriaLabel } from "../util/labelUtils";
import { hasFieldParent } from "../util/hasFieldParent";
import { applicableComponents } from "../applicableComponents/inputBasedComponents";
import { JSXOpeningElement } from "estree-jsx";
Expand Down Expand Up @@ -41,8 +41,9 @@ const rule = ESLintUtils.RuleCreator.withoutDocs({
return;
}

// wrapped in Label tag, labelled with htmlFor, labelled with aria-labelledby
// wrapped in Label tag, labelled with htmlFor, labelled with aria-labelledby, labelled with aria-label
if (
hasAriaLabel(node) ||
hasFieldParent(context) ||
isInsideLabelTag(context) ||
hasAssociatedLabelViaHtmlFor(node, context) ||
Expand Down
14 changes: 13 additions & 1 deletion lib/util/labelUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,24 @@ function hasAssociatedAriaText(openingElement, context, ariaAttribute) {
return hasAssociatedAriaText && hasHtmlId;
}

/**
* Determines if the element has a non-empty aria-label attribute.
* e.g.
* <Input aria-label="Sample label" />
* @param {*} openingElement
* @returns boolean for match found or not.
*/
function hasAriaLabel(openingElement) {
return hasNonEmptyProp(openingElement.attributes, "aria-label");
}

module.exports = {
isInsideLabelTag,
hasLabelWithHtmlForId,
hasLabelWithHtmlId,
hasAssociatedLabelViaAriaLabelledBy,
hasAssociatedLabelViaHtmlFor,
hasAssociatedLabelViaAriaDescribedby,
hasAssociatedAriaText
hasAssociatedAriaText,
hasAriaLabel
};
Loading