Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/oxc_linter/src/rules/eslint/default_case_last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ pub struct DefaultCaseLast;

declare_oxc_lint!(
/// ### What it does
///
/// Enforce default clauses in switch statements to be last
///
/// ### Why is this bad?
///
/// A switch statement can optionally have a default clause.
/// If present, it’s usually the last clause, but it doesn’t need to be. It is also allowed to put the default clause before all case clauses, or anywhere between. The behavior is mostly the same as if it was the last clause. The default block will be still executed only if there is no match in the case clauses (including those defined after the default), but there is also the ability to “fall through” from the default clause to the following clause in the list. However, such flow is not common and it would be confusing to the readers.
/// Even if there is no “fall through” logic, it’s still unexpected to see the default clause before or between the case clauses. By convention, it is expected to be the last clause.
/// If a switch statement should have a default clause, it’s considered a best practice to define it as the last clause.
///
/// ### Example
///
/// Examples of **incorrect** code for this rule:
///
/// ```javascript
/// switch (foo) {
/// default:
Expand All @@ -46,6 +51,22 @@ declare_oxc_lint!(
/// break;
/// }
/// ```
///
/// Examples of **correct** code for this rule:
///
/// ```javascript
/// switch (foo) {
/// case 1:
/// bar();
/// break;
/// case 2:
/// qux();
/// break;
/// default:
/// baz();
/// break;
/// }
/// ```
DefaultCaseLast,
eslint,
style
Expand Down
Loading