max-params-function-def

Try in Playground
react-best-practicesError ProneInformational

0

No tags

No CWE or CVE

This rule warns once a function has five parameters because this can be difficult to maintain.

If you need this many parameters, you might be better off combining the parameters into an object or splitting your function.

Ast Rule: function definition


max-params-function-def

How to write a rule
const MAX_PARAMS = 5;

function visit(node, filename, code) {
  if (!node.parameters) return;
  if (node.parameters?.values.length < MAX_PARAMS) return;

  const error = buildError(
    node.name.start.line,
    node.name.start.col,
    node.name.end.line,
    node.name.end.col,
    `Having this many parameters can be difficult to manage. Consider splitting this function.`,
    "INFORMATIONAL",
    "BEST_PRACTICES"
  );
  addError(error);
}

valid-number-of-params.jsx

Expected test result: no error

function a() {}
function b(foo) {}
function c(foo, bar) {}
function d(foo, bar, baz) {}
function e(foo, bar, baz, qux) {}

too-many-params.jsx

Expected test result: has error

function a(foo, bar, baz, qux, fred) {}
function b(foo, bar, baz, qux, fred, thud) {}
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.