11

I'm new to javascript programming (and scripting languages in general), but I've been using JS Lint to help me when I make syntax errors or accidentally declare a global variable.

However, there is a scenario that JS Lint does not cover, which I feel would be incredibly handy. See the code below:

(function () {
    "use strict";
    /*global alert */

    var testFunction = function (someMessage) {
        alert("stuff is happening: " + someMessage);
    };

    testFunction(1, 2);
    testFunction();
}());

Notice that I am passing the wrong number of parameters to testFunction. I don't foresee myself ever in a situation where I would intentionally leave out a parameter or add an extra one like that. However, neither JS Lint nor JS Hint consider this an error.

Is there some other tool that would catch this for me? Or is there some reason why passing parameters like that shouldn't be error checked?

tandersen
  • 365
  • 2
  • 10
  • Is this close to what you are asking about? https://jslinterrors.com/this-function-has-too-many-parameters – Ray Toal Jul 02 '15 at 23:01
  • Nope. That seems to be a limit on how many parameters the function can have. I don't particularly care about whether the function has 2 or 7 parameters. I just don't want to call a function that has 2 parameters with a call that passes in 7. – tandersen Jul 02 '15 at 23:07
  • Ah so you want a general solution other than marking every function with a custom value like min=3 and max=3 for a 3-parameter function. I haven't see such a lint rule. – Ray Toal Jul 02 '15 at 23:10

3 Answers3

4

This is not generally possible with any static analysis tool. There are several reasons for this:

  • In general, JS functions can accept any number of arguments.
  • Most (all?) linters only work on a single file at a time. They do not know anything about functions declared in other files
  • There is no guarantee that a property being invoked as a function is the function that you expect. Consider this snippet:

    var obj = { myFunc : function(a,b) { ... } };
    var doSomething(x) { x.myFunc = function(a) { ... }; };
    doSomething(obj);
    obj.myFunc();
    

There is no way to know that myFunc now takes a different number of args after the call to doSomething.

JavaScript is a dynamic language and you should accept and embrace that.

Instead of relying on linting to catch problems that it wasn't intended to I would recommend adding preconditions to your functions that does the check.

Create a helper function like this:

function checkThat(expression, message) {
  if (!expression) {
    throw new Error(message);
  }
}

Then use it like this:

function myFunc(a, b) {
  checkThat(arguments.length === 2, "Wrong number of arguments.");

And with proper unit testing, you should never see this error message in production.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
2

It's not natively possible in javascript. You would have to do something like this:

var testFunction = function (someMessage) {
    var args = Array.prototype.slice.call(arguments);
    if (args.length !== 1) throw new Error ("Wrong number of arguments");
    if (typeof args[1] !== string)  throw new Error ("Must pass a string");
    // continue
};
Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96
-1

Paul Irish demoed a hack for this a while back...passing undefined at the end of the parameters...

var testFunction = function (someMessage, undefined) {
  alert("stuff is happening: " + someMessage);
};
testFunction("one", "two", "three");

He demos it here...see if it's what your looking for.

Flimm
  • 136,138
  • 45
  • 251
  • 267
kaidez
  • 679
  • 8
  • 27
  • What exactly is the hack? What does putting `undefined` in the function signature do? – Flimm Mar 09 '22 at 15:04
  • @Flimm: Paul discusses it in that link to the demo above but basically, it prevents someone from passing an extra parameter to a function when it doesn't require it. It's a bit outdated as no one really worries about this happening anymore and Paul admitted that this was fixing an edge case. But it's interesting. – kaidez Mar 10 '22 at 21:01
  • I don't have access to the Youtube video currently. I can still run `testFunction("one", "two", "three")` so I'm not sure what the `undefined` is preventing exactly. – Flimm Mar 10 '22 at 21:57
  • It prevents someone using the method from passing an unwanted param. Again, edge case, old school issue. – kaidez Mar 10 '22 at 22:25
  • If what you're saying is true, then `testFunction("one", "two", "three");` should throw an error saying that invalid arguments were passed. Instead, it runs normally. `, undefined` made no difference at all. I've converted your code to a code snippet, run it and see for yourself. – Flimm Mar 11 '22 at 05:29