no-wait-for-empty-callback.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. const utils_1 = require("@typescript-eslint/utils");
  5. const create_testing_library_rule_1 = require("../create-testing-library-rule");
  6. const node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'no-wait-for-empty-callback';
  8. exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
  9. name: exports.RULE_NAME,
  10. meta: {
  11. type: 'suggestion',
  12. docs: {
  13. description: 'Disallow empty callbacks for `waitFor` and `waitForElementToBeRemoved`',
  14. recommendedConfig: {
  15. dom: 'error',
  16. angular: 'error',
  17. react: 'error',
  18. vue: 'error',
  19. marko: 'error',
  20. },
  21. },
  22. messages: {
  23. noWaitForEmptyCallback: 'Avoid passing empty callback to `{{ methodName }}`. Insert an assertion instead.',
  24. },
  25. schema: [],
  26. },
  27. defaultOptions: [],
  28. create(context, _, helpers) {
  29. function isValidWaitFor(node) {
  30. const parentCallExpression = node.parent;
  31. const parentIdentifier = (0, node_utils_1.getPropertyIdentifierNode)(parentCallExpression);
  32. if (!parentIdentifier) {
  33. return false;
  34. }
  35. return helpers.isAsyncUtil(parentIdentifier, [
  36. 'waitFor',
  37. 'waitForElementToBeRemoved',
  38. ]);
  39. }
  40. function reportIfEmpty(node) {
  41. if (!isValidWaitFor(node)) {
  42. return;
  43. }
  44. if ((0, node_utils_1.isEmptyFunction)(node) &&
  45. (0, node_utils_1.isCallExpression)(node.parent) &&
  46. utils_1.ASTUtils.isIdentifier(node.parent.callee)) {
  47. context.report({
  48. node,
  49. loc: node.body.loc.start,
  50. messageId: 'noWaitForEmptyCallback',
  51. data: {
  52. methodName: node.parent.callee.name,
  53. },
  54. });
  55. }
  56. }
  57. function reportNoop(node) {
  58. if (!isValidWaitFor(node)) {
  59. return;
  60. }
  61. context.report({
  62. node,
  63. loc: node.loc.start,
  64. messageId: 'noWaitForEmptyCallback',
  65. data: {
  66. methodName: (0, node_utils_1.isCallExpression)(node.parent) &&
  67. utils_1.ASTUtils.isIdentifier(node.parent.callee) &&
  68. node.parent.callee.name,
  69. },
  70. });
  71. }
  72. return {
  73. 'CallExpression > ArrowFunctionExpression': reportIfEmpty,
  74. 'CallExpression > FunctionExpression': reportIfEmpty,
  75. 'CallExpression > Identifier[name="noop"]': reportNoop,
  76. };
  77. },
  78. });