Skip to content

Commit 8413175

Browse files
shulaodaDonIsaac
andauthoredOct 4, 2024··
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>
1 parent 9736aa0 commit 8413175

File tree

3 files changed

+70
-70
lines changed

3 files changed

+70
-70
lines changed
 

‎crates/oxc_linter/src/rules/vitest/prefer_to_be_falsy.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use oxc_macros::declare_oxc_lint;
22

3-
use crate::{
4-
context::LintContext,
5-
rule::Rule,
6-
utils::{collect_possible_jest_call_node, prefer_to_be_simply_bool},
7-
};
3+
use crate::{context::LintContext, rule::Rule, utils::collect_possible_jest_call_node};
4+
5+
use super::prefer_to_be_truthy::prefer_to_be_simply_bool;
86

97
#[derive(Debug, Default, Clone)]
108
pub struct PreferToBeFalsy;

‎crates/oxc_linter/src/rules/vitest/prefer_to_be_truthy.rs

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,75 @@
1+
use oxc_ast::{
2+
ast::{Argument, Expression},
3+
AstKind,
4+
};
15
use oxc_macros::declare_oxc_lint;
26

37
use crate::{
48
context::LintContext,
59
rule::Rule,
6-
utils::{collect_possible_jest_call_node, prefer_to_be_simply_bool},
10+
utils::{
11+
collect_possible_jest_call_node, is_equality_matcher,
12+
parse_expect_and_typeof_vitest_fn_call, PossibleJestNode,
13+
},
714
};
815

16+
use oxc_diagnostics::OxcDiagnostic;
17+
use oxc_span::Span;
18+
19+
pub fn prefer_to_be_simply_bool<'a>(
20+
possible_vitest_node: &PossibleJestNode<'a, '_>,
21+
ctx: &LintContext<'a>,
22+
value: bool,
23+
) {
24+
let node = possible_vitest_node.node;
25+
let AstKind::CallExpression(call_expr) = node.kind() else {
26+
return;
27+
};
28+
let Some(vitest_expect_fn_call) =
29+
parse_expect_and_typeof_vitest_fn_call(call_expr, possible_vitest_node, ctx)
30+
else {
31+
return;
32+
};
33+
let Some(matcher) = vitest_expect_fn_call.matcher() else {
34+
return;
35+
};
36+
if !is_equality_matcher(matcher) || vitest_expect_fn_call.args.len() == 0 {
37+
return;
38+
}
39+
let Some(arg_expr) = vitest_expect_fn_call.args.first().and_then(Argument::as_expression)
40+
else {
41+
return;
42+
};
43+
44+
if let Expression::BooleanLiteral(arg) = arg_expr.get_inner_expression() {
45+
if arg.value == value {
46+
let span = Span::new(matcher.span.start, call_expr.span.end);
47+
48+
let is_cmp_mem_expr = match matcher.parent {
49+
Some(Expression::ComputedMemberExpression(_)) => true,
50+
Some(
51+
Expression::StaticMemberExpression(_) | Expression::PrivateFieldExpression(_),
52+
) => false,
53+
_ => return,
54+
};
55+
56+
let call_name = if value { "toBeTruthy" } else { "toBeFalsy" };
57+
58+
ctx.diagnostic_with_fix(
59+
OxcDiagnostic::warn(format!("Use `{call_name}` instead.")).with_label(span),
60+
|fixer| {
61+
let new_matcher = if is_cmp_mem_expr {
62+
format!("[\"{call_name}\"]()")
63+
} else {
64+
format!("{call_name}()")
65+
};
66+
fixer.replace(span, new_matcher)
67+
},
68+
);
69+
}
70+
}
71+
}
72+
973
#[derive(Debug, Default, Clone)]
1074
pub struct PreferToBeTruthy;
1175

‎crates/oxc_linter/src/utils/vitest.rs

+2-64
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
use oxc_ast::{
2-
ast::{Argument, CallExpression, Expression},
3-
AstKind,
4-
};
5-
use oxc_diagnostics::OxcDiagnostic;
6-
use oxc_span::Span;
1+
use oxc_ast::ast::CallExpression;
72

8-
use super::{
9-
is_equality_matcher, parse_jest_fn_call, ParsedExpectFnCall, ParsedJestFnCallNew,
10-
PossibleJestNode,
11-
};
3+
use super::{parse_jest_fn_call, ParsedExpectFnCall, ParsedJestFnCallNew, PossibleJestNode};
124
use crate::LintContext;
135

146
mod valid_vitest_fn;
@@ -27,57 +19,3 @@ pub fn parse_expect_and_typeof_vitest_fn_call<'a>(
2719
ParsedJestFnCallNew::GeneralJest(_) => None,
2820
}
2921
}
30-
31-
pub fn prefer_to_be_simply_bool<'a>(
32-
possible_vitest_node: &PossibleJestNode<'a, '_>,
33-
ctx: &LintContext<'a>,
34-
value: bool,
35-
) {
36-
let node = possible_vitest_node.node;
37-
let AstKind::CallExpression(call_expr) = node.kind() else {
38-
return;
39-
};
40-
let Some(vitest_expect_fn_call) =
41-
parse_expect_and_typeof_vitest_fn_call(call_expr, possible_vitest_node, ctx)
42-
else {
43-
return;
44-
};
45-
let Some(matcher) = vitest_expect_fn_call.matcher() else {
46-
return;
47-
};
48-
if !is_equality_matcher(matcher) || vitest_expect_fn_call.args.len() == 0 {
49-
return;
50-
}
51-
let Some(arg_expr) = vitest_expect_fn_call.args.first().and_then(Argument::as_expression)
52-
else {
53-
return;
54-
};
55-
56-
if let Expression::BooleanLiteral(arg) = arg_expr.get_inner_expression() {
57-
if arg.value == value {
58-
let span = Span::new(matcher.span.start, call_expr.span.end);
59-
60-
let is_cmp_mem_expr = match matcher.parent {
61-
Some(Expression::ComputedMemberExpression(_)) => true,
62-
Some(
63-
Expression::StaticMemberExpression(_) | Expression::PrivateFieldExpression(_),
64-
) => false,
65-
_ => return,
66-
};
67-
68-
let call_name = if value { "toBeTruthy" } else { "toBeFalsy" };
69-
70-
ctx.diagnostic_with_fix(
71-
OxcDiagnostic::warn(format!("Use `{call_name}` instead.")).with_label(span),
72-
|fixer| {
73-
let new_matcher = if is_cmp_mem_expr {
74-
format!("[\"{call_name}\"]()")
75-
} else {
76-
format!("{call_name}()")
77-
};
78-
fixer.replace(span, new_matcher)
79-
},
80-
);
81-
}
82-
}
83-
}

0 commit comments

Comments
 (0)
Please sign in to comment.