forked from oxc-project/oxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefer_to_be_truthy.rs
160 lines (144 loc) · 5.01 KB
/
prefer_to_be_truthy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use oxc_ast::{
ast::{Argument, Expression},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
context::LintContext,
rule::Rule,
utils::{
collect_possible_jest_call_node, is_equality_matcher,
parse_expect_and_typeof_vitest_fn_call, PossibleJestNode,
},
};
fn use_to_be_truthy(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Use `toBeTruthy` instead.").with_label(span0)
}
#[derive(Debug, Default, Clone)]
pub struct PreferToBeTruthy;
declare_oxc_lint!(
/// ### What it does
///
/// This rule warns when `toBe(true)` is used with `expect` or `expectTypeOf`. With `--fix`, it will be replaced with `toBeTruthy()`.
///
/// ### Examples
///
/// ```javascript
/// // bad
/// expect(foo).toBe(true)
/// expectTypeOf(foo).toBe(true)
///
/// // good
/// expect(foo).toBeTruthy()
/// expectTypeOf(foo).toBeTruthy()
/// ```
PreferToBeTruthy,
style,
fix
);
impl Rule for PreferToBeTruthy {
fn run_once(&self, ctx: &LintContext) {
for possible_vitest_node in &collect_possible_jest_call_node(ctx) {
Self::run(possible_vitest_node, ctx);
}
}
}
impl PreferToBeTruthy {
fn run<'a>(possible_vitest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>) {
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 {
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,
};
ctx.diagnostic_with_fix(use_to_be_truthy(span), |fixer| {
let new_matcher =
if is_cmp_mem_expr { "[\"toBeTruthy\"]()" } else { "toBeTruthy()" };
fixer.replace(span, new_matcher)
});
}
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
"[].push(true)",
r#"expect("something");"#,
"expect(true).toBeTrue();",
"expect(false).toBeTrue();",
"expect(fal,se).toBeFalse();",
"expect(true).toBeFalse();",
"expect(value).toEqual();",
"expect(value).not.toBeTrue();",
"expect(value).not.toEqual();",
"expect(value).toBe(undefined);",
"expect(value).not.toBe(undefined);",
"expect(true).toBe(false)",
"expect(value).toBe();",
"expect(true).toMatchSnapshot();",
r#"expect("a string").toMatchSnapshot(true);"#,
r#"expect("a string").not.toMatchSnapshot();"#,
"expect(something).toEqual('a string');",
"expect(true).toBe",
"expectTypeOf(true).toBe()",
];
let fail = vec![
"expect(false).toBe(true);",
"expectTypeOf(false).toBe(true);",
"expect(wasSuccessful).toEqual(true);",
"expect(fs.existsSync('/path/to/file')).toStrictEqual(true);",
r#"expect("a string").not.toBe(true);"#,
r#"expect("a string").not.toEqual(true);"#,
r#"expectTypeOf("a string").not.toStrictEqual(true);"#,
];
let fix = vec![
("expect(false).toBe(true);", "expect(false).toBeTruthy();", None),
("expectTypeOf(false).toBe(true);", "expectTypeOf(false).toBeTruthy();", None),
("expect(wasSuccessful).toEqual(true);", "expect(wasSuccessful).toBeTruthy();", None),
(
"expect(fs.existsSync('/path/to/file')).toStrictEqual(true);",
"expect(fs.existsSync('/path/to/file')).toBeTruthy();",
None,
),
(r#"expect("a string").not.toBe(true);"#, r#"expect("a string").not.toBeTruthy();"#, None),
(
r#"expect("a string").not.toEqual(true);"#,
r#"expect("a string").not.toBeTruthy();"#,
None,
),
(
r#"expectTypeOf("a string").not.toStrictEqual(true);"#,
r#"expectTypeOf("a string").not.toBeTruthy();"#,
None,
),
];
Tester::new(PreferToBeTruthy::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}