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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. messages: {
  43. exceed: "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}."
  44. }
  45. },
  46. create(context) {
  47. //--------------------------------------------------------------------------
  48. // Constants
  49. //--------------------------------------------------------------------------
  50. const option = context.options[0];
  51. let THRESHOLD = 10;
  52. if (
  53. typeof option === "object" &&
  54. (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max"))
  55. ) {
  56. THRESHOLD = option.maximum || option.max;
  57. } else if (typeof option === "number") {
  58. THRESHOLD = option;
  59. }
  60. //--------------------------------------------------------------------------
  61. // Helpers
  62. //--------------------------------------------------------------------------
  63. const callbackStack = [];
  64. /**
  65. * Checks a given function node for too many callbacks.
  66. * @param {ASTNode} node The node to check.
  67. * @returns {void}
  68. * @private
  69. */
  70. function checkFunction(node) {
  71. const parent = node.parent;
  72. if (parent.type === "CallExpression") {
  73. callbackStack.push(node);
  74. }
  75. if (callbackStack.length > THRESHOLD) {
  76. const opts = { num: callbackStack.length, max: THRESHOLD };
  77. context.report({ node, messageId: "exceed", data: opts });
  78. }
  79. }
  80. /**
  81. * Pops the call stack.
  82. * @returns {void}
  83. * @private
  84. */
  85. function popStack() {
  86. callbackStack.pop();
  87. }
  88. //--------------------------------------------------------------------------
  89. // Public API
  90. //--------------------------------------------------------------------------
  91. return {
  92. ArrowFunctionExpression: checkFunction,
  93. "ArrowFunctionExpression:exit": popStack,
  94. FunctionExpression: checkFunction,
  95. "FunctionExpression:exit": popStack
  96. };
  97. }
  98. };