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
22 changes: 22 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/ignored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,26 @@ mod test {
assert!(rule.is_ignored_array_destructured(&Atom::from("_x")));
assert!(!rule.is_ignored_array_destructured("notIgnored"));
}

#[test]
fn test_ignored_catch_errors() {
let rule = NoUnusedVars::from_configuration(serde_json::json!([
{
"caughtErrorsIgnorePattern": "^_",
"caughtErrors": "all",
}
]));
assert!(rule.is_ignored_catch_err("_"));
assert!(rule.is_ignored_catch_err("_err"));
assert!(!rule.is_ignored_catch_err("err"));

let rule = NoUnusedVars::from_configuration(serde_json::json!([
{
"caughtErrors": "none",
}
]));
assert!(rule.is_ignored_catch_err("_"));
assert!(rule.is_ignored_catch_err("_err"));
assert!(rule.is_ignored_catch_err("err"));
}
}
21 changes: 19 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,31 @@ fn test_vars_destructure() {
#[test]
fn test_vars_catch() {
let pass = vec![
// lb
("try {} catch (e) { throw e }", None),
("try {} catch (e) { }", Some(json!([{ "caughtErrors": "none" }]))),
("try {} catch { }", None),
("try {} catch(_) { }", Some(json!([{ "caughtErrorsIgnorePattern": "^_" }]))),
(
"try {} catch(_) { }",
Some(json!([{ "caughtErrors": "all", "caughtErrorsIgnorePattern": "^_" }])),
),
(
"try {} catch(_e) { }",
Some(json!([{ "caughtErrors": "all", "caughtErrorsIgnorePattern": "^_" }])),
),
];

let fail = vec![
// lb
("try {} catch (e) { }", Some(json!([{ "caughtErrors": "all" }]))),
("try {} catch(_) { }", None),
(
"try {} catch(_) { }",
Some(json!([{ "caughtErrors": "all", "varsIgnorePattern": "^_" }])),
),
(
"try {} catch(foo) { }",
Some(json!([{ "caughtErrors": "all", "caughtErrorsIgnorePattern": "^ignored" }])),
),
];

Tester::new(NoUnusedVars::NAME, pass, fail)
Expand Down
24 changes: 24 additions & 0 deletions crates/oxc_linter/src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,27 @@ source: crates/oxc_linter/src/tester.rs
· ╰── 'e' is declared here
╰────
help: Consider handling this error.

⚠ eslint(no-unused-vars): Variable '_' is caught but never used.
╭─[no_unused_vars.tsx:1:14]
1 │ try {} catch(_) { }
· ┬
· ╰── '_' is declared here
╰────
help: Consider handling this error.

⚠ eslint(no-unused-vars): Variable '_' is caught but never used.
╭─[no_unused_vars.tsx:1:14]
1 │ try {} catch(_) { }
· ┬
· ╰── '_' is declared here
╰────
help: Consider handling this error.

⚠ eslint(no-unused-vars): Variable 'foo' is caught but never used.
╭─[no_unused_vars.tsx:1:14]
1 │ try {} catch(foo) { }
· ─┬─
· ╰── 'foo' is declared here
╰────
help: Consider handling this error.