Skip to content

Commit e68bca9

Browse files
committed
docs(linter): improve docs of default-case-last (#11031)
1 parent 56bb9ce commit e68bca9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

crates/oxc_linter/src/rules/eslint/default_case_last.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ pub struct DefaultCaseLast;
1515

1616
declare_oxc_lint!(
1717
/// ### What it does
18+
///
1819
/// Enforce default clauses in switch statements to be last
1920
///
2021
/// ### Why is this bad?
22+
///
2123
/// A switch statement can optionally have a default clause.
2224
/// 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.
2325
/// 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.
2426
/// If a switch statement should have a default clause, it’s considered a best practice to define it as the last clause.
2527
///
2628
/// ### Example
29+
///
30+
/// Examples of **incorrect** code for this rule:
31+
///
2732
/// ```javascript
2833
/// switch (foo) {
2934
/// default:
@@ -46,6 +51,22 @@ declare_oxc_lint!(
4651
/// break;
4752
/// }
4853
/// ```
54+
///
55+
/// Examples of **correct** code for this rule:
56+
///
57+
/// ```javascript
58+
/// switch (foo) {
59+
/// case 1:
60+
/// bar();
61+
/// break;
62+
/// case 2:
63+
/// qux();
64+
/// break;
65+
/// default:
66+
/// baz();
67+
/// break;
68+
/// }
69+
/// ```
4970
DefaultCaseLast,
5071
eslint,
5172
style

0 commit comments

Comments
 (0)