scope.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var _ariaQuery = require("aria-query");
  3. var _jsxAstUtils = require("jsx-ast-utils");
  4. var _schemas = require("../util/schemas");
  5. /**
  6. * @fileoverview Enforce scope prop is only used on <th> elements.
  7. * @author Ethan Cohen
  8. */
  9. // ----------------------------------------------------------------------------
  10. // Rule Definition
  11. // ----------------------------------------------------------------------------
  12. var errorMessage = 'The scope prop can only be used on <th> elements.';
  13. var schema = (0, _schemas.generateObjSchema)();
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/scope.md'
  18. },
  19. schema: [schema]
  20. },
  21. create: function create(context) {
  22. return {
  23. JSXAttribute: function JSXAttribute(node) {
  24. var name = (0, _jsxAstUtils.propName)(node);
  25. if (name && name.toUpperCase() !== 'SCOPE') {
  26. return;
  27. }
  28. var parent = node.parent;
  29. var tagName = (0, _jsxAstUtils.elementType)(parent); // Do not test higher level JSX components, as we do not know what
  30. // low-level DOM element this maps to.
  31. if (!_ariaQuery.dom.has(tagName)) {
  32. return;
  33. }
  34. if (tagName && tagName.toUpperCase() === 'TH') {
  35. return;
  36. }
  37. context.report({
  38. node,
  39. message: errorMessage
  40. });
  41. }
  42. };
  43. }
  44. };