Skip to content

Commit

Permalink
refactor(linter): use Option to reduce nested level in `eslint/getter…
Browse files Browse the repository at this point in the history
…-return` (#4814)

1. Simplify `is_wanted_node`
  • Loading branch information
IWANABETHATGUY authored Aug 10, 2024
1 parent d7fce58 commit 15a0fd4
Showing 1 changed file with 48 additions and 54 deletions.
102 changes: 48 additions & 54 deletions crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,72 +119,66 @@ impl GetterReturn {
}

/// Checks whether it is necessary to check the node
fn is_wanted_node(node: &AstNode, ctx: &LintContext<'_>) -> bool {
if let Some(parent) = ctx.nodes().parent_node(node.id()) {
match parent.kind() {
AstKind::MethodDefinition(mdef) => {
if matches!(mdef.kind, MethodDefinitionKind::Get) {
return true;
}
fn is_wanted_node(node: &AstNode, ctx: &LintContext<'_>) -> Option<bool> {
let parent = ctx.nodes().parent_node(node.id())?;
match parent.kind() {
AstKind::MethodDefinition(mdef) => {
if matches!(mdef.kind, MethodDefinitionKind::Get) {
return Some(true);
}
}
AstKind::ObjectProperty(ObjectProperty { kind, key: prop_key, .. }) => {
if matches!(kind, PropertyKind::Get) {
return Some(true);
}
AstKind::ObjectProperty(ObjectProperty { kind, key: prop_key, .. }) => {
if matches!(kind, PropertyKind::Get) {
return true;
if prop_key.name().is_some_and(|key| key != "get") {
return Some(false);
}

let parent_2 = ctx.nodes().parent_node(parent.id())?;
let parent_3 = ctx.nodes().parent_node(parent_2.id())?;
let parent_4 = ctx.nodes().parent_node(parent_3.id())?;
// handle (X())
match parent_4.kind() {
AstKind::ParenthesizedExpression(p) => {
if Self::handle_paren_expr(&p.expression) {
return Some(true);
}
}
if prop_key.name().is_some_and(|key| key != "get") {
return false;
AstKind::CallExpression(ce) => {
if Self::handle_actual_expression(&ce.callee) {
return Some(true);
}
}
_ => {}
}

if let Some(parent_2) = ctx.nodes().parent_node(parent.id()) {
if let Some(parent_3) = ctx.nodes().parent_node(parent_2.id()) {
if let Some(parent_4) = ctx.nodes().parent_node(parent_3.id()) {
// handle (X())
match parent_4.kind() {
AstKind::ParenthesizedExpression(p) => {
if Self::handle_paren_expr(&p.expression) {
return true;
}
}
AstKind::CallExpression(ce) => {
if Self::handle_actual_expression(&ce.callee) {
return true;
}
}
_ => {}
}

if let Some(parent_5) = ctx.nodes().parent_node(parent_4.id()) {
if let Some(parent_6) = ctx.nodes().parent_node(parent_5.id()) {
match parent_6.kind() {
AstKind::ParenthesizedExpression(p) => {
if Self::handle_paren_expr(&p.expression) {
return true;
}
}
AstKind::CallExpression(ce) => {
if Self::handle_actual_expression(&ce.callee) {
return true;
}
}
_ => {
return false;
}
};
}
}
}
let parent_5 = ctx.nodes().parent_node(parent_4.id())?;
let parent_6 = ctx.nodes().parent_node(parent_5.id())?;
match parent_6.kind() {
AstKind::ParenthesizedExpression(p) => {
if Self::handle_paren_expr(&p.expression) {
return Some(true);
}
}
}
_ => {}
AstKind::CallExpression(ce) => {
if Self::handle_actual_expression(&ce.callee) {
return Some(true);
}
}
_ => {
return Some(false);
}
};
}
_ => {}
}

false
Some(false)
}

fn run_diagnostic<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>, span: Span) {
if !Self::is_wanted_node(node, ctx) {
if !Self::is_wanted_node(node, ctx).unwrap_or_default() {
return;
}

Expand Down

0 comments on commit 15a0fd4

Please sign in to comment.