New: enforce default param be the last
See original GitHub issuePlease 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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:6 (5 by maintainers)
Top GitHub Comments
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 perhaps
default-params-end