refactor(linter) Use is_method_call in more places (#1469)

This commit is contained in:
Cameron 2023-11-21 02:25:25 +00:00 committed by GitHub
parent ce20a02692
commit 32afe1afd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 34 deletions

View file

@ -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<usize>,
max_arg_count: Option<usize>,
) -> 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

View file

@ -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));
}
}

View file

@ -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;
}