diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_vars/options.rs b/crates/oxc_linter/src/rules/eslint/no_unused_vars/options.rs index 76859f3ac0fbf..ffbc6ddf065b9 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_vars/options.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_vars/options.rs @@ -566,6 +566,19 @@ mod tests { assert!(rule.args_ignore_pattern.unwrap().as_str() == "^_"); } + #[test] + fn test_ignore_rest_siblings_only() { + let rule: NoUnusedVarsOptions = json!([ + { + "ignoreRestSiblings": true, + } + ]) + .into(); + assert!(rule.ignore_rest_siblings); + // an options object is provided, so no default pattern is set. + assert!(rule.vars_ignore_pattern.is_none()); + } + #[test] fn test_options_from_null() { let opts = NoUnusedVarsOptions::from(json!(null)); 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 17b79051ee449..e38c0948a2fc7 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 @@ -4,28 +4,6 @@ use super::NoUnusedVars; use crate::{tester::Tester, FixKind, RuleMeta as _}; use serde_json::json; -#[test] -fn test_debug() { - let pass: Vec<&'static str> = vec![]; - let fail = vec![]; - let fix = vec![ - // ( - // "const { foo: fooBar, baz } = obj; f(baz);", - // "const { baz } = obj; f(baz);", - // None, - // FixKind::DangerousSuggestion, - // ), - ("const [a, b] = arr; f(a)", "const [a] = arr; f(a)", None, FixKind::DangerousSuggestion), - // ( - // "let x = 1; x = 2;", - // "let x = 1; x = 2;", - // Some(json!( [{ "varsIgnorePattern": "^tooCompli[cated]" }] )), - // FixKind::DangerousFix, - // ), - ]; - Tester::new(NoUnusedVars::NAME, pass, fail).expect_fix(fix).test(); -} - #[test] fn test_vars_simple() { let pass = vec![ @@ -281,14 +259,39 @@ fn test_vars_reassignment() { #[test] fn test_vars_destructure() { let pass = vec![ - // ("const { a, ...rest } = obj; console.log(rest)", Some(json![{ "ignoreRestSiblings": true }])) + ( + "const { a, ...rest } = obj; console.log(rest)", + Some(json![[{ "ignoreRestSiblings": true }]]), + ), + ( + "const { a, ...rest } = obj; console.log(rest)", + Some(json!( [{ "ignoreRestSiblings": true, "vars": "all" }] )), + ), + ( + "const { a, ...rest } = obj; console.log(rest)", + Some(json!( [{ "ignoreRestSiblings": true, "vars": "all" }] )), + ), + // https://github.com/oxc-project/oxc/issues/4888 + ( + "const { text, ...dbEntry } = entry; return doSomething({ ...dbEntry, someOtherProp });", + Some(json!([{ + "args": "none", + "caughtErrors": "none", + "ignoreRestSiblings": true, + "vars": "all" + }])) + ) ]; let fail = vec![ - ("const { a, ...rest } = obj", Some(json![{ "ignoreRestSiblings": true }])), - ("const [a, ...rest] = arr", Some(json![{ "ignoreRestSiblings": true }])), + ("const { a, ...rest } = obj", Some(json!( [{ "ignoreRestSiblings": true }] ))), + ("const [a, ...rest] = arr", Some(json!( [{ "ignoreRestSiblings": true }] ))), ( "const { a: { b }, ...rest } = obj; console.log(a)", - Some(json![{ "ignoreRestSiblings": true }]), + Some(json!( [{ "ignoreRestSiblings": true }] )), + ), + ( + "const { a: { b }, ...rest } = obj; console.log(rest)", + Some(json!( [{ "ignoreRestSiblings": true }] )), ), ]; diff --git a/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-destructure.snap b/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-destructure.snap index 81cb1df9e6c8f..0b3bcf90c4d89 100644 --- a/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-destructure.snap +++ b/crates/oxc_linter/src/snapshots/no_unused_vars@oxc-vars-destructure.snap @@ -1,14 +1,6 @@ --- source: crates/oxc_linter/src/tester.rs --- - ⚠ eslint(no-unused-vars): Variable 'a' is declared but never used. - ╭─[no_unused_vars.tsx:1:9] - 1 │ const { a, ...rest } = obj - · ┬ - · ╰── 'a' is declared here - ╰──── - help: Consider removing this declaration. - ⚠ eslint(no-unused-vars): Variable 'rest' is declared but never used. ╭─[no_unused_vars.tsx:1:15] 1 │ const { a, ...rest } = obj @@ -48,3 +40,11 @@ source: crates/oxc_linter/src/tester.rs · ╰── 'rest' is declared here ╰──── help: Consider removing this declaration. + + ⚠ eslint(no-unused-vars): Variable 'b' is declared but never used. + ╭─[no_unused_vars.tsx:1:14] + 1 │ const { a: { b }, ...rest } = obj; console.log(rest) + · ┬ + · ╰── 'b' is declared here + ╰──── + help: Consider removing this declaration.