feat(linter): eslint-plugin-vitest/no-alias-methods (#4301)

support
[eslint-plugin-vitest/no-alias-methods](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-alias-method.md)
This commit is contained in:
cinchen 2024-07-18 04:30:35 +08:00 committed by GitHub
parent 4463eb4c48
commit 2213f9393b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 86 additions and 9 deletions

View file

@ -6,13 +6,21 @@ use oxc_span::Span;
use crate::{
context::LintContext,
rule::Rule,
utils::{collect_possible_jest_call_node, parse_expect_jest_fn_call, PossibleJestNode},
utils::{
collect_possible_jest_call_node, get_test_plugin_name, parse_expect_jest_fn_call,
PossibleJestNode, TestPluginName,
},
};
fn no_alias_methods_diagnostic(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("eslint-plugin-jest(no-alias-methods): Unexpected alias {x0:?}"))
.with_help(format!("Replace {x0:?} with its canonical name of {x1:?}"))
.with_label(span2)
fn no_alias_methods_diagnostic(
x0: TestPluginName,
x1: &str,
x2: &str,
span3: Span,
) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("{x0}(no-alias-methods): Unexpected alias {x1:?}"))
.with_help(format!("Replace {x1:?} with its canonical name of {x2:?}"))
.with_label(span3)
}
#[derive(Debug, Default, Clone)]
@ -42,6 +50,17 @@ declare_oxc_lint!(
/// expect(a).nthReturnedWith();
/// expect(a).toThrowError();
/// ```
///
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-alias-methods.md),
/// to use it, add the following configuration to your `.eslintrc.json`:
///
/// ```json
/// {
/// "rules": {
/// "vitest/no-alias-methods": "error"
/// }
/// }
/// ```
NoAliasMethods,
style
);
@ -77,8 +96,10 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
span.end -= 1;
}
let plugin_name = get_test_plugin_name(ctx);
ctx.diagnostic_with_fix(
no_alias_methods_diagnostic(name, canonical_name, matcher.span),
no_alias_methods_diagnostic(plugin_name, name, canonical_name, matcher.span),
// || Fix::new(canonical_name, Span::new(start, end)),
|fixer| fixer.replace(span, canonical_name),
);
@ -140,7 +161,7 @@ impl BadAliasMethodName {
fn test() {
use crate::tester::Tester;
let pass = vec![
let mut pass = vec![
("expect(a).toHaveBeenCalled()", None),
("expect(a).toHaveBeenCalledTimes()", None),
("expect(a).toHaveBeenCalledWith()", None),
@ -156,7 +177,7 @@ fn test() {
("expect(a);", None),
];
let fail = vec![
let mut fail = vec![
("expect(a).toBeCalled()", None),
("expect(a).toBeCalledTimes()", None),
("expect(a).toBeCalledWith()", None),
@ -174,14 +195,47 @@ fn test() {
("expect(a).not['toThrowError']()", None),
];
let fix = vec![
let mut fix = vec![
("expect(a).toBeCalled()", "expect(a).toHaveBeenCalled()", None),
("expect(a).not['toThrowError']()", "expect(a).not['toThrow']()", None),
("expect(a).not[`toThrowError`]()", "expect(a).not[`toThrow`]()", None),
];
let pass_vitest = vec![
"expect(a).toHaveBeenCalled()",
"expect(a).toHaveBeenCalledTimes()",
"expect(a).toHaveBeenCalledWith()",
"expect(a).toHaveBeenLastCalledWith()",
"expect(a).toHaveBeenNthCalledWith()",
"expect(a).toHaveReturned()",
"expect(a).toHaveReturnedTimes()",
"expect(a).toHaveReturnedWith()",
"expect(a).toHaveLastReturnedWith()",
"expect(a).toHaveNthReturnedWith()",
"expect(a).toThrow()",
"expect(a).rejects;",
"expect(a);",
];
let fail_vitest = vec![
"expect(a).toBeCalled()",
"expect(a).toBeCalledTimes()",
r#"expect(a).not["toThrowError"]()"#,
];
let fix_vitest = vec![
("expect(a).toBeCalled()", "expect(a).toHaveBeenCalled()", None),
("expect(a).toBeCalledTimes()", "expect(a).toHaveBeenCalledTimes()", None),
("expect(a).not['toThrowError']()", "expect(a).not['toThrow']()", None),
];
pass.extend(pass_vitest.into_iter().map(|x| (x, None)));
fail.extend(fail_vitest.into_iter().map(|x| (x, None)));
fix.extend(fix_vitest);
Tester::new(NoAliasMethods::NAME, pass, fail)
.with_jest_plugin(true)
.with_vitest_plugin(true)
.expect_fix(fix)
.test_and_snapshot();
}

View file

@ -1,5 +1,6 @@
---
source: crates/oxc_linter/src/tester.rs
assertion_line: 216
---
⚠ eslint-plugin-jest(no-alias-methods): Unexpected alias "toBeCalled"
╭─[no_alias_methods.tsx:1:11]
@ -105,3 +106,24 @@ source: crates/oxc_linter/src/tester.rs
· ──────────────
╰────
help: Replace "toThrowError" with its canonical name of "toThrow"
⚠ eslint-plugin-jest(no-alias-methods): Unexpected alias "toBeCalled"
╭─[no_alias_methods.tsx:1:11]
1 │ expect(a).toBeCalled()
· ──────────
╰────
help: Replace "toBeCalled" with its canonical name of "toHaveBeenCalled"
⚠ eslint-plugin-jest(no-alias-methods): Unexpected alias "toBeCalledTimes"
╭─[no_alias_methods.tsx:1:11]
1 │ expect(a).toBeCalledTimes()
· ───────────────
╰────
help: Replace "toBeCalledTimes" with its canonical name of "toHaveBeenCalledTimes"
⚠ eslint-plugin-jest(no-alias-methods): Unexpected alias "toThrowError"
╭─[no_alias_methods.tsx:1:15]
1 │ expect(a).not["toThrowError"]()
· ──────────────
╰────
help: Replace "toThrowError" with its canonical name of "toThrow"

View file

@ -18,6 +18,7 @@ pub use self::{
pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {
let jest_rules: &[&str] = &[
"consistent-test-it",
"no-alias-methods",
"no-disabled-tests",
"no-focused-tests",
"no-test-prefixes",