feat(linter): eslint-plugin-jest/prefer-hooks-in-order (#4052)

part of https://github.com/oxc-project/oxc/issues/492

Rule Detail:
[link](https://github.com/jest-community/eslint-plugin-jest/blob/main/src/rules/prefer-hooks-in-order.ts)
This commit is contained in:
cinchen 2024-07-10 22:41:19 +08:00 committed by GitHub
parent 3ba7cfed1f
commit 218814483b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 1684 additions and 1 deletions

View file

@ -189,6 +189,7 @@ mod jest {
pub mod prefer_comparison_matcher;
pub mod prefer_equality_matcher;
pub mod prefer_expect_resolves;
pub mod prefer_hooks_in_order;
pub mod prefer_hooks_on_top;
pub mod prefer_jest_mocked;
pub mod prefer_lowercase_title;
@ -588,6 +589,7 @@ oxc_macros::declare_all_lint_rules! {
jest::prefer_comparison_matcher,
jest::prefer_equality_matcher,
jest::prefer_expect_resolves,
jest::prefer_hooks_in_order,
jest::prefer_hooks_on_top,
jest::prefer_jest_mocked,
jest::prefer_lowercase_title,

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,409 @@
---
source: crates/oxc_linter/src/tester.rs
assertion_line: 216
---
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:6:21]
5 │ });
6 │ ╭─▶ beforeAll(() => {
7 │ │ createMyDatabase();
8 │ ╰─▶ });
9 │ };
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:5:17]
4 │ });
5 │ ╭─▶ beforeAll(() => {
6 │ │ createMyDatabase();
7 │ ╰─▶ });
8 │
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:17]
2 │ afterAll(() => {});
3 │ beforeAll(() => {});
· ───────────────────
4 │
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:17]
2 │ afterEach(() => {});
3 │ beforeEach(() => {});
· ────────────────────
4 │
╰────
help: "beforeEach" hooks should be before any "afterEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:17]
2 │ afterEach(() => {});
3 │ beforeAll(() => {});
· ───────────────────
4 │
╰────
help: "beforeAll" hooks should be before any "afterEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:17]
2 │ beforeEach(() => {});
3 │ beforeAll(() => {});
· ───────────────────
4 │
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:17]
2 │ afterAll(() => {});
3 │ afterEach(() => {});
· ───────────────────
4 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:5:17]
4 │ // This comment does not matter for the order
5 │ afterEach(() => {});
· ───────────────────
6 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:17]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:21]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │ });
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:21]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:9:21]
8 │ beforeEach(() => {});
9 │ beforeAll(() => {});
· ───────────────────
10 │ });
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:21]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:9:21]
8 │ beforeEach(() => {});
9 │ beforeAll(() => {});
· ───────────────────
10 │ });
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:7:25]
6 │ beforeEach(() => {});
7 │ beforeAll(() => {});
· ───────────────────
8 │ });
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:10:25]
9 │ afterEach(() => {});
10 │ beforeEach(() => {});
· ────────────────────
11 │ afterEach(() => {});
╰────
help: "beforeEach" hooks should be before any "afterEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:5:21]
4 │ afterAll(() => {});
5 │ beforeAll(() => {});
· ───────────────────
6 │
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:23:29]
22 │ // This comment does nothing
23 │ afterEach(() => {});
· ───────────────────
24 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:8:25]
7 │ });
8 │ ╭─▶ beforeAll(() => {
9 │ │ setupMocks();
10 │ ╰─▶ });
11 │ };
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:19:25]
18 │ afterAll(() => {});
19 │ afterEach(() => {});
· ───────────────────
20 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:7:21]
6 │
7 │ ╭─▶ beforeAll(() => {
8 │ │ createMyDatabase();
9 │ ╰─▶ });
10 │
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:38:25]
37 │
38 │ ╭─▶ beforeEach(() => {
39 │ │ mockLogger();
40 │ ╰─▶ });
41 │
╰────
help: "beforeEach" hooks should be before any "afterEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:6:17]
5 │ });
6 │ ╭─▶ beforeAll(() => {
7 │ │ createMyDatabase();
8 │ ╰─▶ });
9 │ };
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:5:13]
4 │ });
5 │ ╭─▶ beforeAll(() => {
6 │ │ createMyDatabase();
7 │ ╰─▶ });
8 │
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:13]
2 │ afterAll(() => {});
3 │ beforeAll(() => {});
· ───────────────────
4 │
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:13]
2 │ afterEach(() => {});
3 │ beforeEach(() => {});
· ────────────────────
4 │
╰────
help: "beforeEach" hooks should be before any "afterEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:13]
2 │ afterEach(() => {});
3 │ beforeAll(() => {});
· ───────────────────
4 │
╰────
help: "beforeAll" hooks should be before any "afterEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:13]
2 │ beforeEach(() => {});
3 │ beforeAll(() => {});
· ───────────────────
4 │
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:3:13]
2 │ afterAll(() => {});
3 │ afterEach(() => {});
· ───────────────────
4 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:5:13]
4 │ // This comment does not matter for the order
5 │ afterEach(() => {});
· ───────────────────
6 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:13]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:17]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │ });
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:17]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:9:17]
8 │ beforeEach(() => {});
9 │ beforeAll(() => {});
· ───────────────────
10 │ });
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:4:17]
3 │ afterAll(() => {});
4 │ afterEach(() => {});
· ───────────────────
5 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:9:17]
8 │ beforeEach(() => {});
9 │ beforeAll(() => {});
· ───────────────────
10 │ });
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:7:21]
6 │ beforeEach(() => {});
7 │ beforeAll(() => {});
· ───────────────────
8 │ });
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:10:21]
9 │ afterEach(() => {});
10 │ beforeEach(() => {});
· ────────────────────
11 │ afterEach(() => {});
╰────
help: "beforeEach" hooks should be before any "afterEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:5:17]
4 │ afterAll(() => {});
5 │ beforeAll(() => {});
· ───────────────────
6 │
╰────
help: "beforeAll" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:23:25]
22 │ // This comment does nothing
23 │ afterEach(() => {});
· ───────────────────
24 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:8:21]
7 │ });
8 │ ╭─▶ beforeAll(() => {
9 │ │ setupMocks();
10 │ ╰─▶ });
11 │ };
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:19:21]
18 │ afterAll(() => {});
19 │ afterEach(() => {});
· ───────────────────
20 │
╰────
help: "afterEach" hooks should be before any "afterAll" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:7:17]
6 │
7 │ ╭─▶ beforeAll(() => {
8 │ │ createMyDatabase();
9 │ ╰─▶ });
10 │
╰────
help: "beforeAll" hooks should be before any "beforeEach" hooks
⚠ eslint-plugin-jest(prefer-hooks-in-order): Prefer having hooks in a consistent order.
╭─[prefer_hooks_in_order.tsx:38:21]
37 │
38 │ ╭─▶ beforeEach(() => {
39 │ │ mockLogger();
40 │ ╰─▶ });
41 │
╰────
help: "beforeEach" hooks should be before any "afterEach" hooks

View file

@ -16,7 +16,8 @@ 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; 2] = ["consistent_test_it", "no-disabled-tests"];
let jest_rules: [&str; 3] =
["consistent_test_it", "no-disabled-tests", "prefer-hooks-in-order"];
jest_rules.contains(&rule_name)
}