Skip to content

Commit

Permalink
feat(lint/useDefaultSwitchClause): add rule (#2605)
Browse files Browse the repository at this point in the history
  • Loading branch information
michellocana committed Apr 26, 2024
1 parent b3ed181 commit de063b4
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 7 deletions.
15 changes: 15 additions & 0 deletions crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 26 additions & 7 deletions crates/biome_configuration/src/linter/rules.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ define_categories! {
"lint/nursery/useBiomeSuppressionComment": "https://biomejs.dev/linter/rules/use-biome-suppression-comment",
"lint/nursery/useConsistentNewBuiltin": "https://biomejs.dev/linter/rules/use-consistent-new-builtin",
"lint/nursery/useGenericFontNames": "https://biomejs.dev/linter/rules/use-generic-font-names",
"lint/nursery/useDefaultSwitchClause": "https://biomejs.dev/linter/rules/use-default-switch-clause",
"lint/nursery/useImportRestrictions": "https://biomejs.dev/linter/rules/use-import-restrictions",
"lint/nursery/useSortedClasses": "https://biomejs.dev/linter/rules/use-sorted-classes",
"lint/performance/noAccumulatingSpread": "https://biomejs.dev/linter/rules/no-accumulating-spread",
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::ops::Not;

use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::JsSwitchStatement;
use biome_rowan::AstNode;

declare_rule! {
/// Require the default clause in switch statements.
///
/// Some code conventions require that all switch statements have a default clause. The thinking is that it’s better
/// to always explicitly state what the default behavior should be so that it’s clear whether or not the developer
/// forgot to include the default behavior by mistake.
///
/// ## Examples
///
/// ### Invalid
///
/// ```js,expect_diagnostic
/// switch (a) {
/// case 1:
/// /* code */
/// break;
/// }
/// ```
///
/// ### Valid
///
/// ```js
/// switch (a) {
/// case 1:
/// /* code */
/// break;
///
/// default:
/// /* code */
/// break;
/// }
/// ```
pub UseDefaultSwitchClause {
version: "next",
name: "useDefaultSwitchClause",
sources: &[RuleSource::Eslint("default-case")],
recommended: false,
}
}

impl Rule for UseDefaultSwitchClause {
type Query = Ast<JsSwitchStatement>;
type State = ();
type Signals = Option<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

let is_missing_default_case = node
.cases()
.into_iter()
.any(|clause| clause.as_js_default_clause().is_some())
.not();

is_missing_default_case.then_some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();

Some(
RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! {
"Expected a default switch clause."
},
)
.note(markup! {
"The lack of a default clause can be a possible omission."
})
.note(markup! {
"Consider adding a default clause."
}),
)
}
}
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ pub type UseConsistentArrayType = < lint :: style :: use_consistent_array_type :
pub type UseConsistentNewBuiltin = < lint :: nursery :: use_consistent_new_builtin :: UseConsistentNewBuiltin as biome_analyze :: Rule > :: Options ;
pub type UseConst = <lint::style::use_const::UseConst as biome_analyze::Rule>::Options;
pub type UseDefaultParameterLast = < lint :: style :: use_default_parameter_last :: UseDefaultParameterLast as biome_analyze :: Rule > :: Options ;
pub type UseDefaultSwitchClause = < lint :: nursery :: use_default_switch_clause :: UseDefaultSwitchClause as biome_analyze :: Rule > :: Options ;
pub type UseDefaultSwitchClauseLast = < lint :: suspicious :: use_default_switch_clause_last :: UseDefaultSwitchClauseLast as biome_analyze :: Rule > :: Options ;
pub type UseEnumInitializers =
<lint::style::use_enum_initializers::UseEnumInitializers as biome_analyze::Rule>::Options;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
switch (a) {
case 1:
break;
}

switch (a) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.js
---
# Input
```jsx
switch (a) {
case 1:
break;
}

switch (a) {
}

```

# Diagnostics
```
invalid.js:1:1 lint/nursery/useDefaultSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Expected a default switch clause.
> 1 │ switch (a) {
│ ^^^^^^^^^^^^
> 2 │ case 1:
> 3 │ break;
> 4 │ }
│ ^
5 │
6 │ switch (a) {
i The lack of a default clause can be a possible omission.
i Consider adding a default clause.
```

```
invalid.js:6:1 lint/nursery/useDefaultSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Expected a default switch clause.
4 │ }
5 │
> 6 │ switch (a) {
│ ^^^^^^^^^^^^
> 7 │ }
│ ^
8 │
i The lack of a default clause can be a possible omission.
i Consider adding a default clause.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
switch (a) {
case 1:
break;
default:
break;
}

switch (a) {
case 1:
break;
case 2:
default:
break;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.js
---
# Input
```jsx
switch (a) {
case 1:
break;
default:
break;
}

switch (a) {
case 1:
break;
case 2:
default:
break;
}

```
5 changes: 5 additions & 0 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de063b4

Please sign in to comment.