jshint: Regular parameters should not come after default parameters
> jshint -v
jshint v2.9.1
file to test the behaviour:
var a = function(x = 1, i) {}
the result of jshint a.js
a.js: line 1, col 26, Regular parameters should not come after default parameters.
1 error
content of .jshintrc
:
{
"asi": true,
"esversion": 6
}
About this issue
- Original URL
- State: open
- Created 8 years ago
- Comments: 19 (8 by maintainers)
@derwaldgeist Sure! Here’s an example
.jshintrc
file that would silence the warning:Just want to mention that such kind of signature is a part of redux code samples. Look for the line:
so, it is widely used, IMHO.
@thalesfsp : Add
/* jshint -W138 */
add the beginning of your file. (Make sure you’re using jshint v2.9.1 or newer)With ‘callback’ being widely used as the last parameter of a function, this lint warning seems rather silly to me
How can I disasable it…?
The only runtime error you’ll encounter is calling like:
counter()
andcounter(undefined)
, but that’s not my point. My point is that’s terrible design and places an undue burden on the programmer and on their tooling. For example, a minifier could reasonably analyze the following:And produce:
Whereas, putting the the default first:
would produce:
That’s a fairly contrived example, but still illustrates my point that it makes the use of a default parameter completely and utterly pointless.
Just because you can, doesn’t mean you should.
This sort of pattern is absolutely the definition of “lint”.
I strongly disagree with the implication that a pattern which appears in redux is indicative of something “widely used”. I looked through redux and found examples of
counter(undefined, action)
and I’m left wondering what the point of this could possibly be, considering every single one of them actually requires theaction
argument, or face a runtime error. If theaction
is always required and thestate
is optional, why require calls that must explicitly passundefined
—that defeats the purpose of default parameter values.…I’m tempted to file a bug.
I believe it is, and anyone that doesn’t want the warning is welcome to turn it off.
Feel free to close this @jugglinmike
Requiring all call sites to pass an explicit
undefined
, for the sake of getting around a badly designed call signature is considered a bad practice and an incorrect use of default parameters.Stumbled upon the same problem with Redux. Can this be disabled in the .jshintrc config file, please?
I’m still of the opinion that in the absense of a tangible hazard (and even in the presence of otherwise-undesirable patterns), JSHint should remain silent. That said, I’m having trouble understanding how @txm’s example is distinct.