Skip to content

Commit

Permalink
feat(linter): support eslint-plugin-vitest/no-test-prefixes (#4182)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 authored Jul 12, 2024
1 parent cb15303 commit 3e56b2b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 13 deletions.
65 changes: 54 additions & 11 deletions crates/oxc_linter/src/rules/jest/no_test_prefixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use crate::{
context::LintContext,
rule::Rule,
utils::{
collect_possible_jest_call_node, parse_general_jest_fn_call, JestGeneralFnKind,
KnownMemberExpressionProperty, ParsedGeneralJestFnCall, PossibleJestNode,
collect_possible_jest_call_node, get_test_plugin_name, parse_general_jest_fn_call,
JestGeneralFnKind, KnownMemberExpressionProperty, ParsedGeneralJestFnCall,
PossibleJestNode, TestPluginName,
},
};

fn no_test_prefixes_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("eslint-plugin-jest(no-test-prefixes): Use {x0:?} instead."))
.with_label(span1)
fn no_test_prefixes_diagnostic(x0: TestPluginName, x1: &str, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("{x0}(no-test-prefixes): Use {x1:?} instead.")).with_label(span2)
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -42,6 +42,17 @@ declare_oxc_lint!(
/// xtest('foo'); // invalid
/// xdescribe('foo'); // invalid
/// ```
///
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-test-prefixes.md),
/// to use it, add the following configuration to your `.eslintrc.json`:
///
/// ```json
/// {
/// "rules": {
/// "vitest/no-test-prefixes": "error"
/// }
/// }
/// ```
NoTestPrefixes,
style
);
Expand Down Expand Up @@ -84,10 +95,12 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
};

let preferred_node_name = get_preferred_node_names(&jest_fn_call);
let plugin_name = get_test_plugin_name(ctx);

ctx.diagnostic_with_fix(no_test_prefixes_diagnostic(&preferred_node_name, span), |fixer| {
fixer.replace(span, preferred_node_name)
});
ctx.diagnostic_with_fix(
no_test_prefixes_diagnostic(plugin_name, &preferred_node_name, span),
|fixer| fixer.replace(span, preferred_node_name),
);
}

fn get_preferred_node_names(jest_fn_call: &ParsedGeneralJestFnCall) -> String {
Expand All @@ -112,7 +125,7 @@ fn get_preferred_node_names(jest_fn_call: &ParsedGeneralJestFnCall) -> String {
fn test() {
use crate::tester::Tester;

let pass = vec![
let mut pass = vec![
("describe('foo', function () {})", None),
("it('foo', function () {})", None),
("it.concurrent('foo', function () {})", None),
Expand All @@ -132,7 +145,7 @@ fn test() {
("[1,2,3].forEach()", None),
];

let fail = vec![
let mut fail = vec![
("fdescribe('foo', function () {})", None),
("xdescribe.each([])('foo', function () {})", None),
("fit('foo', function () {})", None),
Expand Down Expand Up @@ -166,5 +179,35 @@ fn test() {
),
];

Tester::new(NoTestPrefixes::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot();
let pass_vitest = vec![
("describe(\"foo\", function () {})", None),
("it(\"foo\", function () {})", None),
("it.concurrent(\"foo\", function () {})", None),
("test(\"foo\", function () {})", None),
("test.concurrent(\"foo\", function () {})", None),
("describe.only(\"foo\", function () {})", None),
("it.only(\"foo\", function () {})", None),
("it.each()(\"foo\", function () {})", None),
];

let fail_vitest = vec![
("fdescribe(\"foo\", function () {})", None),
("xdescribe.each([])(\"foo\", function () {})", None),
("fit(\"foo\", function () {})", None),
("xdescribe(\"foo\", function () {})", None),
("xit(\"foo\", function () {})", None),
("xtest(\"foo\", function () {})", None),
("xit.each``(\"foo\", function () {})", None),
("xtest.each``(\"foo\", function () {})", None),
("xit.each([])(\"foo\", function () {})", None),
("xtest.each([])(\"foo\", function () {})", None),
];

pass.extend(pass_vitest);
fail.extend(fail_vitest);

Tester::new(NoTestPrefixes::NAME, pass, fail)
.with_jest_plugin(true)
.with_vitest_plugin(true)
.test_and_snapshot();
}
61 changes: 61 additions & 0 deletions crates/oxc_linter/src/snapshots/no_test_prefixes.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-test-prefixes): Use "describe.only" instead.
╭─[no_test_prefixes.tsx:1:1]
Expand Down Expand Up @@ -84,3 +85,63 @@ source: crates/oxc_linter/src/tester.rs
· ────────
4
╰────

eslint-plugin-jest(no-test-prefixes): Use "describe.only" instead.
╭─[no_test_prefixes.tsx:1:1]
1fdescribe("foo", function () {})
· ─────────
╰────

eslint-plugin-jest(no-test-prefixes): Use "describe.skip.each" instead.
╭─[no_test_prefixes.tsx:1:1]
1xdescribe.each([])("foo", function () {})
· ──────────────
╰────

eslint-plugin-jest(no-test-prefixes): Use "it.only" instead.
╭─[no_test_prefixes.tsx:1:1]
1fit("foo", function () {})
· ───
╰────

eslint-plugin-jest(no-test-prefixes): Use "describe.skip" instead.
╭─[no_test_prefixes.tsx:1:1]
1xdescribe("foo", function () {})
· ─────────
╰────

eslint-plugin-jest(no-test-prefixes): Use "it.skip" instead.
╭─[no_test_prefixes.tsx:1:1]
1xit("foo", function () {})
· ───
╰────

eslint-plugin-jest(no-test-prefixes): Use "test.skip" instead.
╭─[no_test_prefixes.tsx:1:1]
1xtest("foo", function () {})
· ─────
╰────

eslint-plugin-jest(no-test-prefixes): Use "it.skip.each" instead.
╭─[no_test_prefixes.tsx:1:1]
1xit.each``("foo", function () {})
· ────────
╰────

eslint-plugin-jest(no-test-prefixes): Use "test.skip.each" instead.
╭─[no_test_prefixes.tsx:1:1]
1xtest.each``("foo", function () {})
· ──────────
╰────

eslint-plugin-jest(no-test-prefixes): Use "it.skip.each" instead.
╭─[no_test_prefixes.tsx:1:1]
1xit.each([])("foo", function () {})
· ────────
╰────

eslint-plugin-jest(no-test-prefixes): Use "test.skip.each" instead.
╭─[no_test_prefixes.tsx:1:1]
1xtest.each([])("foo", function () {})
· ──────────
╰────
9 changes: 7 additions & 2 deletions crates/oxc_linter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ pub use self::{
/// Many Vitest rule are essentially ports of Jest plugin rules with minor modifications.
/// For these rules, we use the corresponding jest rules with some adjustments for compatibility.
pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {
let jest_rules: &[&str] =
&["consistent-test-it", "no-disabled-tests", "no-focused-tests", "prefer-hooks-in-order"];
let jest_rules: &[&str] = &[
"consistent-test-it",
"no-disabled-tests",
"no-focused-tests",
"no-test-prefixes",
"prefer-hooks-in-order",
];

jest_rules.contains(&rule_name)
}
Expand Down

0 comments on commit 3e56b2b

Please sign in to comment.