Skip to content

Commit

Permalink
test(linter/no-unused-vars): ignored catch parameters (#6555)
Browse files Browse the repository at this point in the history
Add test cases covering unused/ignored catch parameters after [this discussion](https://discord.com/channels/1079625926024900739/1080712072012238858/1295014826388623414) on discord.
  • Loading branch information
DonIsaac committed Oct 14, 2024
1 parent a9260cf commit badd11c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
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/no_unused_vars@oxc-vars-catch.snap
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]
1try {} 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]
1try {} 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]
1try {} catch(foo) { }
· ─┬─
· ╰── 'foo' is declared here
╰────
help: Consider handling this error.

0 comments on commit badd11c

Please sign in to comment.