mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
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:
parent
bbca743689
commit
0784e74791
2 changed files with 17 additions and 6 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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;}}
|
||||
|
|
|
|||
Loading…
Reference in a new issue