control-has-associated-label.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  4. var _jsxAstUtils = require("jsx-ast-utils");
  5. var _arrayIncludes = _interopRequireDefault(require("array-includes"));
  6. var _schemas = require("../util/schemas");
  7. var _isDOMElement = _interopRequireDefault(require("../util/isDOMElement"));
  8. var _isHiddenFromScreenReader = _interopRequireDefault(require("../util/isHiddenFromScreenReader"));
  9. var _isInteractiveElement = _interopRequireDefault(require("../util/isInteractiveElement"));
  10. var _isInteractiveRole = _interopRequireDefault(require("../util/isInteractiveRole"));
  11. var _mayHaveAccessibleLabel = _interopRequireDefault(require("../util/mayHaveAccessibleLabel"));
  12. /**
  13. * @fileoverview Enforce controls are associated with a text label.
  14. * @author Jesse Beach
  15. *
  16. *
  17. */
  18. // ----------------------------------------------------------------------------
  19. // Rule Definition
  20. // ----------------------------------------------------------------------------
  21. var errorMessage = 'A control must be associated with a text label.';
  22. var ignoreList = ['link'];
  23. var schema = (0, _schemas.generateObjSchema)({
  24. labelAttributes: _schemas.arraySchema,
  25. controlComponents: _schemas.arraySchema,
  26. ignoreElements: _schemas.arraySchema,
  27. ignoreRoles: _schemas.arraySchema,
  28. depth: {
  29. description: 'JSX tree depth limit to check for accessible label',
  30. type: 'integer',
  31. minimum: 0
  32. }
  33. });
  34. module.exports = {
  35. meta: {
  36. docs: {},
  37. schema: [schema]
  38. },
  39. create: function create(context) {
  40. var options = context.options[0] || {};
  41. var _options$labelAttribu = options.labelAttributes,
  42. labelAttributes = _options$labelAttribu === void 0 ? [] : _options$labelAttribu,
  43. _options$controlCompo = options.controlComponents,
  44. controlComponents = _options$controlCompo === void 0 ? [] : _options$controlCompo,
  45. _options$ignoreElemen = options.ignoreElements,
  46. ignoreElements = _options$ignoreElemen === void 0 ? [] : _options$ignoreElemen,
  47. _options$ignoreRoles = options.ignoreRoles,
  48. ignoreRoles = _options$ignoreRoles === void 0 ? [] : _options$ignoreRoles;
  49. var newIgnoreElements = new Set([].concat((0, _toConsumableArray2["default"])(ignoreElements), ignoreList));
  50. var rule = function rule(node) {
  51. var tag = (0, _jsxAstUtils.elementType)(node.openingElement);
  52. var role = (0, _jsxAstUtils.getLiteralPropValue)((0, _jsxAstUtils.getProp)(node.openingElement.attributes, 'role')); // Ignore interactive elements that might get their label from a source
  53. // that cannot be discerned from static analysis, like
  54. // <label><input />Save</label>
  55. if (newIgnoreElements.has(tag)) {
  56. return;
  57. } // Ignore roles that are "interactive" but should not require a label.
  58. if ((0, _arrayIncludes["default"])(ignoreRoles, role)) {
  59. return;
  60. }
  61. var props = node.openingElement.attributes;
  62. var nodeIsDOMElement = (0, _isDOMElement["default"])(tag);
  63. var nodeIsHiddenFromScreenReader = (0, _isHiddenFromScreenReader["default"])(tag, props);
  64. var nodeIsInteractiveElement = (0, _isInteractiveElement["default"])(tag, props);
  65. var nodeIsInteractiveRole = (0, _isInteractiveRole["default"])(tag, props);
  66. var nodeIsControlComponent = controlComponents.indexOf(tag) > -1;
  67. if (nodeIsHiddenFromScreenReader) {
  68. return;
  69. }
  70. var hasAccessibleLabel = true;
  71. if (nodeIsInteractiveElement || nodeIsDOMElement && nodeIsInteractiveRole || nodeIsControlComponent) {
  72. // Prevent crazy recursion.
  73. var recursionDepth = Math.min(options.depth === undefined ? 2 : options.depth, 25);
  74. hasAccessibleLabel = (0, _mayHaveAccessibleLabel["default"])(node, recursionDepth, labelAttributes);
  75. }
  76. if (!hasAccessibleLabel) {
  77. context.report({
  78. node: node.openingElement,
  79. message: errorMessage
  80. });
  81. }
  82. }; // Create visitor selectors.
  83. return {
  84. JSXElement: rule
  85. };
  86. }
  87. };