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
13 changes: 13 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
55 changes: 29 additions & 26 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 @@ -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![
Expand Down Expand Up @@ -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 }] )),
),
];

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.