diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_vars/ignored.rs b/crates/oxc_linter/src/rules/eslint/no_unused_vars/ignored.rs index e6c313af6b0dd..0a8f812fce6a8 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_vars/ignored.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_vars/ignored.rs @@ -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")); + } } diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs b/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs index e8ed79af2ca70..d1a418348c81c 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs @@ -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) diff --git a/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-catch.snap b/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-catch.snap index 715af05cb7ed5..a077cd682ae4d 100644 --- a/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-catch.snap +++ b/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-catch.snap @@ -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.