Skip to content

Commit

Permalink
refactor(linter): move shared function from utils to rule (#6127)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
shulaoda and DonIsaac authored Oct 4, 2024
1 parent 9736aa0 commit 8413175
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 70 deletions.
8 changes: 3 additions & 5 deletions crates/oxc_linter/src/rules/vitest/prefer_to_be_falsy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use oxc_macros::declare_oxc_lint;

use crate::{
context::LintContext,
rule::Rule,
utils::{collect_possible_jest_call_node, prefer_to_be_simply_bool},
};
use crate::{context::LintContext, rule::Rule, utils::collect_possible_jest_call_node};

use super::prefer_to_be_truthy::prefer_to_be_simply_bool;

#[derive(Debug, Default, Clone)]
pub struct PreferToBeFalsy;
Expand Down
66 changes: 65 additions & 1 deletion crates/oxc_linter/src/rules/vitest/prefer_to_be_truthy.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,75 @@
use oxc_ast::{
ast::{Argument, Expression},
AstKind,
};
use oxc_macros::declare_oxc_lint;

use crate::{
context::LintContext,
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)]
pub struct PreferToBeTruthy;

Expand Down
66 changes: 2 additions & 64 deletions crates/oxc_linter/src/utils/vitest.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use oxc_ast::{
ast::{Argument, CallExpression, Expression},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;
use oxc_ast::ast::CallExpression;

use super::{
is_equality_matcher, parse_jest_fn_call, ParsedExpectFnCall, ParsedJestFnCallNew,
PossibleJestNode,
};
use super::{parse_jest_fn_call, ParsedExpectFnCall, ParsedJestFnCallNew, PossibleJestNode};
use crate::LintContext;

mod valid_vitest_fn;
Expand All @@ -27,57 +19,3 @@ pub fn parse_expect_and_typeof_vitest_fn_call<'a>(
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)
},
);
}
}
}

0 comments on commit 8413175

Please sign in to comment.