From 162c720ef1095ed094f2642a5b051b7aa2d0ed4b Mon Sep 17 00:00:00 2001 From: Mariusz Antas Date: Fri, 27 Oct 2023 02:28:06 +0200 Subject: [PATCH] feat(linter): eslint-plugin-unicorn switch-case-braces (#1054) Rule taken from https://github.com/web-infra-dev/oxc/issues/684. Tests taken from: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/test/switch-case-braces.mjs --- crates/oxc_linter/src/rules.rs | 2 + .../src/rules/unicorn/switch_case_braces.rs | 156 ++++++++++++++++++ .../src/snapshots/switch_case_braces.snap | 75 +++++++++ 3 files changed, 233 insertions(+) create mode 100644 crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs create mode 100644 crates/oxc_linter/src/snapshots/switch_case_braces.snap diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index a6950e74e..3b1526f3a 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -148,6 +148,7 @@ mod unicorn { pub mod no_unnecessary_await; pub mod prefer_array_flat_map; pub mod prefer_logical_operator_over_ternary; + pub mod switch_case_braces; pub mod text_encoding_identifier_case; pub mod throw_new_error; } @@ -262,6 +263,7 @@ oxc_macros::declare_all_lint_rules! { unicorn::no_unnecessary_await, unicorn::prefer_array_flat_map, unicorn::prefer_logical_operator_over_ternary, + unicorn::switch_case_braces, unicorn::text_encoding_identifier_case, unicorn::throw_new_error, react::jsx_key, diff --git a/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs b/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs new file mode 100644 index 000000000..a970f5fe4 --- /dev/null +++ b/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs @@ -0,0 +1,156 @@ +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::{GetSpan, 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_consequent in &case.consequent { + match case_consequent { + 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(_) => {} + _ => { + let Some(first_statement) = &case.consequent.first() else { + return; + }; + let Some(last_statement) = &case.consequent.last() else { + return; + }; + + let case_body_span = Span { + start: first_statement.span().start, + end: last_statement.span().end, + }; + + ctx.diagnostic_with_fix(SwitchCaseBracesDiagnostic(case_body_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) + }); + + // After first incorrect consequent we have to break to not repeat the work + break; + } + } + } + } + } +} + +#[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: {} }", + "switch(something) { case 1: case 2: { console.log('something'); break; } case 3: console.log('something else'); }", + + ]; + + 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(); +} diff --git a/crates/oxc_linter/src/snapshots/switch_case_braces.snap b/crates/oxc_linter/src/snapshots/switch_case_braces.snap new file mode 100644 index 000000000..d57e62647 --- /dev/null +++ b/crates/oxc_linter/src/snapshots/switch_case_braces.snap @@ -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;} + · ──────────────────────────────── + ╰──── + 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. + + ⚠ 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; } case 3: console.log('something else'); } + · ────────────────────────────── + ╰──── + help: There is less visual clutter for empty cases and proper scope for non-empty cases. + +