项目原始demo,不改动
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.
 
 
 
 

114 lignes
3.4 KiB

  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. docs: {
  12. description: "enforce a maximum depth that callbacks can be nested",
  13. category: "Stylistic Issues",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/max-nested-callbacks"
  16. },
  17. schema: [
  18. {
  19. oneOf: [
  20. {
  21. type: "integer",
  22. minimum: 0
  23. },
  24. {
  25. type: "object",
  26. properties: {
  27. maximum: {
  28. type: "integer",
  29. minimum: 0
  30. },
  31. max: {
  32. type: "integer",
  33. minimum: 0
  34. }
  35. },
  36. additionalProperties: false
  37. }
  38. ]
  39. }
  40. ]
  41. },
  42. create(context) {
  43. //--------------------------------------------------------------------------
  44. // Constants
  45. //--------------------------------------------------------------------------
  46. const option = context.options[0];
  47. let THRESHOLD = 10;
  48. if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
  49. THRESHOLD = option.maximum;
  50. }
  51. if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
  52. THRESHOLD = option.max;
  53. }
  54. if (typeof option === "number") {
  55. THRESHOLD = option;
  56. }
  57. //--------------------------------------------------------------------------
  58. // Helpers
  59. //--------------------------------------------------------------------------
  60. const callbackStack = [];
  61. /**
  62. * Checks a given function node for too many callbacks.
  63. * @param {ASTNode} node The node to check.
  64. * @returns {void}
  65. * @private
  66. */
  67. function checkFunction(node) {
  68. const parent = node.parent;
  69. if (parent.type === "CallExpression") {
  70. callbackStack.push(node);
  71. }
  72. if (callbackStack.length > THRESHOLD) {
  73. const opts = { num: callbackStack.length, max: THRESHOLD };
  74. context.report({ node, message: "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", data: opts });
  75. }
  76. }
  77. /**
  78. * Pops the call stack.
  79. * @returns {void}
  80. * @private
  81. */
  82. function popStack() {
  83. callbackStack.pop();
  84. }
  85. //--------------------------------------------------------------------------
  86. // Public API
  87. //--------------------------------------------------------------------------
  88. return {
  89. ArrowFunctionExpression: checkFunction,
  90. "ArrowFunctionExpression:exit": popStack,
  91. FunctionExpression: checkFunction,
  92. "FunctionExpression:exit": popStack
  93. };
  94. }
  95. };