lang.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _jsxAstUtils = require("jsx-ast-utils");
  4. var _languageTags = _interopRequireDefault(require("language-tags"));
  5. var _schemas = require("../util/schemas");
  6. /**
  7. * @fileoverview Enforce lang attribute has a valid value.
  8. * @author Ethan Cohen
  9. */
  10. // ----------------------------------------------------------------------------
  11. // Rule Definition
  12. // ----------------------------------------------------------------------------
  13. var errorMessage = 'lang attribute must have a valid value.';
  14. var schema = (0, _schemas.generateObjSchema)();
  15. module.exports = {
  16. meta: {
  17. docs: {
  18. url: 'https://github.com/evcohen/eslint-plugin-jsx-a11y/tree/master/docs/rules/lang.md'
  19. },
  20. schema: [schema]
  21. },
  22. create: function create(context) {
  23. return {
  24. JSXAttribute: function JSXAttribute(node) {
  25. var name = (0, _jsxAstUtils.propName)(node);
  26. if (name && name.toUpperCase() !== 'LANG') {
  27. return;
  28. }
  29. var parent = node.parent;
  30. var type = (0, _jsxAstUtils.elementType)(parent);
  31. if (type && type !== 'html') {
  32. return;
  33. }
  34. var value = (0, _jsxAstUtils.getLiteralPropValue)(node); // Don't check identifiers
  35. if (value === null) {
  36. return;
  37. }
  38. if (value === undefined) {
  39. context.report({
  40. node,
  41. message: errorMessage
  42. });
  43. return;
  44. }
  45. if (_languageTags["default"].check(value)) {
  46. return;
  47. }
  48. context.report({
  49. node,
  50. message: errorMessage
  51. });
  52. }
  53. };
  54. }
  55. };