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

feat: add useDefaultSwitchClause rule #2605

Merged
merged 22 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
70fcb16
feat(lint/useDefaultSwitchClause): add basic rule implementation
michellocana Apr 21, 2024
a28220d
feat(lint/useDefaultSwitchClause): add tests from the eslint rule
michellocana Apr 22, 2024
e004cdf
feat(lint/useDefaultSwitchClause): add no default comment example
michellocana Apr 22, 2024
fca3b85
feat(lint/useDefaultSwitchClause): add commentPattern option
michellocana Apr 23, 2024
b2f2a9a
feat(lint/useDefaultSwitchClause): update configuration schema
michellocana Apr 24, 2024
9cb9138
feat(lint/useDefaultSwitchClause): improve configuration description
michellocana Apr 24, 2024
5d7ea9d
feat(lint/useDefaultSwitchClause): fix pattern string escaping
michellocana Apr 24, 2024
8f0a338
feat(lint/useDefaultSwitchClause): add tests to custom config
michellocana Apr 24, 2024
d0a676d
feat(lint/useDefaultSwitchClause): fix linter issues
michellocana Apr 24, 2024
2df9138
feat(lint/useDefaultSwitchClause): run just gen-lint
michellocana Apr 25, 2024
1c15d7e
feat(lint/useDefaultSwitchClause): improve examples
michellocana Apr 25, 2024
4801b94
feat(lint/useDefaultSwitchClause): add rule eslint source
michellocana Apr 25, 2024
608de58
feat(lint/useDefaultSwitchClause): add rule notes
michellocana Apr 25, 2024
425c85d
feat(lint/useDefaultSwitchClause): add missing eslint tests
michellocana Apr 25, 2024
29f38ae
feat(lint/useDefaultSwitchClause): remove commentPattern config
michellocana Apr 26, 2024
eb6b8d5
feat(lint/useDefaultSwitchClause): rename rule to useDefaultSwitchClause
michellocana Apr 26, 2024
5befa6b
feat(lint/useDefaultSwitchClause): improve rule notes
michellocana Apr 26, 2024
90e654c
feat(lint/useDefaultSwitchClause): remove regex dependency from biome…
michellocana Apr 26, 2024
eee7899
feat(lint/useDefaultSwitchClause): improve rule description
michellocana Apr 26, 2024
a69de87
feat(lint/useDefaultSwitchClause): update invalid snapshot
michellocana Apr 26, 2024
819cbda
feat(lint/useDefaultSwitchClause): update description and error messages
michellocana Apr 26, 2024
b7f50c8
feat(lint/useDefaultSwitchClause): treat empty switch statements as i…
michellocana Apr 26, 2024
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
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,93 @@
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 default cases in switch statements.
///
/// Some code conventions require that all switch statements have a default case. 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;
/// }
/// ```
///
/// ```js
/// switch (a) {
/// }
/// ```
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 has_case_clauses = node.cases().into_iter().len() > 0;
let is_missing_default_case = node
.cases()
.into_iter()
.any(|clause| clause.as_js_default_clause().is_some())
.not();

let is_valid = is_missing_default_case && has_case_clauses;

is_valid.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 case."
},
)
.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,4 @@
switch (a) {
case 1:
break;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.js
---
# Input
```jsx
switch (a) {
case 1:
break;
}

```

# Diagnostics
```
invalid.js:1:1 lint/nursery/useDefaultSwitchClause ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Expected a default case.

> 1 │ switch (a) {
│ ^^^^^^^^^^^^
> 2 │ case 1:
> 3 │ break;
> 4 │ }
│ ^
5 │

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,17 @@
switch (a) {
case 1:
break;
default:
break;
}

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

switch (a) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
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;
}

switch (a) {
}

```
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.