no-render-in-setup.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from) {
  3. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  4. to[j] = from[i];
  5. return to;
  6. };
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.findClosestBeforeHook = exports.RULE_NAME = void 0;
  9. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  10. var utils_1 = require("../utils");
  11. var node_utils_1 = require("../node-utils");
  12. exports.RULE_NAME = 'no-render-in-setup';
  13. function findClosestBeforeHook(node, testingFrameworkSetupHooksToFilter) {
  14. if (node === null)
  15. return null;
  16. if (node_utils_1.isCallExpression(node) &&
  17. node_utils_1.isIdentifier(node.callee) &&
  18. testingFrameworkSetupHooksToFilter.includes(node.callee.name)) {
  19. return node.callee;
  20. }
  21. return findClosestBeforeHook(node.parent, testingFrameworkSetupHooksToFilter);
  22. }
  23. exports.findClosestBeforeHook = findClosestBeforeHook;
  24. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  25. name: exports.RULE_NAME,
  26. meta: {
  27. type: 'problem',
  28. docs: {
  29. description: 'Disallow the use of `render` in setup functions',
  30. category: 'Best Practices',
  31. recommended: false,
  32. },
  33. messages: {
  34. noRenderInSetup: 'Move `render` out of `{{name}}` and into individual tests.',
  35. },
  36. fixable: null,
  37. schema: [
  38. {
  39. type: 'object',
  40. properties: {
  41. renderFunctions: {
  42. type: 'array',
  43. },
  44. allowTestingFrameworkSetupHook: {
  45. enum: utils_1.TESTING_FRAMEWORK_SETUP_HOOKS,
  46. },
  47. },
  48. anyOf: [
  49. {
  50. required: ['renderFunctions'],
  51. },
  52. {
  53. required: ['allowTestingFrameworkSetupHook'],
  54. },
  55. ],
  56. },
  57. ],
  58. },
  59. defaultOptions: [
  60. {
  61. renderFunctions: [],
  62. allowTestingFrameworkSetupHook: '',
  63. },
  64. ],
  65. create: function (context, _a) {
  66. var _b;
  67. var _c = _a[0], renderFunctions = _c.renderFunctions, allowTestingFrameworkSetupHook = _c.allowTestingFrameworkSetupHook;
  68. var renderImportedFromTestingLib = false;
  69. var wildcardImportName = null;
  70. return _b = {
  71. 'ImportDeclaration[source.value=/testing-library/] ImportNamespaceSpecifier': function (node) {
  72. wildcardImportName = node.local && node.local.name;
  73. },
  74. 'ImportDeclaration[source.value=/testing-library/]': function (node) {
  75. renderImportedFromTestingLib = node.specifiers.some(function (specifier) {
  76. return (node_utils_1.isImportSpecifier(specifier) && specifier.local.name === 'render');
  77. });
  78. }
  79. },
  80. _b["VariableDeclarator > CallExpression > Identifier[name=\"require\"]"] = function (node) {
  81. var callExpressionArgs = node.parent.arguments;
  82. var testingLibImport = callExpressionArgs.find(function (args) {
  83. return node_utils_1.isLiteral(args) &&
  84. typeof args.value === 'string' &&
  85. RegExp(/testing-library/, 'g').test(args.value);
  86. });
  87. if (!testingLibImport) {
  88. return;
  89. }
  90. var declaratorNode = node.parent
  91. .parent;
  92. renderImportedFromTestingLib =
  93. node_utils_1.isObjectPattern(declaratorNode.id) &&
  94. declaratorNode.id.properties.some(function (property) {
  95. return node_utils_1.isProperty(property) &&
  96. node_utils_1.isIdentifier(property.key) &&
  97. property.key.name === 'render';
  98. });
  99. },
  100. _b.CallExpression = function (node) {
  101. var testingFrameworkSetupHooksToFilter = utils_1.TESTING_FRAMEWORK_SETUP_HOOKS;
  102. if (allowTestingFrameworkSetupHook.length !== 0) {
  103. testingFrameworkSetupHooksToFilter = utils_1.TESTING_FRAMEWORK_SETUP_HOOKS.filter(function (hook) { return hook !== allowTestingFrameworkSetupHook; });
  104. }
  105. var beforeHook = findClosestBeforeHook(node, testingFrameworkSetupHooksToFilter);
  106. var disallowedRenderFns = renderImportedFromTestingLib || wildcardImportName
  107. ? __spreadArray(['render'], renderFunctions) : renderFunctions;
  108. if (node_utils_1.isRenderFunction(node, disallowedRenderFns) && beforeHook) {
  109. context.report({
  110. node: node,
  111. messageId: 'noRenderInSetup',
  112. data: {
  113. name: beforeHook.name,
  114. },
  115. });
  116. }
  117. },
  118. _b;
  119. },
  120. });