10

I find ESLint very helpful when coding in Javascript, but I wonder why there is no rule that checks for the correct number of parameters in a function call:

function myFunction(param1) {
}
...
myFunction(param1,param2);

does not get detected, though it totally violates a reasonable rule. Do I miss something?

2
  • "it totally violates a reasonable rule" What is that rule? There are no rules for the count of arguments in JS.
    – Teemu
    Commented Feb 6, 2018 at 9:18
  • 3
    there is even a rule to enforce a maximum number of parameters in function definitions. ESLint is meant to enforce good coding style, to an extent that everybody can choose for herself by switching the rules off and on.
    – Lokomotywa
    Commented Feb 6, 2018 at 9:26

2 Answers 2

6

Someone could write such a rule, but there are two issues:

  1. It's impossible to use static analysis to determine what function is being called in all cases, so for foo(1), ESLint may or may not be able to tell what function foo refers to. So such a rule would at best be unreliable. Consider:

    function a() {
        return 42;
    }
    function b(x) {
        return x * 2;
    }
    var f = Math.random() < 0.5 ? a : b;
    f();
    

    Should ESLint complain? How can it know whether a or b is being called?

    So such a rule is unenforceable without introducing a type system allowing us to associate types with variables/properties that refer to functions. JavaScript, of course, doesn't have typed variables/properties. (If you want type-safety of that sort, you can use TypeScript, which allows for applying types to functions.)

  2. It's perfectly valid in JavaScript to call a function with fewer or more arguments than it has formal parameters. Some functions are designed to be called that way. You'd need a way to tell ESLint that that was your intention (and again: ESLint may not know what function is being called).

Re #2, in ES5 and earlier, we'd write a "varargs" (variable arguments) function using arguments and/or checking for undefined as the value of a formal parameter:

function foo(formal) {
  // Supply a default for our formal parameter
  if (formal === undefined) { // or you might see `if (arguments.length === 0)` here
    formal = "default";
  }
  console.log("formal =", formal);
  // Show any additional varargs we may handle
  for (var i = 1; i < arguments.length; ++i) {
    console.log("additional =", arguments[i]);
  }
}
foo();
foo(1, 2, 3);

So while it would be possible to write an ESLint tool that compared calls to a function with the number of formal parameters it declares, the rule would constantly be running up against exceptions.

But now that ES2015 introduced both rest arguments and parameter defaults, someone may well write such a rule, as the combination of rest arguments and parameter defaults makes it much more likely we can express our parameter expectations clearly in our function declarations. Here's that same function with ES2015 features:

function foo(formal = "default", ...rest) {
  // Show our formal parameter
  console.log("formal =", formal);
  // Show any additional varargs we may handle
  for (const arg of rest) {
    console.log("additional =", arg);
  }
}
foo();
foo(1, 2, 3);

Note how the declaration would indeed allow ESLint to enforce a rule. But again, see #1: ESLint cannot know from static analysis what function is being called, reliably.

3
  • 4
    I don't disagree with any of the technical details in your answer, but I think it would be useful if ESLint could have an optional warning (not error) along the lines of "Function x may have been called with wrong number of arguments", to be triggered only in cases where the right function can reasonably be identified. Obviously (as you've explained) it couldn't catch all the cases, and in some cases the argument mismatch might be deliberate, but it could catch a few.
    – nnnnnn
    Commented Feb 6, 2018 at 9:37
  • @nnnnnn: And I don't disagree. :-) I wonder if anyone's looked into it, and if so, if they found that the frequency of false positives and false negatives was unacceptably high. A rule giving you a false sense of security could be worse than no rule at all... Commented Feb 6, 2018 at 9:40
  • 2
    I like a false sense of security, sometimes it's the only security I get...
    – nnnnnn
    Commented Feb 6, 2018 at 9:44
4

You can use Visual Studio Code with a dummy JSDoc like /**@param _*/ above the method or put an autogenerated JSDoc, also add "js/ts.implicitProjectConfig.checkJs": true to your settings.

With both steps you will force the IDE checker to treat the method signature as TS language and will put squiggly lines on the wrong number of parameters (More parameters than expected or less parameters than expected). Optionally use Error Lens extension to make the alerts a lot more visible and set rules that alert no-rest-parameters, no-arguments and no-extra-arguments

https://github.com/jfmengels/eslint-plugin-fp/blob/master/docs/rules/no-rest-parameters.md

https://github.com/jfmengels/eslint-plugin-fp/blob/master/docs/rules/no-arguments.md

https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/no-extra-arguments.md

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.