Skip to content
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
12 changes: 12 additions & 0 deletions crates/oxc_linter/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ impl<'a> LintContext<'a> {
.map(|(a, _)| a as u32)
}

/// Finds the previous occurrence of the given token in the source code,
/// starting from the specified position, skipping over comments.
#[expect(clippy::cast_possible_truncation)]
pub fn find_prev_token_from(&self, start: u32, token: &str) -> Option<u32> {
let source = self.source_range(Span::from(0..start));

source
.rmatch_indices(token)
.find(|(a, _)| !self.is_inside_comment(*a as u32))
.map(|(a, _)| a as u32)
}

/// Finds the next occurrence of the given token within a bounded span,
/// starting from the specified position, skipping over comments.
///
Expand Down
28 changes: 21 additions & 7 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use options::{IgnorePattern, NoUnusedVarsOptions};
use oxc_ast::AstKind;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{AstNode, ScopeFlags, SymbolFlags};
use oxc_span::GetSpan;
use oxc_span::{GetSpan, Span};
use symbol::Symbol;

use crate::{
Expand Down Expand Up @@ -338,12 +338,26 @@ impl NoUnusedVars {
}
ctx.diagnostic(diagnostic::declared(symbol, &self.vars_ignore_pattern, false));
}
AstKind::CatchParameter(_) => {
ctx.diagnostic(diagnostic::declared(
symbol,
&self.caught_errors_ignore_pattern,
false,
));
AstKind::CatchParameter(catch) => {
// NOTE: these are safe suggestions as deleting unused catch
// bindings wont have any side effects.
ctx.diagnostic_with_suggestion(
diagnostic::declared(symbol, &self.caught_errors_ignore_pattern, false),
|fixer| {
let Span { start, end, .. } = catch.span();

let (Some(paren_start), Some(paren_end_offset)) = (
ctx.find_prev_token_from(start, "("),
ctx.find_next_token_from(end, ")"),
) else {
return fixer.noop();
};

let paren_end = end + paren_end_offset;
let delete_span = Span::new(paren_start, paren_end + 1);
fixer.delete_range(delete_span)
},
);
}
_ => ctx.diagnostic(diagnostic::declared(symbol, &IgnorePattern::<&str>::None, false)),
}
Expand Down
45 changes: 44 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,51 @@ fn test_vars_catch() {
),
];

// these suggestion fixes are safe
let fix = vec![
("try {} catch (error) { }", "try {} catch { }", None, FixKind::Suggestion),
(
"try { const x = (1 + 1); } catch (error) { }",
"try { const x = (1 + 1); } catch { }",
None,
FixKind::Suggestion,
),
("try {} catch ({ msg }) { }", "try {} catch { }", None, FixKind::Suggestion),
// spacing
("try {} catch (e) { }", "try {} catch { }", None, FixKind::Suggestion),
("try {} catch(e){ }", "try {} catch{ }", None, FixKind::Suggestion),
("try {} catch ( e) { }", "try {} catch { }", None, FixKind::Suggestion),
("try {} catch ( e \t\n ) { }", "try {} catch { }", None, FixKind::Suggestion),
// comments
("try {} catch (/* comment() */ e) { }", "try {} catch { }", None, FixKind::Suggestion),
("try {} catch (e /* comment() */) { }", "try {} catch { }", None, FixKind::Suggestion),
(
"try {} catch /* comment */ (e) { }",
"try {} catch /* comment */ { }",
None,
FixKind::Suggestion,
),
(
r"try {} catch (
// comment
// ()
e) { }",
"try {} catch { }",
None,
FixKind::Suggestion,
),
// typescript
("try {} catch (error: Error) { }", "try {} catch { }", None, FixKind::Suggestion),
(
"try {} catch (error: (typeof thing)[number]) { }",
"try {} catch { }",
None,
FixKind::Suggestion,
),
];

Tester::new(NoUnusedVars::NAME, NoUnusedVars::PLUGIN, pass, fail)
.intentionally_allow_no_fix_tests()
.expect_fix(fix)
.with_snapshot_suffix("oxc-vars-catch")
.test_and_snapshot();
}
Expand Down
Loading