Repository navigation
Description
Please describe what the rule should do:
What category of rule is this? (place an "X" next to just one item)
[] Warns about a potential error (problem)
[x] Suggests an alternate way of doing something (suggestion)
[ ] Enforces code style (layout)
[ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about:
function foo(p1 = "", p2){}
function foo(p1, p2 = "", p3){}Why should this rule be included in ESLint (instead of a plugin)?
Default Parameter was introduced in ES6, and it was widely used in the community.
The ability to define default values for function parameters can make a function easier to use. Default parameter values allow callers to specify as many or as few arguments as they want while getting the same functionality and minimizing boilerplate, wrapper code.
But all function parameters with default values should be declared after the function parameters without default values. Otherwise, it makes it impossible for callers to take advantage of defaults; they must re-specify the defaulted values or pass undefined in order to "get to" the non-default parameters.
Are you willing to submit a pull request to implement this rule?
yes.
Activity
[-]New: enforce optional params be the last[/-][+]New: enforce default param be the last[/+]mysticatea commentedon Feb 9, 2019
I like this idea.
How it works if the last parameter is a rest element?
aladdin-add commentedon Feb 9, 2019
The rest parameter must be last -- code like
function foo(...rest, p1 = ""){}will throw an error.I think we just need to ignore rest parameters.
g-plane commentedon Feb 24, 2019
There are 3 upvotes now. Will this be accepted?
not-an-aardvark commentedon Feb 24, 2019
Yes, it seems like this can be accepted. (In general, feel free to add the label yourself if there are 3 upvotes from team members and also a champion.)
g-plane commentedon Feb 25, 2019
What will the rule name be?
langonja commentedon Apr 26, 2019
@g-plane perhaps
default-params-endNew: add rule default-param-last (fixes #11361) (#12188)