|
| 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 { JSXOpeningElement, JSXAttribute } from "estree-jsx"; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Utility Functions |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +/** |
| 13 | + * Checks if a value is a non-empty string (same logic as hasNonEmptyProp for strings) |
| 14 | + */ |
| 15 | +const isNonEmptyString = (value: any): boolean => { |
| 16 | + return typeof value === "string" && value.trim().length > 0; |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * Checks if an object has a non-empty string property |
| 21 | + */ |
| 22 | +const hasNonEmptyObjectProperty = (obj: any, propertyName: string): boolean => { |
| 23 | + if (!obj || typeof obj !== "object") return false; |
| 24 | + return isNonEmptyString(obj[propertyName]); |
| 25 | +}; |
| 26 | + |
| 27 | +//------------------------------------------------------------------------------ |
| 28 | +// Rule Definition |
| 29 | +//------------------------------------------------------------------------------ |
| 30 | +const rule = ESLintUtils.RuleCreator.withoutDocs({ |
| 31 | + defaultOptions: [], |
| 32 | + meta: { |
| 33 | + type: "problem", |
| 34 | + docs: { |
| 35 | + description: |
| 36 | + "This rule aims to ensure that dismissible Tag components have an aria-label on the dismiss button", |
| 37 | + recommended: false |
| 38 | + }, |
| 39 | + fixable: undefined, |
| 40 | + schema: [], |
| 41 | + messages: { |
| 42 | + missingDismissLabel: "Accessibility: Dismissible Tag must have dismissIcon with aria-label" |
| 43 | + }, |
| 44 | + }, |
| 45 | + create(context) { |
| 46 | + return { |
| 47 | + // visitor functions for different types of nodes |
| 48 | + JSXElement(node: TSESTree.JSXElement) { |
| 49 | + const openingElement = node.openingElement; |
| 50 | + |
| 51 | + // if it is not a Tag, return |
| 52 | + if (elementType(openingElement as JSXOpeningElement) !== "Tag") { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Check if Tag has dismissible prop |
| 57 | + const isDismissible = hasProp(openingElement.attributes as JSXAttribute[], "dismissible"); |
| 58 | + if (!isDismissible) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Check if dismissible Tag has dismissIcon with aria-label |
| 63 | + const dismissIconProp = getProp(openingElement.attributes as JSXAttribute[], "dismissIcon"); |
| 64 | + |
| 65 | + if (!dismissIconProp) { |
| 66 | + context.report({ |
| 67 | + node, |
| 68 | + messageId: `missingDismissLabel` |
| 69 | + }); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Get the dismissIcon value and check if it has valid aria-label |
| 74 | + const dismissIconValue = getPropValue(dismissIconProp); |
| 75 | + |
| 76 | + if (!hasNonEmptyObjectProperty(dismissIconValue, "aria-label")) { |
| 77 | + context.report({ |
| 78 | + node, |
| 79 | + messageId: `missingDismissLabel` |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + }; |
| 84 | + } |
| 85 | +}); |
| 86 | + |
| 87 | +export default rule; |
0 commit comments