Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: enforce default param be the last #11361

Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules

Comments

@aladdin-add
Copy link
Member

aladdin-add commented Feb 7, 2019

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.

@aladdin-add aladdin-add added rule Relates to ESLint's core rules feature This change adds a new feature to ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Feb 7, 2019
@aladdin-add aladdin-add self-assigned this Feb 7, 2019
@aladdin-add aladdin-add changed the title New: enforce optional params be the last New: enforce default param be the last Feb 7, 2019
@mysticatea
Copy link
Member

I like this idea.

How it works if the last parameter is a rest element?

function foo(p1 = "", ...rest){}

@aladdin-add
Copy link
Member Author

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
Copy link
Member

g-plane commented Feb 24, 2019

There are 3 upvotes now. Will this be accepted?

@not-an-aardvark
Copy link
Member

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.)

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Feb 24, 2019
@g-plane
Copy link
Member

g-plane commented Feb 25, 2019

What will the rule name be?

@hornta
Copy link

hornta commented Apr 26, 2019

@g-plane perhaps default-params-end

platinumazure pushed a commit that referenced this issue Sep 7, 2019
* New: add rule default-param-last (fixes #11361)

* Chore: fix typos

* Chore: add test cases

* Apply suggestion: add column for a test case

* Update: change error message

* Docs: add example in opening section

* Add test cases about parameter destructuring
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Mar 6, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Mar 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.