|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/** |
| 4 | + * @typedef {import("eslint").Rule.RuleModule} RuleModule |
| 5 | + * @typedef {import("estree").Node} ASTNode |
| 6 | + */ |
| 7 | + |
| 8 | +const { elementType, getProp, getLiteralPropValue } = require("jsx-ast-utils"); |
| 9 | +const includes = require("array-includes"); |
| 10 | +const { arraySchema, generateObjSchema } = require("eslint-plugin-jsx-a11y/lib/util/schemas"); |
| 11 | +const isDOMElement = require("eslint-plugin-jsx-a11y/lib/util/isDOMElement").default; |
| 12 | +const isHiddenFromScreenReader = require("eslint-plugin-jsx-a11y/lib/util/isHiddenFromScreenReader").default; |
| 13 | +const isInteractiveElement = require("eslint-plugin-jsx-a11y/lib/util/isInteractiveElement").default; |
| 14 | +const isInteractiveRole = require("eslint-plugin-jsx-a11y/lib/util/isInteractiveRole").default; |
| 15 | +const mayHaveAccessibleLabel = require("eslint-plugin-jsx-a11y/lib/util/mayHaveAccessibleLabel").default; |
| 16 | +const findAncestor = require("../utils/find-ancestor"); |
| 17 | + |
| 18 | +const errorMessages = { |
| 19 | + error: "A control must be associated with a text label." |
| 20 | +}; |
| 21 | + |
| 22 | +const ignoreList = ["link"]; |
| 23 | + |
| 24 | +const schema = generateObjSchema({ |
| 25 | + labelAttributes: arraySchema, |
| 26 | + controlComponents: arraySchema, |
| 27 | + ignoreElements: arraySchema, |
| 28 | + ignoreRoles: arraySchema, |
| 29 | + depth: { |
| 30 | + description: "JSX tree depth limit to check for accessible label", |
| 31 | + type: "integer", |
| 32 | + minimum: 0 |
| 33 | + }, |
| 34 | + ignoreAttributeInner: { type: "boolean" }, |
| 35 | + attributeTracingDepth: { type: "integer" } |
| 36 | +}); |
| 37 | + |
| 38 | +/** |
| 39 | + * @type RuleModule |
| 40 | + */ |
| 41 | +module.exports = { |
| 42 | + meta: { |
| 43 | + docs: { |
| 44 | + |
| 45 | + description: "Enforce that a control (an interactive element) has a text label", |
| 46 | + |
| 47 | + category: "Best Practices", |
| 48 | + |
| 49 | + recommended: false, |
| 50 | + url: "https://github.com/croutonn/eslint-plugin/blob/main/docs/rules/jsx-a11y-control-has-associated-label.md" |
| 51 | + }, |
| 52 | + |
| 53 | + fixable: null, |
| 54 | + messages: errorMessages, |
| 55 | + schema: [schema], |
| 56 | + |
| 57 | + type: "suggestion" |
| 58 | + }, |
| 59 | + |
| 60 | + create: context => { |
| 61 | + const options = context.options[0] || {}; |
| 62 | + const { |
| 63 | + labelAttributes = [], |
| 64 | + controlComponents = [], |
| 65 | + ignoreElements = [], |
| 66 | + ignoreRoles = [], |
| 67 | + ignoreAttributeInner = true, |
| 68 | + attributeTracingDepth = 4 |
| 69 | + } = options; |
| 70 | + |
| 71 | + const newIgnoreElements = new Set([...ignoreElements, ...ignoreList]); |
| 72 | + |
| 73 | + /** |
| 74 | + * Core function of the rule |
| 75 | + * @param {ASTNode} node target node |
| 76 | + * @returns {void} |
| 77 | + */ |
| 78 | + function rule(node) { |
| 79 | + const tag = elementType(node.openingElement); |
| 80 | + const role = getLiteralPropValue(getProp(node.openingElement.attributes, "role")); |
| 81 | + |
| 82 | + |
| 83 | + // Ignore interactive elements that might get their label from a source |
| 84 | + // that cannot be discerned from static analysis, like |
| 85 | + // <label><input />Save</label> |
| 86 | + if (newIgnoreElements.has(tag)) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Ignore roles that are "interactive" but should not require a label. |
| 91 | + if (includes(ignoreRoles, role)) { |
| 92 | + return; |
| 93 | + } |
| 94 | + const props = node.openingElement.attributes; |
| 95 | + const nodeIsDOMElement = isDOMElement(tag); |
| 96 | + const nodeIsHiddenFromScreenReader = isHiddenFromScreenReader(tag, props); |
| 97 | + const nodeIsInteractiveElement = isInteractiveElement(tag, props); |
| 98 | + const nodeIsInteractiveRole = isInteractiveRole(tag, props); |
| 99 | + const nodeIsControlComponent = controlComponents.indexOf(tag) > -1; |
| 100 | + |
| 101 | + if (nodeIsHiddenFromScreenReader) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + let hasAccessibleLabel = true; |
| 106 | + |
| 107 | + if ( |
| 108 | + nodeIsInteractiveElement || |
| 109 | + ( |
| 110 | + nodeIsDOMElement && |
| 111 | + nodeIsInteractiveRole |
| 112 | + ) || |
| 113 | + nodeIsControlComponent |
| 114 | + |
| 115 | + ) { |
| 116 | + |
| 117 | + // Prevent crazy recursion. |
| 118 | + const recursionDepth = Math.min( |
| 119 | + options.depth === void 0 ? 2 : options.depth, |
| 120 | + 25 |
| 121 | + ); |
| 122 | + |
| 123 | + hasAccessibleLabel = mayHaveAccessibleLabel( |
| 124 | + node, |
| 125 | + recursionDepth, |
| 126 | + labelAttributes |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + if (ignoreAttributeInner && findAncestor(node, (parent => parent.type === "JSXAttribute"), attributeTracingDepth)) { |
| 131 | + |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + if (!hasAccessibleLabel) { |
| 136 | + context.report({ |
| 137 | + node: node.openingElement, |
| 138 | + messageId: "error" |
| 139 | + }); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Create visitor selectors. |
| 144 | + return { |
| 145 | + JSXElement: rule |
| 146 | + }; |
| 147 | + } |
| 148 | +}; |
0 commit comments