mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(linter): move shared function from utils to rule (#6127)
I previously extracted the common parts of `prefer_to_be_truthy` and `prefer_to_be_falsy` into `utils`. However, we should place rule-specific logic that isn't general-purpose directly within the respective rules. Co-authored-by: Don Isaac <donald.isaac@gmail.com>
This commit is contained in:
parent
9736aa0112
commit
841317538f
3 changed files with 70 additions and 70 deletions
|
|
@ -1,10 +1,8 @@
|
||||||
use oxc_macros::declare_oxc_lint;
|
use oxc_macros::declare_oxc_lint;
|
||||||
|
|
||||||
use crate::{
|
use crate::{context::LintContext, rule::Rule, utils::collect_possible_jest_call_node};
|
||||||
context::LintContext,
|
|
||||||
rule::Rule,
|
use super::prefer_to_be_truthy::prefer_to_be_simply_bool;
|
||||||
utils::{collect_possible_jest_call_node, prefer_to_be_simply_bool},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct PreferToBeFalsy;
|
pub struct PreferToBeFalsy;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,75 @@
|
||||||
|
use oxc_ast::{
|
||||||
|
ast::{Argument, Expression},
|
||||||
|
AstKind,
|
||||||
|
};
|
||||||
use oxc_macros::declare_oxc_lint;
|
use oxc_macros::declare_oxc_lint;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::LintContext,
|
context::LintContext,
|
||||||
rule::Rule,
|
rule::Rule,
|
||||||
utils::{collect_possible_jest_call_node, prefer_to_be_simply_bool},
|
utils::{
|
||||||
|
collect_possible_jest_call_node, is_equality_matcher,
|
||||||
|
parse_expect_and_typeof_vitest_fn_call, PossibleJestNode,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use oxc_diagnostics::OxcDiagnostic;
|
||||||
|
use oxc_span::Span;
|
||||||
|
|
||||||
|
pub fn prefer_to_be_simply_bool<'a>(
|
||||||
|
possible_vitest_node: &PossibleJestNode<'a, '_>,
|
||||||
|
ctx: &LintContext<'a>,
|
||||||
|
value: bool,
|
||||||
|
) {
|
||||||
|
let node = possible_vitest_node.node;
|
||||||
|
let AstKind::CallExpression(call_expr) = node.kind() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some(vitest_expect_fn_call) =
|
||||||
|
parse_expect_and_typeof_vitest_fn_call(call_expr, possible_vitest_node, ctx)
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some(matcher) = vitest_expect_fn_call.matcher() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if !is_equality_matcher(matcher) || vitest_expect_fn_call.args.len() == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let Some(arg_expr) = vitest_expect_fn_call.args.first().and_then(Argument::as_expression)
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Expression::BooleanLiteral(arg) = arg_expr.get_inner_expression() {
|
||||||
|
if arg.value == value {
|
||||||
|
let span = Span::new(matcher.span.start, call_expr.span.end);
|
||||||
|
|
||||||
|
let is_cmp_mem_expr = match matcher.parent {
|
||||||
|
Some(Expression::ComputedMemberExpression(_)) => true,
|
||||||
|
Some(
|
||||||
|
Expression::StaticMemberExpression(_) | Expression::PrivateFieldExpression(_),
|
||||||
|
) => false,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
|
||||||
|
let call_name = if value { "toBeTruthy" } else { "toBeFalsy" };
|
||||||
|
|
||||||
|
ctx.diagnostic_with_fix(
|
||||||
|
OxcDiagnostic::warn(format!("Use `{call_name}` instead.")).with_label(span),
|
||||||
|
|fixer| {
|
||||||
|
let new_matcher = if is_cmp_mem_expr {
|
||||||
|
format!("[\"{call_name}\"]()")
|
||||||
|
} else {
|
||||||
|
format!("{call_name}()")
|
||||||
|
};
|
||||||
|
fixer.replace(span, new_matcher)
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct PreferToBeTruthy;
|
pub struct PreferToBeTruthy;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,6 @@
|
||||||
use oxc_ast::{
|
use oxc_ast::ast::CallExpression;
|
||||||
ast::{Argument, CallExpression, Expression},
|
|
||||||
AstKind,
|
|
||||||
};
|
|
||||||
use oxc_diagnostics::OxcDiagnostic;
|
|
||||||
use oxc_span::Span;
|
|
||||||
|
|
||||||
use super::{
|
use super::{parse_jest_fn_call, ParsedExpectFnCall, ParsedJestFnCallNew, PossibleJestNode};
|
||||||
is_equality_matcher, parse_jest_fn_call, ParsedExpectFnCall, ParsedJestFnCallNew,
|
|
||||||
PossibleJestNode,
|
|
||||||
};
|
|
||||||
use crate::LintContext;
|
use crate::LintContext;
|
||||||
|
|
||||||
mod valid_vitest_fn;
|
mod valid_vitest_fn;
|
||||||
|
|
@ -27,57 +19,3 @@ pub fn parse_expect_and_typeof_vitest_fn_call<'a>(
|
||||||
ParsedJestFnCallNew::GeneralJest(_) => None,
|
ParsedJestFnCallNew::GeneralJest(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prefer_to_be_simply_bool<'a>(
|
|
||||||
possible_vitest_node: &PossibleJestNode<'a, '_>,
|
|
||||||
ctx: &LintContext<'a>,
|
|
||||||
value: bool,
|
|
||||||
) {
|
|
||||||
let node = possible_vitest_node.node;
|
|
||||||
let AstKind::CallExpression(call_expr) = node.kind() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(vitest_expect_fn_call) =
|
|
||||||
parse_expect_and_typeof_vitest_fn_call(call_expr, possible_vitest_node, ctx)
|
|
||||||
else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(matcher) = vitest_expect_fn_call.matcher() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
if !is_equality_matcher(matcher) || vitest_expect_fn_call.args.len() == 0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let Some(arg_expr) = vitest_expect_fn_call.args.first().and_then(Argument::as_expression)
|
|
||||||
else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Expression::BooleanLiteral(arg) = arg_expr.get_inner_expression() {
|
|
||||||
if arg.value == value {
|
|
||||||
let span = Span::new(matcher.span.start, call_expr.span.end);
|
|
||||||
|
|
||||||
let is_cmp_mem_expr = match matcher.parent {
|
|
||||||
Some(Expression::ComputedMemberExpression(_)) => true,
|
|
||||||
Some(
|
|
||||||
Expression::StaticMemberExpression(_) | Expression::PrivateFieldExpression(_),
|
|
||||||
) => false,
|
|
||||||
_ => return,
|
|
||||||
};
|
|
||||||
|
|
||||||
let call_name = if value { "toBeTruthy" } else { "toBeFalsy" };
|
|
||||||
|
|
||||||
ctx.diagnostic_with_fix(
|
|
||||||
OxcDiagnostic::warn(format!("Use `{call_name}` instead.")).with_label(span),
|
|
||||||
|fixer| {
|
|
||||||
let new_matcher = if is_cmp_mem_expr {
|
|
||||||
format!("[\"{call_name}\"]()")
|
|
||||||
} else {
|
|
||||||
format!("{call_name}()")
|
|
||||||
};
|
|
||||||
fixer.replace(span, new_matcher)
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue