mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
test(linter/no-unused-vars): add ignored destructuring test cases (#4922)
Add test cases that cover #4888. I can't reproduce the issue this way, so I'll try running oxlint as a CLI instead. These test cases will be useful to have in our repo anyways.
This commit is contained in:
parent
f1fcdde593
commit
c21d735c3d
3 changed files with 50 additions and 34 deletions
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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 }] )),
|
||||
),
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue