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

Related to #6466 

This PR does not fundamentally solve the problem, we need to implement
`raw` or `value` for `RegExpLiteral`.


1ba2a247e1/crates/oxc_codegen/src/gen.rs (L1194-L1198)


1ba2a247e1/crates/oxc_ast/src/ast/literal.rs (L94-L101)
This commit is contained in:
dalaoshu 2024-10-13 00:15:08 +08:00 committed by GitHub
parent bbca743689
commit 0784e74791
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 6 deletions

View file

@ -75,7 +75,6 @@ impl Rule for SwitchCaseBraces {
ctx.diagnostic_with_fix( ctx.diagnostic_with_fix(
switch_case_braces_diagnostic(case_body_span), switch_case_braces_diagnostic(case_body_span),
|fixer| { |fixer| {
use oxc_codegen::{Context, Gen};
let modified_code = { let modified_code = {
let mut formatter = fixer.codegen(); 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' '); formatter.print_char(b' ');
formatter.print_char(b'{'); formatter.print_char(b'{');
case.consequent
.iter() let source_text = ctx.source_text();
.for_each(|x| x.gen(&mut formatter, Context::default())); for x in &case.consequent {
formatter.print_str(x.span().source_text(source_text));
}
formatter.print_char(b'}'); formatter.print_char(b'}');
formatter.into_source_text() formatter.into_source_text()
@ -124,6 +126,7 @@ fn test() {
]; ];
let fail = vec![ 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(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: {} 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'); break;}",
"switch(something) { case 1: case 2: {console.log('something');\nbreak;\n}}", "switch(something) { case 1: case 2: {console.log('something');break;}}",
None, None,
), ),
( (
"switch(foo) { default: doSomething(); }", "switch(foo) { default: doSomething(); }",
"switch(foo) { default: {doSomething();\n} }", "switch(foo) { default: {doSomething();} }",
None, None,
), ),
("switch(s){case'':/]/}", "switch(s){case '': {/]/}}", None),
]; ];
Tester::new(SwitchCaseBraces::NAME, pass, fail).expect_fix(fix).test_and_snapshot(); Tester::new(SwitchCaseBraces::NAME, pass, fail).expect_fix(fix).test_and_snapshot();

View file

@ -1,6 +1,13 @@
--- ---
source: crates/oxc_linter/src/tester.rs 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. ⚠ 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] ╭─[switch_case_braces.tsx:1:29]
1 │ switch(something) { case 1: {} case 2: {console.log('something'); break;}} 1 │ switch(something) { case 1: {} case 2: {console.log('something'); break;}}