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

feat(linter): add fixer for unicorn/prefer-optional-catch-binding #4867

Merged
merged 4 commits into from
Aug 14, 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
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;
heygsc marked this conversation as resolved.
Show resolved Hide resolved
};
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();
DonIsaac marked this conversation as resolved.
Show resolved Hide resolved
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]
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]
1 │ try { } 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]
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.