Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. 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. IfStatement(node) {
  107. if (node.parent.type !== "IfStatement") {
  108. pushBlock(node);
  109. }
  110. },
  111. SwitchStatement: pushBlock,
  112. TryStatement: pushBlock,
  113. DoWhileStatement: pushBlock,
  114. WhileStatement: pushBlock,
  115. WithStatement: pushBlock,
  116. ForStatement: pushBlock,
  117. ForInStatement: pushBlock,
  118. ForOfStatement: pushBlock,
  119. "IfStatement:exit": popBlock,
  120. "SwitchStatement:exit": popBlock,
  121. "TryStatement:exit": popBlock,
  122. "DoWhileStatement:exit": popBlock,
  123. "WhileStatement:exit": popBlock,
  124. "WithStatement:exit": popBlock,
  125. "ForStatement:exit": popBlock,
  126. "ForInStatement:exit": popBlock,
  127. "ForOfStatement:exit": popBlock,
  128. "FunctionDeclaration:exit": endFunction,
  129. "FunctionExpression:exit": endFunction,
  130. "ArrowFunctionExpression:exit": endFunction,
  131. "Program:exit": endFunction
  132. };
  133. }
  134. };