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(linter): eslint-plugin-unicorn switch-case-braces #1054

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ mod unicorn {
pub mod no_thenable;
pub mod no_unnecessary_await;
pub mod prefer_array_flat_map;
pub mod switch_case_braces;
pub mod throw_new_error;
}

Expand Down Expand Up @@ -253,10 +254,11 @@ oxc_macros::declare_all_lint_rules! {
unicorn::filename_case,
unicorn::no_console_spaces,
unicorn::no_instanceof_array,
unicorn::no_unnecessary_await,
unicorn::no_thenable,
unicorn::throw_new_error,
unicorn::no_unnecessary_await,
unicorn::prefer_array_flat_map,
unicorn::switch_case_braces,
unicorn::throw_new_error,
react::jsx_key,
react::jsx_no_comment_text_nodes,
react::jsx_no_duplicate_props,
Expand Down
139 changes: 139 additions & 0 deletions crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use oxc_ast::{ast::Statement, AstKind};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_formatter::Gen;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{context::LintContext, fixer::Fix, rule::Rule, AstNode};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.")]
#[diagnostic(
severity(warning),
help("There is less visual clutter for empty cases and proper scope for non-empty cases.")
)]
struct SwitchCaseBracesDiagnostic(#[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct SwitchCaseBraces;

declare_oxc_lint!(
/// ### What it does
/// Require empty switch cases to not have braces. Non-empty braces are required to have braces around them.
///
/// ### Why is this bad?
/// There is less visual clutter for empty cases and proper scope for non-empty cases.
///
/// ### Example
/// ```javascript
/// switch (num) {
/// case 1: {
///
/// }
/// case 2:
/// console.log('Case 2');
/// break;
/// }
/// ```
SwitchCaseBraces,
correctness
);

impl Rule for SwitchCaseBraces {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::SwitchStatement(switch) = node.kind() else { return };

if switch.cases.is_empty() {
return;
}

for case in &switch.cases {
for case_body in &case.consequent {
match case_body {
Statement::BlockStatement(case_block) => {
if case_block.body.is_empty() {
ctx.diagnostic_with_fix(
SwitchCaseBracesDiagnostic(case_block.span),
|| Fix::new("", case_block.span),
);
}
}
Statement::EmptyStatement(_) => {}
_ => {
ctx.diagnostic_with_fix(SwitchCaseBracesDiagnostic(case.span), || {
let modified_code = {
let mut formatter = ctx.formatter();

if let Some(case_test) = &case.test {
formatter.print_str(b"case ");
case_test.gen(&mut formatter);
} else {
formatter.print_str(b"default")
}

formatter.print_colon();
formatter.print_space();
formatter.print(b'{');
case.consequent.iter().for_each(|x| x.gen(&mut formatter));
formatter.print(b'}');

formatter.into_code()
};

Fix::new(modified_code, case.span)
});
}
}
}
}
}
}

#[test]
fn test() {
use crate::tester::Tester;

let pass = vec![
"switch(something) { case 1: case 2: {console.log('something'); break;}}",
"switch(foo){ case 1: { break; } }",
"switch(foo){ case 1: { ; /* <-- not empty */} }",
"switch(foo){ case 1: { {} /* <-- not empty */} }",
"switch(foo){ case 1: { break; } }",
"switch(foo){ default: { doSomething(); } }",
];

let fail = vec![
"switch(something) { case 1: {} case 2: {console.log('something'); break;}}",
"switch(something) { case 1: case 2: console.log('something'); break;}",
"switch(foo) { case 1: {} case 2: {} default: { doSomething(); } }",
"switch(foo) { case 1: { /* fallthrough */ } default: {}/* fallthrough */ case 3: { doSomething(); break; } }",
"switch(foo) { default: doSomething(); }",
"switch(foo) { case 1: { doSomething(); } break; /* <-- This should be between braces */ }",
"switch(foo) { default: label: {} }",
];

let fix = vec![
(
"switch(something) { case 1: {} case 2: {console.log('something'); break;}}",
"switch(something) { case 1: case 2: {console.log('something'); break;}}",
None,
),
(
"switch(something) { case 1: {} case 2: console.log('something'); break;}",
"switch(something) { case 1: case 2: {console.log(\"something\");\nbreak;\n}}",
None,
),
(
"switch(foo) { default: doSomething(); }",
"switch(foo) { default: {doSomething();\n} }",
None,
),
];

Tester::new_without_config(SwitchCaseBraces::NAME, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}
75 changes: 75 additions & 0 deletions crates/oxc_linter/src/snapshots/switch_case_braces.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
source: crates/oxc_linter/src/tester.rs
expression: switch_case_braces
---
⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(something) { case 1: {} case 2: {console.log('something'); break;}}
· ──
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(something) { case 1: case 2: console.log('something'); break;}
· ────────────────────────────────────────
Boshen marked this conversation as resolved.
Show resolved Hide resolved
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(something) { case 1: case 2: console.log('something'); break;}
· ────────────────────────────────────────
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(foo) { case 1: {} case 2: {} default: { doSomething(); } }
· ──
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(foo) { case 1: {} case 2: {} default: { doSomething(); } }
· ──
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(foo) { case 1: { /* fallthrough */ } default: {}/* fallthrough */ case 3: { doSomething(); break; } }
· ─────────────────────
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(foo) { case 1: { /* fallthrough */ } default: {}/* fallthrough */ case 3: { doSomething(); break; } }
· ──
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(foo) { default: doSomething(); }
· ───────────────────────
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(foo) { case 1: { doSomething(); } break; /* <-- This should be between braces */ }
· ─────────────────────────────────
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.

⚠ eslint-plugin-unicorn(switch-case-braces): Empty switch case shouldn't have braces and not-empty case should have braces around it.
╭─[switch_case_braces.tsx:1:1]
1 │ switch(foo) { default: label: {} }
· ──────────────────
╰────
help: There is less visual clutter for empty cases and proper scope for non-empty cases.