From 1df65e685ae6aef9eaa8104598834e82ee430bfb Mon Sep 17 00:00:00 2001 From: shulaoda Date: Sat, 12 Oct 2024 17:47:12 +0800 Subject: [PATCH] fix(linter): error fixer of switch-case-braces --- .../src/rules/unicorn/switch_case_braces.rs | 16 ++++++++++------ .../src/snapshots/switch_case_braces.snap | 7 +++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs b/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs index f9784e8e1e27b..a4956da802c15 100644 --- a/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs +++ b/crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs @@ -75,7 +75,6 @@ impl Rule for SwitchCaseBraces { ctx.diagnostic_with_fix( switch_case_braces_diagnostic(case_body_span), |fixer| { - use oxc_codegen::{Context, Gen}; let modified_code = { let mut formatter = fixer.codegen(); @@ -89,9 +88,12 @@ impl Rule for SwitchCaseBraces { formatter.print_char(b':'); formatter.print_char(b' '); formatter.print_char(b'{'); - case.consequent - .iter() - .for_each(|x| x.gen(&mut formatter, Context::default())); + + let source_text = ctx.source_text(); + for x in &case.consequent { + formatter.print_str(x.span().source_text(source_text)); + } + formatter.print_char(b'}'); formatter.into_source_text() @@ -124,6 +126,7 @@ fn test() { ]; let fail = vec![ + "switch(s){case'':/]/}", "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(); } }", @@ -142,14 +145,15 @@ fn test() { ), ( "switch(something) { case 1: {} case 2: console.log('something'); break;}", - "switch(something) { case 1: case 2: {console.log('something');\nbreak;\n}}", + "switch(something) { case 1: case 2: {console.log('something');break;}}", None, ), ( "switch(foo) { default: doSomething(); }", - "switch(foo) { default: {doSomething();\n} }", + "switch(foo) { default: {doSomething();} }", None, ), + ("switch(s){case'':/]/}", "switch(s){case '': {/]/}}", None), ]; Tester::new(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 index c16dcc6472943..d8a9a17a96d17 100644 --- a/crates/oxc_linter/src/snapshots/switch_case_braces.snap +++ b/crates/oxc_linter/src/snapshots/switch_case_braces.snap @@ -1,6 +1,13 @@ --- source: crates/oxc_linter/src/tester.rs --- + ⚠ 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:18] + 1 │ switch(s){case'':/]/} + · ─── + ╰──── + 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:29] 1 │ switch(something) { case 1: {} case 2: {console.log('something'); break;}}