refactor(linter): reduce nested if statements in eslint/no_this_before_super (#5485)

This commit is contained in:
IWANABETHATGUY 2024-09-05 13:21:18 +00:00
parent bc7be70077
commit 979c16c486

View file

@ -66,7 +66,7 @@ impl Rule for NoThisBeforeSuper {
for node in semantic.nodes().iter() {
match node.kind() {
AstKind::Function(_) | AstKind::ArrowFunctionExpression(_) => {
if Self::is_wanted_node(node, ctx) {
if Self::is_wanted_node(node, ctx).unwrap_or_default() {
wanted_nodes.push(node);
}
}
@ -133,26 +133,20 @@ impl Rule for NoThisBeforeSuper {
}
impl NoThisBeforeSuper {
fn is_wanted_node(node: &AstNode, ctx: &LintContext<'_>) -> bool {
if let Some(parent) = ctx.nodes().parent_node(node.id()) {
if let AstKind::MethodDefinition(mdef) = parent.kind() {
if matches!(mdef.kind, MethodDefinitionKind::Constructor) {
let parent_2 = ctx.nodes().parent_node(parent.id());
if let Some(parent_2) = parent_2 {
let parent_3 = ctx.nodes().parent_node(parent_2.id());
if let Some(parent_3) = parent_3 {
if let AstKind::Class(c) = parent_3.kind() {
if let Some(super_class) = &c.super_class {
return !matches!(super_class, Expression::NullLiteral(_));
}
}
}
}
fn is_wanted_node(node: &AstNode, ctx: &LintContext<'_>) -> Option<bool> {
let parent = ctx.nodes().parent_node(node.id())?;
if let AstKind::MethodDefinition(mdef) = parent.kind() {
if matches!(mdef.kind, MethodDefinitionKind::Constructor) {
let parent_2 = ctx.nodes().parent_node(parent.id())?;
let parent_3 = ctx.nodes().parent_node(parent_2.id())?;
if let AstKind::Class(c) = parent_3.kind() {
let super_class = c.super_class.as_ref()?;
return Some(!matches!(super_class, Expression::NullLiteral(_)));
}
}
}
false
Some(false)
}
fn analyze(