Skip to content

Commit

Permalink
feat: unused spreads in function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jul 23, 2024
1 parent 6946ffc commit 6883506
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
9 changes: 6 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl NoUnusedVars {
}

// check if res is an array/object unpacking pattern that should be ignored
matches!(decl.id.check_unused_binding_pattern(&*self, symbol), Some(res) if res.is_ignore())
matches!(decl.id.check_unused_binding_pattern(self, symbol), Some(res) if res.is_ignore())
}

pub(super) fn is_allowed_argument<'a>(
Expand Down Expand Up @@ -104,7 +104,7 @@ impl NoUnusedVars {
// arguments inside setters are allowed
// (1 to skip self, then the next should be a function or method) = 2
let maybe_method_or_fn =
semantic.nodes().iter_parents(params_id).nth(2).map(|node| node.kind());
semantic.nodes().iter_parents(params_id).nth(2).map(oxc_semantic::AstNode::kind);
if matches!(
maybe_method_or_fn,
Some(
Expand All @@ -131,7 +131,10 @@ impl NoUnusedVars {
// check if this is a positional argument - unused non-positional
// arguments are never allowed
if param.pattern.kind.is_destructuring_pattern() {
return false;
// allow unpacked parameters that are ignored
let maybe_unused_binding_pattern =
param.pattern.check_unused_binding_pattern(self, symbol);
return maybe_unused_binding_pattern.map_or(false, |res| res.is_ignore());
}

// find the index of the parameter in the parameters list. We want to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ impl<'a> CheckBinding<'a> for ObjectPattern<'a> {
return self
.rest
.as_ref()
.map(|rest| rest.check_unused_binding_pattern(options, symbol))
.flatten();
.and_then(|rest| rest.check_unused_binding_pattern(options, symbol));
}
}

Expand Down Expand Up @@ -150,7 +149,7 @@ impl<'a> CheckBinding<'a> for ArrayPattern<'a> {
.destructured_array_ignore_pattern
.as_ref()
.is_some_and(|pattern| pattern.is_match(symbol.name()));
return res | is_ignorable;
res | is_ignorable
});

if res.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl Tester {

settings.bind(|| {
insta::assert_snapshot!(name, self.snapshot);
})
});
}

fn test_pass(&mut self) {
Expand Down

0 comments on commit 6883506

Please sign in to comment.