|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { ESLintUtils, TSESTree } from "@typescript-eslint/utils"; |
| 5 | +import { elementType, hasProp, getProp, getPropValue } from "jsx-ast-utils"; |
| 6 | +import { hasNonEmptyProp } from "../util/hasNonEmptyProp"; |
| 7 | +import { JSXOpeningElement, JSXAttribute } from "estree-jsx"; |
| 8 | + |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | +// Utility Functions |
| 11 | +//------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +/** |
| 14 | + * Checks if a value is a non-empty string |
| 15 | + */ |
| 16 | +const isNonEmptyString = (value: any): boolean => { |
| 17 | + return typeof value === "string" && value.trim().length > 0; |
| 18 | +}; |
| 19 | + |
| 20 | +//------------------------------------------------------------------------------ |
| 21 | +// Rule Definition |
| 22 | +//------------------------------------------------------------------------------ |
| 23 | +const rule = ESLintUtils.RuleCreator.withoutDocs({ |
| 24 | + defaultOptions: [], |
| 25 | + meta: { |
| 26 | + type: "problem", |
| 27 | + docs: { |
| 28 | + description: |
| 29 | + "This rule aims to ensure that dismissible Tag components have proper accessibility labelling: either aria-label on dismissIcon or aria-label on Tag with role on dismissIcon", |
| 30 | + recommended: "strict", |
| 31 | + url: "https://react.fluentui.dev/?path=/docs/components-tag-tag--docs" |
| 32 | + }, |
| 33 | + fixable: undefined, |
| 34 | + schema: [], |
| 35 | + messages: { |
| 36 | + missingDismissLabel: |
| 37 | + "Accessibility: Dismissible Tag must have either aria-label on dismissIcon or aria-label on Tag with role on dismissIcon" |
| 38 | + } |
| 39 | + }, |
| 40 | + create(context) { |
| 41 | + return { |
| 42 | + // visitor functions for different types of nodes |
| 43 | + JSXElement(node: TSESTree.JSXElement) { |
| 44 | + const openingElement = node.openingElement; |
| 45 | + |
| 46 | + // if it is not a Tag, return |
| 47 | + if (elementType(openingElement as JSXOpeningElement) !== "Tag") { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Check if Tag has dismissible prop |
| 52 | + const isDismissible = hasProp(openingElement.attributes as JSXAttribute[], "dismissible"); |
| 53 | + if (!isDismissible) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Check if dismissible Tag has proper accessibility labelling |
| 58 | + const dismissIconProp = getProp(openingElement.attributes as JSXAttribute[], "dismissIcon"); |
| 59 | + if (!dismissIconProp) { |
| 60 | + context.report({ |
| 61 | + node, |
| 62 | + messageId: `missingDismissLabel` |
| 63 | + }); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const dismissIconValue = getPropValue(dismissIconProp); |
| 68 | + |
| 69 | + // Check if dismissIcon has aria-label |
| 70 | + const dismissIconHasAriaLabel = |
| 71 | + dismissIconValue && typeof dismissIconValue === "object" && isNonEmptyString((dismissIconValue as any)["aria-label"]); |
| 72 | + |
| 73 | + // Check if dismissIcon has role |
| 74 | + const dismissIconHasRole = |
| 75 | + dismissIconValue && typeof dismissIconValue === "object" && isNonEmptyString((dismissIconValue as any)["role"]); |
| 76 | + |
| 77 | + // Check if Tag has aria-label (required when dismissIcon has role) |
| 78 | + const tagHasAriaLabel = hasNonEmptyProp(openingElement.attributes, "aria-label"); |
| 79 | + // Valid patterns: |
| 80 | + // Option 1: dismissIcon has aria-label |
| 81 | + // Option 2: Tag has aria-label and dismissIcon has role |
| 82 | + const hasValidLabelling = dismissIconHasAriaLabel || (tagHasAriaLabel && dismissIconHasRole); |
| 83 | + if (!hasValidLabelling) { |
| 84 | + context.report({ |
| 85 | + node, |
| 86 | + messageId: `missingDismissLabel` |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + } |
| 92 | +}); |
| 93 | + |
| 94 | +export default rule; |
0 commit comments