Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

77 lignes
2.2 KiB

  1. 'use strict';
  2. var _jsxAstUtils = require('jsx-ast-utils');
  3. var _schemas = require('../util/schemas');
  4. var _ISO = require('../util/attributes/ISO.json');
  5. var _ISO2 = _interopRequireDefault(_ISO);
  6. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7. var errorMessage = 'lang attribute must have a valid value.'; /**
  8. * @fileoverview Enforce lang attribute has a valid value.
  9. * @author Ethan Cohen
  10. */
  11. // ----------------------------------------------------------------------------
  12. // Rule Definition
  13. // ----------------------------------------------------------------------------
  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);
  35. // Don't check identifiers
  36. if (value === null) {
  37. return;
  38. }
  39. if (value === undefined) {
  40. context.report({
  41. node: node,
  42. message: errorMessage
  43. });
  44. return;
  45. }
  46. var hyphen = value.indexOf('-');
  47. var lang = hyphen > -1 ? value.substring(0, hyphen) : value;
  48. var country = hyphen > -1 ? value.substring(3) : undefined;
  49. if (_ISO2.default.languages.indexOf(lang) > -1 && (country === undefined || _ISO2.default.countries.indexOf(country) > -1)) {
  50. return;
  51. }
  52. context.report({
  53. node: node,
  54. message: errorMessage
  55. });
  56. }
  57. };
  58. }
  59. };