ripper234
ripper234

Reputation: 230008

Can I tell IntelliJ (10.5) not to warn about not passing an optional javascript method argument?

function foo(a, opt_b) {
  opt_b = opt_b || 1;
  ...
}

foo(1);  // IntelliJ will yell at me, saying "Invalid number of parameters, expected 2"

Is there a way to document foo() such that IntelliJ won't yell at me?

Upvotes: 13

Views: 2922

Answers (4)

Mark Ritterhoff
Mark Ritterhoff

Reputation: 140

According to http://usejsdoc.org/tags-param.html there are two ways to use comments to declare a parameter as optional:

/**
 * @param {string} [somebody] - Somebody's name.
 */
function sayHello(somebody) {
    if (!somebody) {
        somebody = 'John Doe';
    }
    alert('Hello ' + somebody);
}

and

/**
 * @param {string=} somebody - Somebody's name.
 */
function sayHello(somebody) {
    if (!somebody) {
        somebody = 'John Doe';
    }
    alert('Hello ' + somebody);
}

Both ways worked for me.

Upvotes: 2

entreprenr
entreprenr

Reputation: 276

Using JavaDoc-style comments for JavaScript, you can declare a parameter as optional with brackets [] around it:

/**
 * My function
 * @param [optionalParam] An optional parameter
 */
myFunction: function(optionalParam) {
    //...
},

Upvotes: 25

James
James

Reputation: 710

"JavaScript > General > Signature Mismatch Problem" is the inspection setting that will disable the incorrect-number-of-parameters warning for you. Unfortunately, it affects more than just parameter count. According to the description inside IntelliJ IDEA, it "Checks JavaScript called function parameters, return values, assigned expressions to be of correct type. The validation works in JavaScript, html or jsp files."

Turn off JavaScript parameter count warnings (and all type warnings as well)

Upvotes: 7

Sarfraz
Sarfraz

Reputation: 382686

You can change the settings via inspections option for JS:

enter image description here

Upvotes: 1

Related Questions