Skip to content

Commit

Permalink
feat(linter): add fixer for unicorn/prefer-optional-catch-binding (#4867
Browse files Browse the repository at this point in the history
)
  • Loading branch information
heygsc authored Aug 14, 2024
1 parent 707a01f commit 97e38cd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ declare_oxc_lint!(
/// ```
PreferOptionalCatchBinding,
style,
pending
fix
);

impl Rule for PreferOptionalCatchBinding {
Expand All @@ -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)
},
);
}
}

Expand Down Expand Up @@ -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();
}
10 changes: 10 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_optional_catch_binding.snap
Original file line number Diff line number Diff line change
@@ -1,57 +1,67 @@
---
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]
1try {} 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]
1try {} 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]
1try { } catch (e)
· ─
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]
1try {} 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]
1try {} 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]
1try {} 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]
1try {} 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]
1try {} 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]
1try {} catch ({cause: {message}}) {}
· ──────────────────
╰────
help: Delete this code.

0 comments on commit 97e38cd

Please sign in to comment.