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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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-empty-callback';
  8. var WAIT_EXPRESSION_QUERY = 'CallExpression[callee.name=/^(waitFor|waitForElementToBeRemoved)$/]';
  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: "It's preferred to avoid empty callbacks in `waitFor` and `waitForElementToBeRemoved`",
  15. category: 'Best Practices',
  16. recommended: false,
  17. },
  18. messages: {
  19. noWaitForEmptyCallback: 'Avoid passing empty callback to `{{ methodName }}`. Insert an assertion instead.',
  20. },
  21. fixable: null,
  22. schema: [],
  23. },
  24. defaultOptions: [],
  25. create: function (context) {
  26. var _a;
  27. function reportIfEmpty(node) {
  28. if (node_utils_1.isBlockStatement(node.body) &&
  29. node.body.body.length === 0 &&
  30. node_utils_1.isCallExpression(node.parent) &&
  31. node_utils_1.isIdentifier(node.parent.callee)) {
  32. context.report({
  33. node: node,
  34. loc: node.body.loc.start,
  35. messageId: 'noWaitForEmptyCallback',
  36. data: {
  37. methodName: node.parent.callee.name,
  38. },
  39. });
  40. }
  41. }
  42. function reportNoop(node) {
  43. context.report({
  44. node: node,
  45. loc: node.loc.start,
  46. messageId: 'noWaitForEmptyCallback',
  47. });
  48. }
  49. return _a = {},
  50. _a[WAIT_EXPRESSION_QUERY + " > ArrowFunctionExpression"] = reportIfEmpty,
  51. _a[WAIT_EXPRESSION_QUERY + " > FunctionExpression"] = reportIfEmpty,
  52. _a[WAIT_EXPRESSION_QUERY + " > Identifier[name=\"noop\"]"] = reportNoop,
  53. _a;
  54. },
  55. });