Skip to content

New: enforce default param be the last #11361

Closed

Description

@aladdin-add

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

  1. added
    ruleRelates to ESLint's core rules
    featureThis change adds a new feature to ESLint
    evaluatingThe team will evaluate this issue to decide whether it meets the criteria for inclusion
    on Feb 7, 2019
  2. self-assigned this
    on Feb 7, 2019
  3. changed the title [-]New: enforce optional params be the last[/-] [+]New: enforce default param be the last[/+] on Feb 7, 2019
  4. mysticatea commented on Feb 9, 2019

    @mysticatea
    Member

    I like this idea.

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

    function foo(p1 = "", ...rest){}
  5. aladdin-add commented on Feb 9, 2019

    @aladdin-add
    MemberAuthor

    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.

  6. g-plane commented on Feb 24, 2019

    @g-plane
    Member

    There are 3 upvotes now. Will this be accepted?

  7. not-an-aardvark commented on Feb 24, 2019

    @not-an-aardvark
    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.)

  8. added
    acceptedThere is consensus among the team that this change meets the criteria for inclusion
    and removed
    evaluatingThe team will evaluate this issue to decide whether it meets the criteria for inclusion
    on Feb 24, 2019
  9. g-plane commented on Feb 25, 2019

    @g-plane
    Member

    What will the rule name be?

  10. langonja commented on Apr 26, 2019

    @langonja

    @g-plane perhaps default-params-end

  11. added a commit that references this issue on Sep 7, 2019
    0313441
  12. locked and limited conversation to collaborators on Mar 6, 2020
  13. added
    archived due to ageThis issue has been archived; please open a new issue for any further discussion
    on Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions