no-if.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 testCaseNames = new Set([...Object.keys(_utils.TestCaseName), 'it.only', 'it.concurrent.only', 'it.skip', 'it.concurrent.skip', 'test.only', 'test.concurrent.only', 'test.skip', 'test.concurrent.skip', 'fit.concurrent']);
  9. const isTestFunctionExpression = node => node.parent !== undefined && node.parent.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && testCaseNames.has((0, _utils.getNodeName)(node.parent.callee));
  10. const conditionName = {
  11. [_experimentalUtils.AST_NODE_TYPES.ConditionalExpression]: 'conditional',
  12. [_experimentalUtils.AST_NODE_TYPES.SwitchStatement]: 'switch',
  13. [_experimentalUtils.AST_NODE_TYPES.IfStatement]: 'if'
  14. };
  15. var _default = (0, _utils.createRule)({
  16. name: __filename,
  17. meta: {
  18. docs: {
  19. description: 'Disallow conditional logic',
  20. category: 'Best Practices',
  21. recommended: false
  22. },
  23. messages: {
  24. conditionalInTest: 'Test should not contain {{ condition }} statements.'
  25. },
  26. schema: [],
  27. type: 'suggestion'
  28. },
  29. defaultOptions: [],
  30. create(context) {
  31. const stack = [];
  32. function validate(node) {
  33. const lastElementInStack = stack[stack.length - 1];
  34. if (stack.length === 0 || !lastElementInStack) {
  35. return;
  36. }
  37. context.report({
  38. data: {
  39. condition: conditionName[node.type]
  40. },
  41. messageId: 'conditionalInTest',
  42. node
  43. });
  44. }
  45. return {
  46. CallExpression(node) {
  47. if ((0, _utils.isTestCaseCall)(node)) {
  48. stack.push(true);
  49. if ((0, _utils.getNodeName)(node).endsWith('each')) {
  50. stack.push(true);
  51. }
  52. }
  53. },
  54. FunctionExpression(node) {
  55. stack.push(isTestFunctionExpression(node));
  56. },
  57. FunctionDeclaration(node) {
  58. const declaredVariables = context.getDeclaredVariables(node);
  59. const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
  60. stack.push(testCallExpressions.length > 0);
  61. },
  62. ArrowFunctionExpression(node) {
  63. stack.push(isTestFunctionExpression(node));
  64. },
  65. IfStatement: validate,
  66. SwitchStatement: validate,
  67. ConditionalExpression: validate,
  68. 'CallExpression:exit'() {
  69. stack.pop();
  70. },
  71. 'FunctionExpression:exit'() {
  72. stack.pop();
  73. },
  74. 'FunctionDeclaration:exit'() {
  75. stack.pop();
  76. },
  77. 'ArrowFunctionExpression:exit'() {
  78. stack.pop();
  79. }
  80. };
  81. }
  82. });
  83. exports.default = _default;