max-depth.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @fileoverview A rule to set the maximum depth block can be nested in a function.
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Enforce a maximum depth that blocks can be nested",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/max-depth"
  17. },
  18. schema: [
  19. {
  20. oneOf: [
  21. {
  22. type: "integer",
  23. minimum: 0
  24. },
  25. {
  26. type: "object",
  27. properties: {
  28. maximum: {
  29. type: "integer",
  30. minimum: 0
  31. },
  32. max: {
  33. type: "integer",
  34. minimum: 0
  35. }
  36. },
  37. additionalProperties: false
  38. }
  39. ]
  40. }
  41. ],
  42. messages: {
  43. tooDeeply: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}}."
  44. }
  45. },
  46. create(context) {
  47. //--------------------------------------------------------------------------
  48. // Helpers
  49. //--------------------------------------------------------------------------
  50. const functionStack = [],
  51. option = context.options[0];
  52. let maxDepth = 4;
  53. if (
  54. typeof option === "object" &&
  55. (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max"))
  56. ) {
  57. maxDepth = option.maximum || option.max;
  58. }
  59. if (typeof option === "number") {
  60. maxDepth = option;
  61. }
  62. /**
  63. * When parsing a new function, store it in our function stack
  64. * @returns {void}
  65. * @private
  66. */
  67. function startFunction() {
  68. functionStack.push(0);
  69. }
  70. /**
  71. * When parsing is done then pop out the reference
  72. * @returns {void}
  73. * @private
  74. */
  75. function endFunction() {
  76. functionStack.pop();
  77. }
  78. /**
  79. * Save the block and Evaluate the node
  80. * @param {ASTNode} node node to evaluate
  81. * @returns {void}
  82. * @private
  83. */
  84. function pushBlock(node) {
  85. const len = ++functionStack[functionStack.length - 1];
  86. if (len > maxDepth) {
  87. context.report({ node, messageId: "tooDeeply", data: { depth: len, maxDepth } });
  88. }
  89. }
  90. /**
  91. * Pop the saved block
  92. * @returns {void}
  93. * @private
  94. */
  95. function popBlock() {
  96. functionStack[functionStack.length - 1]--;
  97. }
  98. //--------------------------------------------------------------------------
  99. // Public API
  100. //--------------------------------------------------------------------------
  101. return {
  102. Program: startFunction,
  103. FunctionDeclaration: startFunction,
  104. FunctionExpression: startFunction,
  105. ArrowFunctionExpression: startFunction,
  106. StaticBlock: startFunction,
  107. IfStatement(node) {
  108. if (node.parent.type !== "IfStatement") {
  109. pushBlock(node);
  110. }
  111. },
  112. SwitchStatement: pushBlock,
  113. TryStatement: pushBlock,
  114. DoWhileStatement: pushBlock,
  115. WhileStatement: pushBlock,
  116. WithStatement: pushBlock,
  117. ForStatement: pushBlock,
  118. ForInStatement: pushBlock,
  119. ForOfStatement: pushBlock,
  120. "IfStatement:exit": popBlock,
  121. "SwitchStatement:exit": popBlock,
  122. "TryStatement:exit": popBlock,
  123. "DoWhileStatement:exit": popBlock,
  124. "WhileStatement:exit": popBlock,
  125. "WithStatement:exit": popBlock,
  126. "ForStatement:exit": popBlock,
  127. "ForInStatement:exit": popBlock,
  128. "ForOfStatement:exit": popBlock,
  129. "FunctionDeclaration:exit": endFunction,
  130. "FunctionExpression:exit": endFunction,
  131. "ArrowFunctionExpression:exit": endFunction,
  132. "StaticBlock:exit": endFunction,
  133. "Program:exit": endFunction
  134. };
  135. }
  136. };