-
-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use crate::{ | ||
context::LintContext, | ||
rule::Rule, | ||
utils::{ | ||
collect_possible_jest_call_node, is_type_of_jest_fn_call, parse_expect_jest_fn_call, | ||
JestFnKind, PossibleJestNode, | ||
}, | ||
}; | ||
|
||
use oxc_ast::AstKind; | ||
use oxc_diagnostics::{ | ||
miette::{self, Diagnostic}, | ||
thiserror::Error, | ||
}; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::{Atom, Span}; | ||
|
||
#[derive(Debug, Error, Diagnostic)] | ||
#[error("eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`.")] | ||
#[diagnostic(severity(warning), help("Prefer {0:?}With(/* expected args */)"))] | ||
struct PreferCalledWithDiagnostic(Atom, #[label] pub Span); | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct PreferCalledWith; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```javascript | ||
/// | ||
/// // valid | ||
/// expect(noArgsFunction).toBeCalledWith(); | ||
/// expect(roughArgsFunction).toBeCalledWith(expect.anything(), expect.any(Date)); | ||
/// expect(anyArgsFunction).toBeCalledTimes(1); | ||
/// expect(uncalledFunction).not.toBeCalled(); | ||
/// | ||
/// // invalid | ||
/// expect(someFunction).toBeCalled(); | ||
/// expect(someFunction).toHaveBeenCalled(); | ||
/// ``` | ||
/// | ||
PreferCalledWith, | ||
style, | ||
); | ||
|
||
impl Rule for PreferCalledWith { | ||
fn run_once(&self, ctx: &LintContext<'_>) { | ||
for possible_jest_node in &collect_possible_jest_call_node(ctx) { | ||
Self::run(possible_jest_node, ctx); | ||
} | ||
} | ||
} | ||
|
||
impl PreferCalledWith { | ||
pub fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>) { | ||
let node = possible_jest_node.node; | ||
let AstKind::CallExpression(call_expr) = node.kind() else { | ||
return; | ||
}; | ||
|
||
if !is_type_of_jest_fn_call(call_expr, possible_jest_node, ctx, &[JestFnKind::Expect]) { | ||
return; | ||
} | ||
|
||
let Some(jest_fn_call) = parse_expect_jest_fn_call(call_expr, possible_jest_node, ctx) | ||
else { | ||
return; | ||
}; | ||
|
||
let mut has_not_modifier = false; | ||
for modifier in jest_fn_call.modifiers() { | ||
if let Some(modifier_name) = modifier.name() { | ||
if modifier_name.eq("not") { | ||
has_not_modifier = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if has_not_modifier { | ||
return; | ||
} | ||
|
||
if let Some(matcher_property) = jest_fn_call.matcher() { | ||
if let Some(matcher_name) = matcher_property.name() { | ||
if matcher_name == "toBeCalled" || matcher_name == "toHaveBeenCalled" { | ||
ctx.diagnostic(PreferCalledWithDiagnostic( | ||
Atom::from(matcher_name), | ||
matcher_property.span, | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
("expect(fn).toBeCalledWith();", None), | ||
("expect(fn).toHaveBeenCalledWith();", None), | ||
("expect(fn).toBeCalledWith(expect.anything());", None), | ||
("expect(fn).toHaveBeenCalledWith(expect.anything());", None), | ||
("expect(fn).not.toBeCalled();", None), | ||
("expect(fn).rejects.not.toBeCalled();", None), | ||
("expect(fn).not.toHaveBeenCalled();", None), | ||
("expect(fn).not.toBeCalledWith();", None), | ||
("expect(fn).not.toHaveBeenCalledWith();", None), | ||
("expect(fn).resolves.not.toHaveBeenCalledWith();", None), | ||
("expect(fn).toBeCalledTimes(0);", None), | ||
("expect(fn).toHaveBeenCalledTimes(0);", None), | ||
("expect(fn);", None), | ||
]; | ||
|
||
let fail = vec![ | ||
("expect(fn).toBeCalled();", None), | ||
("expect(fn).resolves.toBeCalled();", None), | ||
("expect(fn).toHaveBeenCalled();", None), | ||
]; | ||
|
||
Tester::new(PreferCalledWith::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
assertion_line: 150 | ||
expression: prefer_called_with | ||
--- | ||
⚠ eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`. | ||
╭─[prefer_called_with.tsx:1:1] | ||
1 │ expect(fn).toBeCalled(); | ||
· ────────── | ||
╰──── | ||
help: Prefer "toBeCalled"With(/* expected args */) | ||
|
||
⚠ eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`. | ||
╭─[prefer_called_with.tsx:1:1] | ||
1 │ expect(fn).resolves.toBeCalled(); | ||
· ────────── | ||
╰──── | ||
help: Prefer "toBeCalled"With(/* expected args */) | ||
|
||
⚠ eslint-plugin-jest(prefer-called-with): Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()`. | ||
╭─[prefer_called_with.tsx:1:1] | ||
1 │ expect(fn).toHaveBeenCalled(); | ||
· ──────────────── | ||
╰──── | ||
help: Prefer "toHaveBeenCalled"With(/* expected args */) | ||
|
||
|