Skip to content

Commit

Permalink
feat(lint/useDefaultSwitchClause): treat empty switch statements as i…
Browse files Browse the repository at this point in the history
…nvalid
  • Loading branch information
michellocana committed Apr 26, 2024
1 parent 819cbda commit b7f50c8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ declare_rule! {
/// break;
/// }
/// ```
///
/// ```js
/// switch (a) {
/// }
/// ```
pub UseDefaultSwitchClause {
version: "next",
name: "useDefaultSwitchClause",
Expand All @@ -59,16 +54,13 @@ impl Rule for UseDefaultSwitchClause {
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(())
is_missing_default_case.then_some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ switch (a) {
case 1:
break;
}

switch (a) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ switch (a) {
break;
}

switch (a) {
}

```

# Diagnostics
Expand All @@ -24,6 +27,27 @@ invalid.js:1:1 lint/nursery/useDefaultSwitchClause ━━━━━━━━━
> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@ switch (a) {
default:
break;
}

switch (a) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ switch (a) {
break;
}

switch (a) {
}

```

0 comments on commit b7f50c8

Please sign in to comment.