From 979c16c486358f1bd73874a1ad9cc042f34e1792 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY <17974631+IWANABETHATGUY@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:21:18 +0000 Subject: [PATCH] refactor(linter): reduce nested if statements in eslint/no_this_before_super (#5485) --- .../src/rules/eslint/no_this_before_super.rs | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs b/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs index 60851014412f3..b77a80e5565e5 100644 --- a/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs +++ b/crates/oxc_linter/src/rules/eslint/no_this_before_super.rs @@ -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 { + 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(