Skip to content

Commit

Permalink
fix(linter): error fixer of switch-case-braces
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Oct 12, 2024
1 parent 1ba2a24 commit 1df65e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
16 changes: 10 additions & 6 deletions crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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()
Expand Down Expand Up @@ -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(); } }",
Expand All @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/snapshots/switch_case_braces.snap
Original file line number Diff line number Diff line change
@@ -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]
1switch(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]
1switch(something) { case 1: {} case 2: {console.log('something'); break;}}
Expand Down

0 comments on commit 1df65e6

Please sign in to comment.