-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter/eslint-plugin-vitest): implement no-restricted-vi-methods
- Loading branch information
Showing
8 changed files
with
276 additions
and
114 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
92 changes: 92 additions & 0 deletions
92
crates/oxc_linter/src/rules/vitest/no_restricted_vi_methods.rs
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,92 @@ | ||
use oxc_macros::declare_oxc_lint; | ||
use rustc_hash::FxHashMap; | ||
|
||
use crate::{ | ||
context::LintContext, | ||
rule::Rule, | ||
utils::{ | ||
collect_possible_jest_call_node, NoRestrictedTestMethods, NoRestrictedTestMethodsConfig, | ||
}, | ||
}; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoRestrictedViMethods(Box<NoRestrictedTestMethodsConfig>); | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Restrict the use of specific `vi` methods. | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// vi.useFakeTimers(); | ||
/// it('calls the callback after 1 second via advanceTimersByTime', () => { | ||
/// // ... | ||
/// | ||
/// vi.advanceTimersByTime(1000); | ||
/// | ||
/// // ... | ||
/// }); | ||
/// | ||
/// test('plays video', () => { | ||
/// const spy = vi.spyOn(video, 'play'); | ||
/// | ||
/// // ... | ||
/// }); | ||
/// | ||
NoRestrictedViMethods, | ||
style, | ||
); | ||
|
||
impl NoRestrictedTestMethods for NoRestrictedViMethods { | ||
fn restricted_test_methods(&self) -> &FxHashMap<String, String> { | ||
&self.0.restricted_test_methods | ||
} | ||
} | ||
|
||
impl Rule for NoRestrictedViMethods { | ||
fn from_configuration(value: serde_json::Value) -> Self { | ||
Self(Box::new(Self::get_configuration(&value))) | ||
} | ||
|
||
fn run_once(&self, ctx: &LintContext) { | ||
for possible_vitest_node in &collect_possible_jest_call_node(ctx) { | ||
self.run_test(possible_vitest_node, ctx, false); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
("vi", None), | ||
("vi()", None), | ||
("vi.mock()", None), | ||
("expect(a).rejects;", None), | ||
("expect(a);", None), | ||
( | ||
" | ||
import { vi } from 'vitest'; | ||
vi; | ||
", | ||
None, | ||
), // { "parserOptions": { "sourceType": "module" } } | ||
]; | ||
|
||
let fail = vec![ | ||
("vi.fn()", Some(serde_json::json!([{ "fn": null }]))), | ||
("vi.mock()", Some(serde_json::json!([{ "mock": "Do not use mocks" }]))), | ||
( | ||
" | ||
import { vi } from 'vitest'; | ||
vi.advanceTimersByTime(); | ||
", | ||
Some(serde_json::json!([{ "advanceTimersByTime": null }])), | ||
), // { "parserOptions": { "sourceType": "module" } }, | ||
(r#"vi["fn"]()"#, Some(serde_json::json!([{ "fn": null }]))), | ||
]; | ||
|
||
Tester::new(NoRestrictedViMethods::NAME, pass, fail).test_and_snapshot(); | ||
} |
32 changes: 32 additions & 0 deletions
32
crates/oxc_linter/src/snapshots/no_restricted_vi_methods.snap
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,32 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
--- | ||
⚠ eslint-plugin-vitest(no-restricted-vi-methods): Disallow specific `vi.` methods | ||
╭─[no_restricted_vi_methods.tsx:1:4] | ||
1 │ vi.fn() | ||
· ── | ||
╰──── | ||
help: Use of `"fn"` is disallowed | ||
|
||
⚠ eslint-plugin-vitest(no-restricted-vi-methods): Disallow specific `vi.` methods | ||
╭─[no_restricted_vi_methods.tsx:1:4] | ||
1 │ vi.mock() | ||
· ──── | ||
╰──── | ||
help: "Do not use mocks" | ||
|
||
⚠ eslint-plugin-vitest(no-restricted-vi-methods): Disallow specific `vi.` methods | ||
╭─[no_restricted_vi_methods.tsx:3:12] | ||
2 │ import { vi } from 'vitest'; | ||
3 │ vi.advanceTimersByTime(); | ||
· ─────────────────── | ||
4 │ | ||
╰──── | ||
help: Use of `"advanceTimersByTime"` is disallowed | ||
|
||
⚠ eslint-plugin-vitest(no-restricted-vi-methods): Disallow specific `vi.` methods | ||
╭─[no_restricted_vi_methods.tsx:1:4] | ||
1 │ vi["fn"]() | ||
· ──── | ||
╰──── | ||
help: Use of `"fn"` is disallowed |
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
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
Oops, something went wrong.