no-focused-tests.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. const findOnlyNode = node => {
  9. const callee = node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression ? node.callee.tag : node.callee.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? node.callee.callee : node.callee;
  10. if (callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression) {
  11. if (callee.object.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression) {
  12. if ((0, _utils.isSupportedAccessor)(callee.object.property, 'only')) {
  13. return callee.object.property;
  14. }
  15. }
  16. if ((0, _utils.isSupportedAccessor)(callee.property, 'only')) {
  17. return callee.property;
  18. }
  19. }
  20. return null;
  21. };
  22. var _default = (0, _utils.createRule)({
  23. name: __filename,
  24. meta: {
  25. docs: {
  26. category: 'Best Practices',
  27. description: 'Disallow focused tests',
  28. recommended: 'error',
  29. suggestion: true
  30. },
  31. messages: {
  32. focusedTest: 'Unexpected focused test.',
  33. suggestRemoveFocus: 'Remove focus from test.'
  34. },
  35. schema: [],
  36. type: 'suggestion'
  37. },
  38. defaultOptions: [],
  39. create: context => ({
  40. CallExpression(node) {
  41. if (!(0, _utils.isDescribeCall)(node) && !(0, _utils.isTestCaseCall)(node)) {
  42. return;
  43. }
  44. if ((0, _utils.getNodeName)(node).startsWith('f')) {
  45. context.report({
  46. messageId: 'focusedTest',
  47. node,
  48. suggest: [{
  49. messageId: 'suggestRemoveFocus',
  50. fix: fixer => fixer.removeRange([node.range[0], node.range[0] + 1])
  51. }]
  52. });
  53. return;
  54. }
  55. const onlyNode = findOnlyNode(node);
  56. if (!onlyNode) {
  57. return;
  58. }
  59. context.report({
  60. messageId: 'focusedTest',
  61. node: onlyNode,
  62. suggest: [{
  63. messageId: 'suggestRemoveFocus',
  64. fix: fixer => fixer.removeRange([onlyNode.range[0] - 1, onlyNode.range[1] + Number(onlyNode.type !== _experimentalUtils.AST_NODE_TYPES.Identifier)])
  65. }]
  66. });
  67. }
  68. })
  69. });
  70. exports.default = _default;