Skip to content

Commit

Permalink
refactor(linter): use Expression::is_super (#7850)
Browse files Browse the repository at this point in the history
Simplify code by using `Expression::is_super` which was introduced in #7831.

Also remove a lifetime bound which is unnecessary and replace use of `bool::then` (which I always find hard to read) with more explicit code.
  • Loading branch information
overlookmotel committed Dec 13, 2024
1 parent 5d42df8 commit 8f3444f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_useless_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,21 @@ fn is_overriding(params: &FormalParameters) -> bool {
params.items.iter().any(|param| param.r#override)
}

/// Check if a function body only contains a single super call. Ignores directives.
/// Check if a function body only contains a single `super()` call. Ignores directives.
///
/// Returns the call expression if the body contains a single super call, otherwise [`None`].
fn is_single_super_call<'f, 'a: 'f>(body: &'f FunctionBody<'a>) -> Option<&'f CallExpression<'a>> {
/// Returns the call expression if the body contains a single `super()` call, otherwise [`None`].
fn is_single_super_call<'a, 'f>(body: &'f FunctionBody<'a>) -> Option<&'f CallExpression<'a>> {
if body.statements.len() != 1 {
return None;
}
let Statement::ExpressionStatement(expr) = &body.statements[0] else { return None };
let Expression::CallExpression(call) = &expr.expression else { return None };

matches!(call.callee, Expression::Super(_)).then(|| call.as_ref())
if call.callee.is_super() {
Some(call)
} else {
None
}
}

/// Returns `false` if any parameter is an array/object unpacking binding or an
Expand Down

0 comments on commit 8f3444f

Please sign in to comment.