max-params-function-assign

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: assignment


max-params-function-assign

How to write a rule
const MAX_PARAMS = 5;

function visit(node, filename, code) {
  if (!node || !node.right) return;
	
	// if the right side isn't a function, skip it
  if (node.right.astType !== 'functiondefinition') return;
  
	// if the function doesn't have an parameters, skip it
	if (!node.right.parameters) return;
	
	// if the function has less than the max, skip it
  if (node.right.parameters?.values.length < MAX_PARAMS) return;

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

  addError(error);
}

unacceptable.js

Expected test result: has error

Function expressions with too many parameters

const a = (foo, bar, baz, qux, fred) => {};
const b = (foo, bar, baz, qux, fred, thud) => {};
const c = function(foo, bar, baz, qux, fred) {};
const d = function(foo, bar, baz, qux, fred, thud) {};

acceptable.js

Expected test result: no error

Function expressions with an acceptable number of parameters

const a = () => {};
const b = (foo) => {};
const c = (foo, bar) => {};
const d = (foo, bar, baz) => {};
const e = (foo, bar, baz, qux) => {};
const f = function() {};
const g = function(foo) {};
const h = function(foo, bar) {};
const i = function(foo, bar, baz) {};
const j = function(foo, bar, baz, qux) {};
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.