test(linter/no-unused-vars): ignored catch parameters (#6555)

Add test cases covering unused/ignored catch parameters after [this discussion](https://discord.com/channels/1079625926024900739/1080712072012238858/1295014826388623414) on discord.
This commit is contained in:
DonIsaac 2024-10-14 19:19:56 +00:00
parent a9260cf6d1
commit badd11ce0d
3 changed files with 65 additions and 2 deletions

View file

@ -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"));
}
}

View file

@ -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)

View file

@ -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.