question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow "never" in rule default-case

See original GitHub issue

What rule do you want to change?

default-case

What change to do you want to make?

Generate more warnings

How do you think the change should be implemented?

A new option

Example code

/*eslint default-case: ["error", "never"]*/

switch (a) {
    case 1:
        /* code */
        break;

    default:
        /* code */
        break;
}

What does the rule currently do for this code?

Right now there is only an option to enforce that default cases must be used in switch statements, but no option to enforce that default should not be used at all. In my experience default cases bring more trouble because it indicates that some cases have not been handled. Also, it breaks type checking where the type system can detect if the switch case is exhaustive or not. For example by using switch-exhaustiveness-check

What will the rule do after it’s changed?

The rule will be able to be configured to not only enforce usage of default cases, but also to enforce not to use default cases.

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nzakascommented, Mar 9, 2022

Thanks for the input. As stated in the README, we actually aren’t making any further updates to rules that enforce stylistic preferences. If you have a strong preference for such a change, you can always copy the existing rule into a custom rule and make it behave the way you want.

1reaction
mdjermanoviccommented, Mar 7, 2022

This change is consistent with a lot of other rules where one can choose to enforce or disable a feature, however. That this rule accepts a special comment syntax to disable the check seems more like a bug to me than a feature.

Rules that accept special comments have a different purpose than rules that require/disallow some feature based on configuration. The purpose of rules that accept special comments is to enforce indicating the intention to use or not use a feature in places where using or not using that feature looks like a possible error, not to entirely require/disallow the feature.

default-case treats missing default cases as possible errors and requires a special comment to indicate intention.

Similarly, no-fallthrough treats fallthroughs between cases as possible errors, and requires a special comment to indicate that the fallthrough was intentional.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule suggestion: use of never #2616 - GitHub
I think the rule I want is "disallow any use of a never -typed value ... case 'a': return 1; case 'b': return...
Read more >
Rule 55. Default; Default Judgment - Law.Cornell.Edu
A default judgment that does not dispose of all of the claims among all parties is not a final judgment unless the court...
Read more >
Should switch statements always contain a default clause?
Nonexhaustive switch statements: Those should always have a default value. As the name suggests those are statements which do not cover all possible...
Read more >
default-case - ESLint - Pluggable JavaScript Linter
This rule aims to require default case in switch statements. You may optionally include a // no default after the last case if...
Read more >
Setting Aside A Civil Default Judgment
If the court has entered a default judgment against you, the plaintiff can collect it like any other judgment. That means she can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found