consistent-data-testid.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. var utils_1 = require("../utils");
  5. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  6. var node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'consistent-data-testid';
  8. var FILENAME_PLACEHOLDER = '{fileName}';
  9. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  10. name: exports.RULE_NAME,
  11. meta: {
  12. type: 'suggestion',
  13. docs: {
  14. description: 'Ensures consistent usage of `data-testid`',
  15. category: 'Best Practices',
  16. recommended: false,
  17. },
  18. messages: {
  19. invalidTestId: '`{{attr}}` "{{value}}" should match `{{regex}}`',
  20. },
  21. fixable: null,
  22. schema: [
  23. {
  24. type: 'object',
  25. default: {},
  26. additionalProperties: false,
  27. required: ['testIdPattern'],
  28. properties: {
  29. testIdPattern: {
  30. type: 'string',
  31. },
  32. testIdAttribute: {
  33. default: 'data-testid',
  34. oneOf: [
  35. {
  36. type: 'string',
  37. },
  38. {
  39. type: 'array',
  40. items: {
  41. type: 'string',
  42. },
  43. },
  44. ],
  45. },
  46. },
  47. },
  48. ],
  49. },
  50. defaultOptions: [
  51. {
  52. testIdPattern: '',
  53. testIdAttribute: 'data-testid',
  54. },
  55. ],
  56. create: function (context, _a) {
  57. var _b;
  58. var options = _a[0];
  59. var getFilename = context.getFilename;
  60. var testIdPattern = options.testIdPattern, attr = options.testIdAttribute;
  61. function getFileNameData() {
  62. var splitPath = getFilename().split('/');
  63. var fileNameWithExtension = splitPath.pop();
  64. var parent = splitPath.pop();
  65. var fileName = fileNameWithExtension.split('.').shift();
  66. return {
  67. fileName: fileName === 'index' ? parent : fileName,
  68. };
  69. }
  70. function getTestIdValidator(fileName) {
  71. return new RegExp(testIdPattern.replace(FILENAME_PLACEHOLDER, fileName));
  72. }
  73. function isTestIdAttribute(name) {
  74. if (typeof attr === 'string') {
  75. return attr === name;
  76. }
  77. else {
  78. return attr.includes(name);
  79. }
  80. }
  81. return _b = {},
  82. _b["JSXIdentifier"] = function (node) {
  83. if (!node_utils_1.isJSXAttribute(node.parent) ||
  84. !node_utils_1.isLiteral(node.parent.value) ||
  85. !isTestIdAttribute(node.name)) {
  86. return;
  87. }
  88. var value = node.parent.value.value;
  89. var fileName = getFileNameData().fileName;
  90. var regex = getTestIdValidator(fileName);
  91. if (value && typeof value === 'string' && !regex.test(value)) {
  92. context.report({
  93. node: node,
  94. messageId: 'invalidTestId',
  95. data: {
  96. attr: node.name,
  97. value: value,
  98. regex: regex,
  99. },
  100. });
  101. }
  102. },
  103. _b;
  104. },
  105. });