Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

max-depth.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "enforce a maximum depth that blocks can be nested",
  14. category: "Stylistic Issues",
  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. },
  43. create(context) {
  44. //--------------------------------------------------------------------------
  45. // Helpers
  46. //--------------------------------------------------------------------------
  47. const functionStack = [],
  48. option = context.options[0];
  49. let maxDepth = 4;
  50. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
  51. maxDepth = option.maximum;
  52. }
  53. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
  54. maxDepth = option.max;
  55. }
  56. if (typeof option === "number") {
  57. maxDepth = option;
  58. }
  59. /**
  60. * When parsing a new function, store it in our function stack
  61. * @returns {void}
  62. * @private
  63. */
  64. function startFunction() {
  65. functionStack.push(0);
  66. }
  67. /**
  68. * When parsing is done then pop out the reference
  69. * @returns {void}
  70. * @private
  71. */
  72. function endFunction() {
  73. functionStack.pop();
  74. }
  75. /**
  76. * Save the block and Evaluate the node
  77. * @param {ASTNode} node node to evaluate
  78. * @returns {void}
  79. * @private
  80. */
  81. function pushBlock(node) {
  82. const len = ++functionStack[functionStack.length - 1];
  83. if (len > maxDepth) {
  84. context.report({ node, message: "Blocks are nested too deeply ({{depth}}).", data: { depth: len } });
  85. }
  86. }
  87. /**
  88. * Pop the saved block
  89. * @returns {void}
  90. * @private
  91. */
  92. function popBlock() {
  93. functionStack[functionStack.length - 1]--;
  94. }
  95. //--------------------------------------------------------------------------
  96. // Public API
  97. //--------------------------------------------------------------------------
  98. return {
  99. Program: startFunction,
  100. FunctionDeclaration: startFunction,
  101. FunctionExpression: startFunction,
  102. ArrowFunctionExpression: startFunction,
  103. IfStatement(node) {
  104. if (node.parent.type !== "IfStatement") {
  105. pushBlock(node);
  106. }
  107. },
  108. SwitchStatement: pushBlock,
  109. TryStatement: pushBlock,
  110. DoWhileStatement: pushBlock,
  111. WhileStatement: pushBlock,
  112. WithStatement: pushBlock,
  113. ForStatement: pushBlock,
  114. ForInStatement: pushBlock,
  115. ForOfStatement: pushBlock,
  116. "IfStatement:exit": popBlock,
  117. "SwitchStatement:exit": popBlock,
  118. "TryStatement:exit": popBlock,
  119. "DoWhileStatement:exit": popBlock,
  120. "WhileStatement:exit": popBlock,
  121. "WithStatement:exit": popBlock,
  122. "ForStatement:exit": popBlock,
  123. "ForInStatement:exit": popBlock,
  124. "ForOfStatement:exit": popBlock,
  125. "FunctionDeclaration:exit": endFunction,
  126. "FunctionExpression:exit": endFunction,
  127. "ArrowFunctionExpression:exit": endFunction,
  128. "Program:exit": endFunction
  129. };
  130. }
  131. };