mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 20:28:58 +00:00
feat(linter): add fixer for unicorn/prefer-optional-catch-binding (#4867)
This commit is contained in:
parent
707a01f438
commit
97e38cd493
2 changed files with 53 additions and 3 deletions
|
|
@ -39,7 +39,7 @@ declare_oxc_lint!(
|
|||
/// ```
|
||||
PreferOptionalCatchBinding,
|
||||
style,
|
||||
pending
|
||||
fix
|
||||
);
|
||||
|
||||
impl Rule for PreferOptionalCatchBinding {
|
||||
|
|
@ -51,7 +51,31 @@ impl Rule for PreferOptionalCatchBinding {
|
|||
if references_count != 0 {
|
||||
return;
|
||||
}
|
||||
ctx.diagnostic(prefer_optional_catch_binding_diagnostic(catch_param.pattern.span()));
|
||||
let Some(parent_node) = ctx.nodes().parent_node(node.id()) else {
|
||||
return;
|
||||
};
|
||||
let AstKind::CatchClause(catch_clause) = parent_node.kind() else {
|
||||
return;
|
||||
};
|
||||
ctx.diagnostic_with_fix(
|
||||
prefer_optional_catch_binding_diagnostic(catch_param.pattern.span()),
|
||||
|fixer| {
|
||||
let mut start = catch_clause.span().start + 5;
|
||||
let total_param = Span::new(start, catch_param.span().start);
|
||||
let total_param_value = ctx.source_range(total_param);
|
||||
let plus_space: u32 = total_param_value
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.position(|x| !x.is_ascii_whitespace())
|
||||
.unwrap_or(0)
|
||||
.try_into()
|
||||
.unwrap();
|
||||
start += plus_space;
|
||||
let end = catch_clause.body.span().start;
|
||||
let span = Span::new(start, end);
|
||||
fixer.delete(&span)
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -110,5 +134,21 @@ fn test() {
|
|||
r"try {} catch ({cause: {message}}) {}",
|
||||
];
|
||||
|
||||
Tester::new(PreferOptionalCatchBinding::NAME, pass, fail).test_and_snapshot();
|
||||
let fix = vec![
|
||||
(r"try {} catch (_) {}", r"try {} catch {}"),
|
||||
(r"try {} catch (theRealErrorName) {}", r"try {} catch {}"),
|
||||
(
|
||||
r"try { } catch (e)
|
||||
{ }",
|
||||
r"try { } catch { }",
|
||||
),
|
||||
(r"try {} catch(e) {}", r"try {} catch{}"),
|
||||
(r"try {} catch (e){}", r"try {} catch {}"),
|
||||
(r"try {} catch ({}) {}", r"try {} catch {}"),
|
||||
(r"try {} catch ({message}) {}", r"try {} catch {}"),
|
||||
(r"try {} catch ({message: notUsedMessage}) {}", r"try {} catch {}"),
|
||||
(r"try {} catch ({cause: {message}}) {}", r"try {} catch {}"),
|
||||
];
|
||||
|
||||
Tester::new(PreferOptionalCatchBinding::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,20 @@
|
|||
---
|
||||
source: crates/oxc_linter/src/tester.rs
|
||||
assertion_line: 234
|
||||
---
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:15]
|
||||
1 │ try {} catch (_) {}
|
||||
· ─
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:15]
|
||||
1 │ try {} catch (theRealErrorName) {}
|
||||
· ────────────────
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:25]
|
||||
|
|
@ -19,39 +22,46 @@ source: crates/oxc_linter/src/tester.rs
|
|||
· ─
|
||||
2 │ { }
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:14]
|
||||
1 │ try {} catch(e) {}
|
||||
· ─
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:15]
|
||||
1 │ try {} catch (e){}
|
||||
· ─
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:15]
|
||||
1 │ try {} catch ({}) {}
|
||||
· ──
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:15]
|
||||
1 │ try {} catch ({message}) {}
|
||||
· ─────────
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:15]
|
||||
1 │ try {} catch ({message: notUsedMessage}) {}
|
||||
· ─────────────────────────
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(prefer-optional-catch-binding): Prefer omitting the catch binding parameter if it is unused
|
||||
╭─[prefer_optional_catch_binding.tsx:1:15]
|
||||
1 │ try {} catch ({cause: {message}}) {}
|
||||
· ──────────────────
|
||||
╰────
|
||||
help: Delete this code.
|
||||
|
|
|
|||
Loading…
Reference in a new issue