admin管理员组

文章数量:1310297

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?

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?

Share Improve this question asked Jul 2, 2015 at 22:58 tandersentandersen 3653 silver badges10 bronze badges 3
  • Is this close to what you are asking about? jslinterrors./this-function-has-too-many-parameters – Ray Toal Commented Jul 2, 2015 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 Commented Jul 2, 2015 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 Commented Jul 2, 2015 at 23:10
Add a ment  | 

4 Answers 4

Reset to default 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 remend 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.

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
};

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./jfmengels/eslint-plugin-fp/blob/master/docs/rules/no-rest-parameters.md

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

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

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.

本文标签: