Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

99 wiersze
3.1 KiB

  1. /**
  2. * @fileoverview Rule to flag when a function has too many parameters
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash");
  10. const astUtils = require("../util/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: "enforce a maximum number of parameters in function definitions",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/max-params"
  21. },
  22. schema: [
  23. {
  24. oneOf: [
  25. {
  26. type: "integer",
  27. minimum: 0
  28. },
  29. {
  30. type: "object",
  31. properties: {
  32. maximum: {
  33. type: "integer",
  34. minimum: 0
  35. },
  36. max: {
  37. type: "integer",
  38. minimum: 0
  39. }
  40. },
  41. additionalProperties: false
  42. }
  43. ]
  44. }
  45. ]
  46. },
  47. create(context) {
  48. const sourceCode = context.getSourceCode();
  49. const option = context.options[0];
  50. let numParams = 3;
  51. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
  52. numParams = option.maximum;
  53. }
  54. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
  55. numParams = option.max;
  56. }
  57. if (typeof option === "number") {
  58. numParams = option;
  59. }
  60. /**
  61. * Checks a function to see if it has too many parameters.
  62. * @param {ASTNode} node The node to check.
  63. * @returns {void}
  64. * @private
  65. */
  66. function checkFunction(node) {
  67. if (node.params.length > numParams) {
  68. context.report({
  69. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  70. node,
  71. message: "{{name}} has too many parameters ({{count}}). Maximum allowed is {{max}}.",
  72. data: {
  73. name: lodash.upperFirst(astUtils.getFunctionNameWithKind(node)),
  74. count: node.params.length,
  75. max: numParams
  76. }
  77. });
  78. }
  79. }
  80. return {
  81. FunctionDeclaration: checkFunction,
  82. ArrowFunctionExpression: checkFunction,
  83. FunctionExpression: checkFunction
  84. };
  85. }
  86. };