From 32afe1afd095cd8300849e9f31e9d9daf638a693 Mon Sep 17 00:00:00 2001 From: Cameron Date: Tue, 21 Nov 2023 02:25:25 +0000 Subject: [PATCH] refactor(linter) Use `is_method_call` in more places (#1469) --- crates/oxc_linter/src/ast_util.rs | 11 +++--- .../rules/unicorn/prefer_array_flat_map.rs | 38 ++++++------------- .../src/rules/unicorn/prefer_array_some.rs | 4 +- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/crates/oxc_linter/src/ast_util.rs b/crates/oxc_linter/src/ast_util.rs index afafea7fe..79e2ef3e9 100644 --- a/crates/oxc_linter/src/ast_util.rs +++ b/crates/oxc_linter/src/ast_util.rs @@ -316,7 +316,7 @@ pub fn extract_regex_flags<'a>( pub fn is_method_call<'a>( call_expr: &CallExpression<'a>, - methods: &[&'a str], + methods: Option<&[&'a str]>, min_arg_count: Option, max_arg_count: Option, ) -> bool { @@ -337,10 +337,11 @@ pub fn is_method_call<'a>( return false; }; - let Some(static_property_name) = member_expr.static_property_name() else { return false }; - - if !methods.contains(&static_property_name) { - return false; + if let Some(methods) = methods { + let Some(static_property_name) = member_expr.static_property_name() else { return false }; + if !methods.contains(&static_property_name) { + return false; + } } true diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_array_flat_map.rs b/crates/oxc_linter/src/rules/unicorn/prefer_array_flat_map.rs index 556109875..c90cbe28d 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_array_flat_map.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_array_flat_map.rs @@ -9,7 +9,7 @@ use oxc_diagnostics::{ use oxc_macros::declare_oxc_lint; use oxc_span::Span; -use crate::{context::LintContext, rule::Rule, AstNode}; +use crate::{ast_util::is_method_call, context::LintContext, rule::Rule, AstNode}; #[derive(Debug, Error, Diagnostic)] #[error("eslint-plugin-unicorn(prefer-array-flat-map): `Array.flatMap` performs `Array.map` and `Array.flat` in one step.")] @@ -46,14 +46,16 @@ impl Rule for PreferArrayFlatMap { return; } - let callee = &flat_call_expr.callee.without_parenthesized(); - - if let Expression::MemberExpression(v) = callee { - if let Some(static_property_name) = v.static_property_name() { - if static_property_name != "flat" { - return; - } - } + if !is_method_call(flat_call_expr, Some(&["flat"]), None, None) { + return; + } + let Expression::MemberExpression(member_expr) = &flat_call_expr.callee else { return }; + let Expression::CallExpression(call_expr) = &member_expr.object().without_parenthesized() + else { + return; + }; + if !is_method_call(call_expr, Some(&["map"]), None, None) { + return; } if let Some(first_arg) = flat_call_expr.arguments.first() { @@ -66,24 +68,6 @@ impl Rule for PreferArrayFlatMap { } } - let Expression::MemberExpression(member_expr) = callee else { return }; - - let Expression::CallExpression(map_call_expr) = - member_expr.object().without_parenthesized() - else { - return; - }; - - if let Expression::MemberExpression(map_member_expr) = - &map_call_expr.callee.without_parenthesized() - { - if let Some(property_name) = map_member_expr.static_property_name() { - if property_name != "map" { - return; - } - } - }; - ctx.diagnostic(PreferArrayFlatMapDiagnostic(flat_call_expr.span)); } } diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs b/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs index 6053a9f9f..cb64bb314 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_array_some.rs @@ -55,7 +55,7 @@ impl Rule for PreferArraySome { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { match node.kind() { AstKind::CallExpression(call_expr) => { - if !is_method_call(call_expr, &["find", "findLast"], Some(1), Some(2)) { + if !is_method_call(call_expr, Some(&["find", "findLast"]), Some(1), Some(2)) { return; } @@ -103,7 +103,7 @@ impl Rule for PreferArraySome { return; }; - if !is_method_call(left_call_expr, &["filter"], None, None) { + if !is_method_call(left_call_expr, Some(&["filter"]), None, None) { return; }