-
-
Notifications
You must be signed in to change notification settings - Fork 867
feat(linter/eslint-plugin-vitest): Implement consistent-vitest-vi rule #17389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b248c15
feat(linter/eslint-plugin-vitest): Implement consistent-vitest-vi rule
Afsoon 7e8232d
u
camc314 1d35ecd
u
camc314 4e0f906
u
camc314 593c56d
u
camc314 8851db5
Apply suggestion from @connorshea
connorshea a8a0f5a
[autofix.ci] apply automated fixes
autofix-ci[bot] 0eea4b4
Apply suggestion from @connorshea
connorshea b9f0bd9
feat(linter/eslint-plugin-vitest): Update with new `from_configuratio…
Afsoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
266 changes: 266 additions & 0 deletions
266
crates/oxc_linter/src/rules/vitest/consistent_vitest_vi.rs
This file contains hidden or 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,266 @@ | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::borrow::Cow; | ||
|
|
||
| use oxc_ast::{AstKind, ast::ImportDeclarationSpecifier}; | ||
| use oxc_diagnostics::OxcDiagnostic; | ||
| use oxc_macros::declare_oxc_lint; | ||
| use oxc_span::{GetSpan, Span}; | ||
|
|
||
| use crate::{ | ||
| AstNode, | ||
| context::LintContext, | ||
| rule::{DefaultRuleConfig, Rule}, | ||
| utils::{JestFnKind, JestGeneralFnKind, PossibleJestNode, parse_general_jest_fn_call}, | ||
| }; | ||
|
|
||
| fn consistent_vitest_vi_diagnostic(span: Span, fn_value: &VitestFnName) -> OxcDiagnostic { | ||
| OxcDiagnostic::warn("The vitest function accessor used is not allowed") | ||
| .with_help(format!( | ||
| "Prefer using `{}` instead of `{}`.", | ||
| fn_value.as_str(), | ||
| fn_value.not().as_str() | ||
| )) | ||
| .with_label(span) | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone, Deserialize)] | ||
| pub struct ConsistentVitestVi(Box<ConsistentVitestConfig>); | ||
|
|
||
| impl std::ops::Deref for ConsistentVitestVi { | ||
| type Target = ConsistentVitestConfig; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, Default, JsonSchema, Deserialize, Serialize)] | ||
| #[serde(rename_all = "lowercase")] | ||
| pub enum VitestFnName { | ||
| #[default] | ||
| Vi, | ||
| Vitest, | ||
| } | ||
|
|
||
| impl VitestFnName { | ||
| fn not(&self) -> Self { | ||
| match self { | ||
| VitestFnName::Vi => VitestFnName::Vitest, | ||
| VitestFnName::Vitest => VitestFnName::Vi, | ||
| } | ||
| } | ||
|
|
||
| fn as_str(&self) -> &'static str { | ||
| match self { | ||
| VitestFnName::Vi => "vi", | ||
| VitestFnName::Vitest => "vitest", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone, JsonSchema, Deserialize)] | ||
| #[serde(rename_all = "lowercase")] | ||
| pub struct ConsistentVitestConfig { | ||
| /// Decides whether to prefer vitest function accessor | ||
| #[serde(rename = "fn", default)] | ||
| function: VitestFnName, | ||
| } | ||
|
|
||
| declare_oxc_lint!( | ||
| /// ### What it does | ||
| /// | ||
| /// This rule triggers an error when an unexpected vitest accessor is used. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// Not having a consistent vitest accessor can lead to confusion | ||
| /// when `vi` and `vitest` are used interchangeably. | ||
| /// | ||
| /// ### Examples | ||
| /// | ||
| /// Examples of **incorrect** code for this rule: | ||
| /// ```js | ||
| /// vitest.mock('./src/calculator.ts', { spy: true }) | ||
| /// | ||
| /// vi.stubEnv('NODE_ENV', 'production') | ||
| /// ``` | ||
| /// | ||
| /// Examples of **correct** code for this rule: | ||
| /// ```js | ||
| /// vi.mock('./src/calculator.ts', { spy: true }) | ||
| /// | ||
| /// vi.stubEnv('NODE_ENV', 'production') | ||
| /// ``` | ||
| ConsistentVitestVi, | ||
| vitest, | ||
| style, | ||
| fix, | ||
| config = ConsistentVitestConfig, | ||
| ); | ||
|
|
||
| impl Rule for ConsistentVitestVi { | ||
| fn from_configuration(value: serde_json::Value) -> Result<Self, serde_json::Error> { | ||
| Ok(serde_json::from_value::<DefaultRuleConfig<Self>>(value) | ||
| .unwrap_or_default() | ||
| .into_inner()) | ||
| } | ||
|
|
||
| fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
| match node.kind() { | ||
| AstKind::ImportDeclaration(import) => { | ||
| if import.source.value != "vitest" { | ||
| return; | ||
| } | ||
|
|
||
| let opposite = self.function.not(); | ||
| let Some(vitest_import) = import | ||
| .specifiers | ||
| .as_ref() | ||
| .and_then(|specs| specs.iter().find(|spec| spec.name() == opposite.as_str())) | ||
| else { | ||
| return; | ||
| }; | ||
|
|
||
| ctx.diagnostic_with_fix( | ||
| consistent_vitest_vi_diagnostic(vitest_import.span(), &self.function), | ||
| |fixer| { | ||
| let mut specifiers_without_opposite_accessor: Vec<Cow<str>> = import | ||
| .specifiers | ||
| .as_ref() | ||
| .map(|specs| { | ||
| specs | ||
| .iter() | ||
| .filter(|spec| spec.name() != opposite.as_str()) | ||
| .map(ImportDeclarationSpecifier::name) | ||
| .collect() | ||
| }) | ||
| .unwrap_or_default(); | ||
|
|
||
| if specifiers_without_opposite_accessor.is_empty() { | ||
| fixer.replace(vitest_import.local().span, self.function.as_str()) | ||
| } else { | ||
| if !specifiers_without_opposite_accessor | ||
| .iter() | ||
| .any(|s| s.as_ref() == self.function.as_str()) | ||
| { | ||
| specifiers_without_opposite_accessor | ||
| .push(self.function.as_str().into()); | ||
| } | ||
|
|
||
| let import_text = specifiers_without_opposite_accessor.join(", "); | ||
|
|
||
| let Some(first_specifier) = | ||
| import.specifiers.as_ref().and_then(|specs| specs.first()) | ||
| else { | ||
| return fixer.noop(); | ||
| }; | ||
|
|
||
| let Some(last_specifier) = | ||
| import.specifiers.as_ref().and_then(|specs| specs.last()) | ||
| else { | ||
| return fixer.noop(); | ||
| }; | ||
|
|
||
| let specifiers_span = Span::new( | ||
| first_specifier.local().span.start, | ||
| last_specifier.local().span.end, | ||
| ); | ||
|
|
||
| fixer.replace(specifiers_span, import_text) | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
| AstKind::CallExpression(call_expr) => { | ||
| let Some(vitest_fn) = parse_general_jest_fn_call( | ||
| call_expr, | ||
| &PossibleJestNode { node, original: None }, | ||
| ctx, | ||
| ) else { | ||
| return; | ||
| }; | ||
|
|
||
| if vitest_fn.kind != JestFnKind::General(JestGeneralFnKind::Vitest) { | ||
| return; | ||
| } | ||
|
|
||
| if vitest_fn.name == self.function.not().as_str() { | ||
| let Some(member_expression) = call_expr.callee.as_member_expression() else { | ||
| return; | ||
| }; | ||
|
|
||
| ctx.diagnostic_with_fix( | ||
| consistent_vitest_vi_diagnostic( | ||
| member_expression.object().span(), | ||
| &self.function, | ||
| ), | ||
| |fixer| { | ||
| fixer.replace(member_expression.object().span(), self.function.as_str()) | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test() { | ||
| use crate::tester::Tester; | ||
|
|
||
| let pass = vec![ | ||
| (r#"import { expect, it } from "vitest";"#, None), | ||
| (r#"import { vi } from "vitest";"#, None), | ||
| (r#"import { vitest } from "vitest";"#, Some(serde_json::json!([{ "fn": "vitest" }]))), | ||
| ( | ||
| r#"import { vi } from "vitest"; | ||
| vi.stubEnv("NODE_ENV", "production");"#, | ||
| None, | ||
| ), | ||
| (r#"vi.stubEnv("NODE_ENV", "production");"#, None), | ||
| ]; | ||
|
|
||
| let fail = vec![ | ||
| (r#"import { vitest } from "vitest";"#, None), | ||
| (r#"import { expect, vi, vitest } from "vitest";"#, None), | ||
| ( | ||
| r#"import { vitest } from "vitest"; | ||
| vitest.stubEnv("NODE_ENV", "production");"#, | ||
| None, | ||
| ), | ||
| ( | ||
| r#"vi.stubEnv("NODE_ENV", "production"); | ||
| vi.clearAllMocks();"#, | ||
| Some(serde_json::json!([{ "fn": "vitest" }])), | ||
| ), | ||
| ]; | ||
|
|
||
| let fix = vec![ | ||
| (r#"import { vitest } from "vitest";"#, r#"import { vi } from "vitest";"#, None), // WORKING | ||
| ( | ||
| r#"import { expect, vi, vitest } from "vitest";"#, | ||
| r#"import { expect, vi } from "vitest";"#, | ||
| None, | ||
| ), | ||
| ( | ||
| r#"import { vitest } from "vitest"; | ||
| vitest.stubEnv("NODE_ENV", "production");"#, | ||
| r#"import { vi } from "vitest"; | ||
| vi.stubEnv("NODE_ENV", "production");"#, | ||
| None, | ||
| ), | ||
| ( | ||
| r#"vi.stubEnv("NODE_ENV", "production"); | ||
| vi.clearAllMocks();"#, | ||
| r#"vitest.stubEnv("NODE_ENV", "production"); | ||
| vitest.clearAllMocks();"#, | ||
| Some(serde_json::json!([{ "fn": "vitest" }])), | ||
| ), | ||
| ]; | ||
| Tester::new(ConsistentVitestVi::NAME, ConsistentVitestVi::PLUGIN, pass, fail) | ||
| .expect_fix(fix) | ||
| .with_vitest_plugin(true) | ||
| .test_and_snapshot(); | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
crates/oxc_linter/src/snapshots/vitest_consistent_vitest_vi.snap
This file contains hidden or 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,48 @@ | ||
| --- | ||
| source: crates/oxc_linter/src/tester.rs | ||
| --- | ||
| ⚠ eslint-plugin-vitest(consistent-vitest-vi): The vitest function accessor used is not allowed | ||
| ╭─[consistent_vitest_vi.tsx:1:10] | ||
| 1 │ import { vitest } from "vitest"; | ||
| · ────── | ||
| ╰──── | ||
| help: Prefer using `vi` instead of `vitest`. | ||
|
|
||
| ⚠ eslint-plugin-vitest(consistent-vitest-vi): The vitest function accessor used is not allowed | ||
| ╭─[consistent_vitest_vi.tsx:1:22] | ||
| 1 │ import { expect, vi, vitest } from "vitest"; | ||
| · ────── | ||
| ╰──── | ||
| help: Prefer using `vi` instead of `vitest`. | ||
|
|
||
| ⚠ eslint-plugin-vitest(consistent-vitest-vi): The vitest function accessor used is not allowed | ||
| ╭─[consistent_vitest_vi.tsx:1:10] | ||
| 1 │ import { vitest } from "vitest"; | ||
| · ────── | ||
| 2 │ vitest.stubEnv("NODE_ENV", "production"); | ||
| ╰──── | ||
| help: Prefer using `vi` instead of `vitest`. | ||
|
|
||
| ⚠ eslint-plugin-vitest(consistent-vitest-vi): The vitest function accessor used is not allowed | ||
| ╭─[consistent_vitest_vi.tsx:2:4] | ||
| 1 │ import { vitest } from "vitest"; | ||
| 2 │ vitest.stubEnv("NODE_ENV", "production"); | ||
| · ────── | ||
| ╰──── | ||
| help: Prefer using `vi` instead of `vitest`. | ||
|
|
||
| ⚠ eslint-plugin-vitest(consistent-vitest-vi): The vitest function accessor used is not allowed | ||
| ╭─[consistent_vitest_vi.tsx:1:1] | ||
| 1 │ vi.stubEnv("NODE_ENV", "production"); | ||
| · ── | ||
| 2 │ vi.clearAllMocks(); | ||
| ╰──── | ||
| help: Prefer using `vitest` instead of `vi`. | ||
|
|
||
| ⚠ eslint-plugin-vitest(consistent-vitest-vi): The vitest function accessor used is not allowed | ||
| ╭─[consistent_vitest_vi.tsx:2:4] | ||
| 1 │ vi.stubEnv("NODE_ENV", "production"); | ||
| 2 │ vi.clearAllMocks(); | ||
| · ── | ||
| ╰──── | ||
| help: Prefer using `vitest` instead of `vi`. |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.