Skip to content

Commit

Permalink
wip: binding patterns, IIFEs, and fn/variable declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Jul 23, 2024
1 parent 0664084 commit 6946ffc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
65 changes: 65 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,72 @@ use oxc_semantic::Semantic;
use super::binding_pattern::CheckBinding;
use super::{options::ArgsOption, NoUnusedVars, Symbol};

impl<'s, 'a> Symbol<'s, 'a> {
/// Returns `true` if this function is a callback passed to another function
pub fn is_function_callback(&self) -> bool {
debug_assert!(self.declaration().kind().is_function_like());

for parent in self.iter_parents() {
match parent.kind() {
AstKind::MemberExpression(_) | AstKind::ParenthesizedExpression(_) => {
continue;
}
AstKind::CallExpression(_) => {
return true;
}
_ => {
return false;
}
}
}

false
}

/// ```ts
/// var foo = function foo() {};
/// // ^^^ does not have a read reference, needs manual check
/// foo()
/// ```
pub fn is_function_assigned_to_same_name_variable(&self) -> bool {
debug_assert!(self.declaration().kind().is_function_like());

for parent in self.iter_parents() {
match parent.kind() {
AstKind::MemberExpression(_) | AstKind::ParenthesizedExpression(_) => {
continue;
}
AstKind::VariableDeclarator(decl) => {
return decl
.id
.get_binding_identifier()
.is_some_and(|id| id.name == self.name())
}
_ => {
return false;
}
}
}

false
}
}
impl NoUnusedVars {
/// Returns `true` if this unused variable declaration should be allowed
/// (i.e. not reported)
pub(super) fn is_allowed_variable_declaration<'a>(
&self,
symbol: &Symbol<'_, 'a>,
decl: &VariableDeclarator<'a>,
) -> bool {
if decl.kind.is_var() && self.vars.is_local() && symbol.is_root() {
return true;
}

// 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())
}

pub(super) fn is_allowed_argument<'a>(
&self,
semantic: &Semantic<'a>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,17 @@ impl<'a> CheckBinding<'a> for ArrayPattern<'a> {
let Some(el) = el.as_ref() else {
continue;
};
let res = el.check_unused_binding_pattern(options, symbol);
// const [a, _b, c] = arr; console.log(a, b)
// here, res will contain data for _b, and we want to check if it
// can be ignored (if it matches destructuredArrayIgnorePattern)
let res = el.check_unused_binding_pattern(options, symbol).map(|res| {
let is_ignorable = options
.destructured_array_ignore_pattern
.as_ref()
.is_some_and(|pattern| pattern.is_match(symbol.name()));
return res | is_ignorable;
});

if res.is_some() {
return res;
}
Expand Down
26 changes: 17 additions & 9 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ impl Rule for NoUnusedVars {
}
}
AstKind::VariableDeclarator(decl) => {
if decl.kind.is_var() && self.vars.is_local() && symbol.is_root() {
if self.is_allowed_variable_declaration(&symbol, decl) {
return;
}
};
let report =
if let Some(last_write) = symbol.references().rev().find(|r| r.is_write()) {
diagnostic::assign(&symbol, last_write.span())
Expand All @@ -195,14 +195,22 @@ impl Rule for NoUnusedVars {
}
ctx.diagnostic(diagnostic::param(&symbol));
}
AstKind::CatchParameter(_) => ctx.diagnostic(diagnostic::declared(&symbol)),
unknown => {
debug_assert!(
false,
"NoUnusedVars::run_on_symbol - unhandled AstKind: {:?}",
unknown.debug_name()
)
AstKind::Function(_) => {
if symbol.is_function_callback()
|| symbol.is_function_assigned_to_same_name_variable()
{
return;
}
ctx.diagnostic(diagnostic::declared(&symbol));
}
_ => ctx.diagnostic(diagnostic::declared(&symbol)),
// unknown => {
// debug_assert!(
// false,
// "NoUnusedVars::run_on_symbol - unhandled AstKind: {:?}",
// unknown.debug_name()
// )
// }
};
// match (is_ignored, is_used) {
// (true, true) => {
Expand Down

0 comments on commit 6946ffc

Please sign in to comment.