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-nested-callbacks.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @fileoverview Rule to enforce a maximum number of nested callbacks.
  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 callbacks can be nested",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/max-nested-callbacks"
  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. // Constants
  46. //--------------------------------------------------------------------------
  47. const option = context.options[0];
  48. let THRESHOLD = 10;
  49. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
  50. THRESHOLD = option.maximum;
  51. }
  52. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
  53. THRESHOLD = option.max;
  54. }
  55. if (typeof option === "number") {
  56. THRESHOLD = option;
  57. }
  58. //--------------------------------------------------------------------------
  59. // Helpers
  60. //--------------------------------------------------------------------------
  61. const callbackStack = [];
  62. /**
  63. * Checks a given function node for too many callbacks.
  64. * @param {ASTNode} node The node to check.
  65. * @returns {void}
  66. * @private
  67. */
  68. function checkFunction(node) {
  69. const parent = node.parent;
  70. if (parent.type === "CallExpression") {
  71. callbackStack.push(node);
  72. }
  73. if (callbackStack.length > THRESHOLD) {
  74. const opts = { num: callbackStack.length, max: THRESHOLD };
  75. context.report({ node, message: "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", data: opts });
  76. }
  77. }
  78. /**
  79. * Pops the call stack.
  80. * @returns {void}
  81. * @private
  82. */
  83. function popStack() {
  84. callbackStack.pop();
  85. }
  86. //--------------------------------------------------------------------------
  87. // Public API
  88. //--------------------------------------------------------------------------
  89. return {
  90. ArrowFunctionExpression: checkFunction,
  91. "ArrowFunctionExpression:exit": popStack,
  92. FunctionExpression: checkFunction,
  93. "FunctionExpression:exit": popStack
  94. };
  95. }
  96. };