Skip to content

Commit

Permalink
feat(linter): eslint-plugin-vitest/no-alias-methods (#4301)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 authored Jul 17, 2024
1 parent 4463eb4 commit 2213f93
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
72 changes: 63 additions & 9 deletions crates/oxc_linter/src/rules/jest/no_alias_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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),
);
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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();
}
22 changes: 22 additions & 0 deletions crates/oxc_linter/src/snapshots/no_alias_methods.snap
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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]
1expect(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]
1expect(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]
1expect(a).not["toThrowError"]()
· ──────────────
╰────
help: Replace "toThrowError" with its canonical name of "toThrow"
1 change: 1 addition & 0 deletions crates/oxc_linter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2213f93

Please sign in to comment.