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-params.js 3.1KB

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