|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { ESLintUtils, TSESTree } from "@typescript-eslint/utils"; |
| 5 | +import { hasTextContentChild } from "../util/hasTextContentChild"; |
| 6 | +import { hasNonEmptyProp } from "../util/hasNonEmptyProp"; |
| 7 | +import { hasAssociatedLabelViaAriaLabelledBy } from "../util/labelUtils"; |
| 8 | +import { elementType } from "jsx-ast-utils"; |
| 9 | +import { JSXOpeningElement } from "estree-jsx"; |
| 10 | + |
| 11 | +//------------------------------------------------------------------------------ |
| 12 | +// Rule Definition |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +const rule = ESLintUtils.RuleCreator.withoutDocs({ |
| 16 | + defaultOptions: [], |
| 17 | + meta: { |
| 18 | + type: "problem", |
| 19 | + docs: { |
| 20 | + description: |
| 21 | + "This rule aims to ensure that Tabs with icons but no text labels have an accessible name and that Tablist is properly labeled.", |
| 22 | + recommended: "strict", |
| 23 | + url: "https://www.w3.org/WAI/ARIA/apg/patterns/tabs/" // URL to the documentation page for this rule |
| 24 | + }, |
| 25 | + fixable: undefined, |
| 26 | + schema: [], |
| 27 | + messages: { |
| 28 | + missingTabLabel: "Accessibility: Tab elements must have an aria-label attribute is there is no visiable text content", |
| 29 | + missingTablistLabel: "Accessibility: Tablist must have an accessible label" |
| 30 | + } |
| 31 | + }, |
| 32 | + |
| 33 | + create(context) { |
| 34 | + return { |
| 35 | + // visitor functions for different types of nodes |
| 36 | + JSXOpeningElement(node: TSESTree.JSXOpeningElement) { |
| 37 | + const elementTypeValue = elementType(node as unknown as JSXOpeningElement); |
| 38 | + |
| 39 | + // if it is not a Tablist or Tab, return |
| 40 | + if (elementTypeValue !== "Tablist" && elementTypeValue !== "Tab") { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // Check for Tablist elements |
| 45 | + if (elementTypeValue === "Tablist") { |
| 46 | + if ( |
| 47 | + // if the Tablist has a label, if the Tablist has an associated label, return |
| 48 | + hasNonEmptyProp(node.attributes, "aria-label") || //aria-label |
| 49 | + hasAssociatedLabelViaAriaLabelledBy(node, context) // aria-labelledby |
| 50 | + ) { |
| 51 | + return; |
| 52 | + } |
| 53 | + context.report({ |
| 54 | + node, |
| 55 | + messageId: "missingTablistLabel" |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + // Check for Tab elements |
| 60 | + if (elementTypeValue === "Tab") { |
| 61 | + if ( |
| 62 | + hasTextContentChild(node.parent as unknown as TSESTree.JSXElement) || // text content |
| 63 | + hasNonEmptyProp(node.attributes, "aria-label") // aria-label |
| 64 | + ) { |
| 65 | + return; |
| 66 | + } |
| 67 | + context.report({ |
| 68 | + node, |
| 69 | + messageId: "missingTabLabel" |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +export default rule; |
0 commit comments