no-wait-for-snapshot.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  5. var utils_1 = require("../utils");
  6. var node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'no-wait-for-snapshot';
  8. var ASYNC_UTILS_REGEXP = new RegExp("^(" + utils_1.ASYNC_UTILS.join('|') + ")$");
  9. var SNAPSHOT_REGEXP = /^(toMatchSnapshot|toMatchInlineSnapshot)$/;
  10. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  11. name: exports.RULE_NAME,
  12. meta: {
  13. type: 'problem',
  14. docs: {
  15. description: 'Ensures no snapshot is generated inside of a `waitFor` call',
  16. category: 'Best Practices',
  17. recommended: 'warn',
  18. },
  19. messages: {
  20. noWaitForSnapshot: "A snapshot can't be generated inside of a `{{ name }}` call",
  21. },
  22. fixable: null,
  23. schema: [],
  24. },
  25. defaultOptions: [],
  26. create: function (context) {
  27. var _a;
  28. var asyncUtilsUsage = [];
  29. var importedAsyncUtils = [];
  30. var snapshotUsage = [];
  31. return _a = {
  32. 'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier': function (node) {
  33. var parent = node.parent;
  34. if (!utils_1.LIBRARY_MODULES.includes(parent.source.value.toString()))
  35. return;
  36. var name;
  37. if (node.type === 'ImportSpecifier') {
  38. name = node.imported.name;
  39. }
  40. if (node.type === 'ImportNamespaceSpecifier') {
  41. name = node.local.name;
  42. }
  43. importedAsyncUtils.push(name);
  44. }
  45. },
  46. _a["CallExpression > Identifier[name=" + ASYNC_UTILS_REGEXP + "]"] = function (node) {
  47. asyncUtilsUsage.push({ node: node, name: node.name });
  48. },
  49. _a["CallExpression > MemberExpression > Identifier[name=" + ASYNC_UTILS_REGEXP + "]"] = function (node) {
  50. var memberExpression = node.parent;
  51. var identifier = memberExpression.object;
  52. var memberExpressionName = identifier.name;
  53. asyncUtilsUsage.push({
  54. node: memberExpression,
  55. name: memberExpressionName,
  56. });
  57. },
  58. _a["Identifier[name=" + SNAPSHOT_REGEXP + "]"] = function (node) {
  59. snapshotUsage.push(node);
  60. },
  61. _a['Program:exit'] = function () {
  62. var testingLibraryUtilUsage = asyncUtilsUsage.filter(function (usage) {
  63. if (node_utils_1.isMemberExpression(usage.node)) {
  64. var object = usage.node.object;
  65. return importedAsyncUtils.includes(object.name);
  66. }
  67. return importedAsyncUtils.includes(usage.name);
  68. });
  69. function getClosestAsyncUtil(asyncUtilUsage, node) {
  70. var callExpression = node_utils_1.findClosestCallExpressionNode(node);
  71. while (callExpression != null) {
  72. if (callExpression.callee === asyncUtilUsage.node)
  73. return asyncUtilUsage;
  74. callExpression = node_utils_1.findClosestCallExpressionNode(callExpression.parent);
  75. }
  76. return null;
  77. }
  78. snapshotUsage.forEach(function (node) {
  79. testingLibraryUtilUsage.forEach(function (asyncUtilUsage) {
  80. var closestAsyncUtil = getClosestAsyncUtil(asyncUtilUsage, node);
  81. if (closestAsyncUtil != null) {
  82. var name_1;
  83. if (node_utils_1.isMemberExpression(closestAsyncUtil.node)) {
  84. name_1 = closestAsyncUtil.node.property
  85. .name;
  86. }
  87. else {
  88. name_1 = closestAsyncUtil.name;
  89. }
  90. context.report({
  91. node: node,
  92. messageId: 'noWaitForSnapshot',
  93. data: { name: name_1 },
  94. });
  95. }
  96. });
  97. });
  98. },
  99. _a;
  100. },
  101. });