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 rule proposal: default-case-last #12665

Closed
mdjermanovic opened this issue Dec 15, 2019 · 4 comments
Closed

New rule proposal: default-case-last #12665

mdjermanovic opened this issue Dec 15, 2019 · 4 comments
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

@mdjermanovic
Copy link
Member

Please describe what the rule should do:

Enforce default clauses in switch statements to be last.

What category of rule is this? (place an "X" next to just one item)

[X] Suggests an alternate way of doing something (suggestion)

Provide 2-3 code examples that this rule will warn about:

switch (foo) {
  case 1:
    bar();
    break;
  default:
    baz();
    break;
  case 2:
    quux();
    break;    
}

switch (foo) {
  default:
    doSomethingForNonZero();
    // fallthrough
  case 0:
    doSomethingForAll();
    break;
}

Why should this rule be included in ESLint (instead of a plugin)?

This rule enforces a common convention.

Even if a non-last default might make sense for a specific situation, it's better to refactor the code to a more understandable version.

A non-last default is, I believe, unexpected for readers. A reader might not know if it is a valid code at all, and what's the actual behavior (does the evaluation stop when it finds default in the middle, does it execute the default block and then continue searching, etc.).

This rule applies only to switch statements that already have default clauses. It doesn't enforce the existence of default clauses (there is the default-case rule for that purpose).

Are you willing to submit a pull request to implement this rule?

Yes.

@mdjermanovic mdjermanovic 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 Dec 15, 2019
@mdjermanovic mdjermanovic self-assigned this Dec 15, 2019
@platinumazure
Copy link
Member

I didn't even know this was allowed in JS. Yes, we should definitely create a rule for enforcing this!

@ilyavolodin ilyavolodin 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 Dec 15, 2019
@mdjermanovic
Copy link
Member Author

I'm working on this.

@shirohana
Copy link

Hi there,

I found that the rule default-case-last hasn't be linked correctly in https://eslint.org/docs/rules/default-case-last but shows in https://eslint.org/docs/rules/#best-practices, is it in a plan to be update or just a bug (´・ω・`)?

@kaicataldo
Copy link
Member

This is a bug in our website generator for prerelease versions. Sorry about that!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jul 17, 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 Jul 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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
Projects
None yet
Development

No branches or pull requests

5 participants