no-identical-title.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. const newDescribeContext = () => ({
  8. describeTitles: [],
  9. testTitles: []
  10. });
  11. var _default = (0, _utils.createRule)({
  12. name: __filename,
  13. meta: {
  14. docs: {
  15. category: 'Best Practices',
  16. description: 'Disallow identical titles',
  17. recommended: 'error'
  18. },
  19. messages: {
  20. multipleTestTitle: 'Test title is used multiple times in the same describe block.',
  21. multipleDescribeTitle: 'Describe block title is used multiple times in the same describe block.'
  22. },
  23. schema: [],
  24. type: 'suggestion'
  25. },
  26. defaultOptions: [],
  27. create(context) {
  28. const contexts = [newDescribeContext()];
  29. return {
  30. CallExpression(node) {
  31. var _getNodeName;
  32. const currentLayer = contexts[contexts.length - 1];
  33. if ((0, _utils.isDescribeCall)(node)) {
  34. contexts.push(newDescribeContext());
  35. }
  36. if ((_getNodeName = (0, _utils.getNodeName)(node.callee)) !== null && _getNodeName !== void 0 && _getNodeName.endsWith('.each')) {
  37. return;
  38. }
  39. const [argument] = node.arguments;
  40. if (!argument || !(0, _utils.isStringNode)(argument)) {
  41. return;
  42. }
  43. const title = (0, _utils.getStringValue)(argument);
  44. if ((0, _utils.isTestCaseCall)(node)) {
  45. if (currentLayer.testTitles.includes(title)) {
  46. context.report({
  47. messageId: 'multipleTestTitle',
  48. node: argument
  49. });
  50. }
  51. currentLayer.testTitles.push(title);
  52. }
  53. if (!(0, _utils.isDescribeCall)(node)) {
  54. return;
  55. }
  56. if (currentLayer.describeTitles.includes(title)) {
  57. context.report({
  58. messageId: 'multipleDescribeTitle',
  59. node: argument
  60. });
  61. }
  62. currentLayer.describeTitles.push(title);
  63. },
  64. 'CallExpression:exit'(node) {
  65. if ((0, _utils.isDescribeCall)(node)) {
  66. contexts.pop();
  67. }
  68. }
  69. };
  70. }
  71. });
  72. exports.default = _default;