|
| 1 | +/** |
| 2 | + * @fileoverview Requires accessible prop when accessibility props |
| 3 | + * @author Paul Briand |
| 4 | + */ |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +const isAccessible = require("../utils/isAccessible"); |
| 8 | +const hasPropAccessible = require("../utils/hasAccessibleProp"); |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +//------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 14 | +module.exports = { |
| 15 | + meta: { |
| 16 | + type: "problem", // `problem`, `suggestion`, or `layout` |
| 17 | + docs: { |
| 18 | + description: |
| 19 | + "Requires accessible prop when accessibility props are defined", |
| 20 | + recommended: false, |
| 21 | + url: "https://github.com/bamlab/react-native-project-config/tree/main/packages/eslint-plugin/docs/rules/accessibility-props-require-accessible.md", // URL to the documentation page for this rule |
| 22 | + }, |
| 23 | + fixable: "code", // Or `code` or `whitespace` |
| 24 | + schema: [], // Add a schema if the rule has options |
| 25 | + messages: { |
| 26 | + roleRequiresAccessible: |
| 27 | + "Requires accessible prop when role prop is defined", |
| 28 | + labelRequiresAccessible: |
| 29 | + "Requires accessible prop when label prop is defined", |
| 30 | + }, |
| 31 | + }, |
| 32 | + |
| 33 | + create(context) { |
| 34 | + return { |
| 35 | + JSXOpeningElement(node) { |
| 36 | + if (!isAccessible(node)) { |
| 37 | + if ( |
| 38 | + node.attributes.some((attribute) => |
| 39 | + ["role", "accessibilityRole"].includes(attribute.name.name) |
| 40 | + ) |
| 41 | + ) { |
| 42 | + if (!hasPropAccessible(node)) { |
| 43 | + context.report({ |
| 44 | + node, |
| 45 | + messageId: "roleRequiresAccessible", |
| 46 | + fix: (fixer) => { |
| 47 | + const openingTagEnd = node.range[1]; |
| 48 | + return fixer.insertTextBeforeRange( |
| 49 | + [openingTagEnd - (node.selfClosing ? 2 : 1), openingTagEnd], |
| 50 | + " accessible" |
| 51 | + ); |
| 52 | + }, |
| 53 | + }); |
| 54 | + } else { |
| 55 | + context.report({ |
| 56 | + node, |
| 57 | + messageId: "roleRequiresAccessible", |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (!isAnyParentAccessible(node)) { |
| 63 | + if ( |
| 64 | + node.attributes.some((attribute) => |
| 65 | + [ |
| 66 | + "accessibilityLabel", |
| 67 | + "accessibilityHint", |
| 68 | + "accessibilityLabelledBy", |
| 69 | + "aria-label", |
| 70 | + "aria-labelledby", |
| 71 | + ].includes(attribute.name.name) |
| 72 | + ) |
| 73 | + ) { |
| 74 | + if (!hasPropAccessible(node)) { |
| 75 | + context.report({ |
| 76 | + node, |
| 77 | + messageId: "labelRequiresAccessible", |
| 78 | + fix: (fixer) => { |
| 79 | + const openingTagEnd = node.range[1]; |
| 80 | + return fixer.insertTextBeforeRange( |
| 81 | + [ |
| 82 | + openingTagEnd - (node.selfClosing ? 2 : 1), |
| 83 | + openingTagEnd, |
| 84 | + ], |
| 85 | + " accessible" |
| 86 | + ); |
| 87 | + }, |
| 88 | + }); |
| 89 | + } else { |
| 90 | + context.report({ |
| 91 | + node, |
| 92 | + messageId: "labelRequiresAccessible", |
| 93 | + }); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + }; |
| 100 | + }, |
| 101 | +}; |
| 102 | + |
| 103 | +const isAnyParentAccessible = (node) => { |
| 104 | + /* function applied to a JSXOpeningElement, its parent is the JSXElement. |
| 105 | + We have to look for the parent of the parent */ |
| 106 | + if (node.parent.parent && node.parent.parent.type === "JSXElement") { |
| 107 | + if (isAccessible(node.parent.parent.openingElement)) return true; |
| 108 | + |
| 109 | + return isAnyParentAccessible(node.parent.parent.openingElement); |
| 110 | + } |
| 111 | +}; |
0 commit comments