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

fix(linter): error fixer of switch-case-braces #6474

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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]
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;}}
Expand Down