no-test-prefixes.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. var _default = (0, _utils.createRule)({
  9. name: __filename,
  10. meta: {
  11. docs: {
  12. category: 'Best Practices',
  13. description: 'Use `.only` and `.skip` over `f` and `x`',
  14. recommended: 'error'
  15. },
  16. messages: {
  17. usePreferredName: 'Use "{{ preferredNodeName }}" instead'
  18. },
  19. fixable: 'code',
  20. schema: [],
  21. type: 'suggestion'
  22. },
  23. defaultOptions: [],
  24. create(context) {
  25. return {
  26. CallExpression(node) {
  27. const nodeName = (0, _utils.getNodeName)(node.callee);
  28. if (!nodeName || !(0, _utils.isDescribeCall)(node) && !(0, _utils.isTestCaseCall)(node)) return;
  29. const preferredNodeName = getPreferredNodeName(nodeName);
  30. if (!preferredNodeName) return;
  31. const funcNode = node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression ? node.callee.tag : node.callee.type === _experimentalUtils.AST_NODE_TYPES.CallExpression ? node.callee.callee : node.callee;
  32. context.report({
  33. messageId: 'usePreferredName',
  34. node: node.callee,
  35. data: {
  36. preferredNodeName
  37. },
  38. fix(fixer) {
  39. return [fixer.replaceText(funcNode, preferredNodeName)];
  40. }
  41. });
  42. }
  43. };
  44. }
  45. });
  46. exports.default = _default;
  47. function getPreferredNodeName(nodeName) {
  48. const firstChar = nodeName.charAt(0);
  49. const suffix = nodeName.endsWith('.each') ? '.each' : '';
  50. if (firstChar === 'f') {
  51. return `${nodeName.slice(1).replace('.each', '')}.only${suffix}`;
  52. }
  53. if (firstChar === 'x') {
  54. return `${nodeName.slice(1).replace('.each', '')}.skip${suffix}`;
  55. }
  56. return null;
  57. }