Skip to content
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

fix(linter): jest/vitest rule compat #4797

Merged
merged 12 commits into from
Aug 29, 2024
9 changes: 9 additions & 0 deletions crates/oxc_linter/src/frameworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ impl FrameworkFlags {
pub const fn is_vitest(self) -> bool {
self.contains(Self::Vitest)
}

#[inline]
pub const fn is_jest(self) -> bool {
self.contains(Self::Jest)
}
}

/// <https://jestjs.io/docs/configuration#testmatch-arraystring>
Expand All @@ -83,3 +88,7 @@ pub(crate) fn is_jestlike_file(path: &Path) -> bool {
pub(crate) fn has_vitest_imports(module_record: &ModuleRecord) -> bool {
module_record.import_entries.iter().any(|entry| entry.module_request.name() == "vitest")
}

pub(crate) fn has_jest_imports(module_record: &ModuleRecord) -> bool {
module_record.import_entries.iter().any(|entry| entry.module_request.name() == "@jest/globals")
}
9 changes: 5 additions & 4 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ impl Linter {
if self.options.plugins.jest || self.options.plugins.vitest {
let mut test_flags = FrameworkFlags::empty();

if frameworks::is_jestlike_file(path) {
test_flags.set(FrameworkFlags::Jest, self.options.plugins.jest);
test_flags.set(FrameworkFlags::Vitest, self.options.plugins.vitest);
} else if frameworks::has_vitest_imports(ctx.module_record()) {
if frameworks::has_vitest_imports(ctx.module_record()) {
test_flags.set(FrameworkFlags::Vitest, true);
} else if frameworks::is_jestlike_file(path)
|| frameworks::has_jest_imports(ctx.module_record())
{
test_flags.set(FrameworkFlags::Jest, true);
}

ctx = ctx.and_frameworks(test_flags);
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/jest/no_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ fn test() {
),
("window.location = 'valid'", None, None, None),
("module.somethingElse = 'foo';", None, None, None),
("export const myThing = 'valid'", None, None, None),
("export default function () {}", None, None, None),
("export const myThing = 'valid'", None, None, Some(PathBuf::from("foo.js"))),
("export default function () {}", None, None, Some(PathBuf::from("foo.js"))),
("module.exports = function(){}", None, None, None),
("module.exports.myThing = 'valid';", None, None, None),
];
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/require_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,5 +622,5 @@ fn tests() {
),
];

Tester::new(RequireHook::NAME, pass, fail).test_and_snapshot();
Tester::new(RequireHook::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot();
}
26 changes: 23 additions & 3 deletions crates/oxc_linter/src/rules/jest/valid_describe_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,26 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
return;
}

if call_expr.arguments.len() == 0 {
let arg_len = call_expr.arguments.len();

// Handle describe.todo("runPrettierFormat")
if ctx.frameworks().is_vitest() && arg_len == 1 {
if let Some(member_expr) = call_expr.callee.as_member_expression() {
let Some(property_name) = member_expr.static_property_name() else {
return;
};
if property_name == "todo" {
return;
}
}
}

if arg_len == 0 {
diagnostic(ctx, call_expr.span, Message::NameAndCallback);
return;
}

if call_expr.arguments.len() == 1 {
if arg_len == 1 {
// For better error notice, we locate it to arguments[0]
diagnostic(ctx, call_expr.arguments[0].span(), Message::NameAndCallback);
return;
Expand Down Expand Up @@ -353,7 +367,13 @@ fn test() {
("fdescribe(\"foo\", () => {})", None),
("describe.only(\"foo\", () => {})", None),
("describe.skip(\"foo\", () => {})", None),
("describe.todo(\"runPrettierFormat\");", None),
(
"
import { describe } from 'vitest';
describe.todo(\"runPrettierFormat\");
",
None,
),
(
"
describe('foo', () => {
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ impl Tester {
self.current_working_directory.join(&self.rule_path)
} else if let Some(path) = path {
self.current_working_directory.join(path)
} else if self.plugins.jest {
self.rule_path.with_extension("test.tsx")
} else {
self.rule_path.clone()
};
Expand Down
17 changes: 15 additions & 2 deletions crates/oxc_linter/src/utils/jest/parse_jest_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use oxc_span::Span;

use crate::{
context::LintContext,
utils::jest::{is_pure_string, JestFnKind, JestGeneralFnKind, PossibleJestNode},
utils::{
jest::{is_pure_string, JestFnKind, JestGeneralFnKind, PossibleJestNode},
vitest::VALID_VITEST_FN_CALL_CHAINS,
},
};

pub fn parse_jest_fn_call<'a>(
Expand Down Expand Up @@ -100,7 +103,12 @@ pub fn parse_jest_fn_call<'a>(

let mut call_chains = Vec::from([Cow::Borrowed(name)]);
call_chains.extend(members.iter().filter_map(KnownMemberExpressionProperty::name));
if !is_valid_jest_call(&call_chains) {

if ctx.frameworks().is_jest() && !is_valid_jest_call(&call_chains) {
return None;
}

if ctx.frameworks().is_vitest() && !is_valid_vitest_call(&call_chains) {
return None;
}

Expand Down Expand Up @@ -298,6 +306,10 @@ fn is_valid_jest_call(members: &[Cow<str>]) -> bool {
.is_ok()
}

fn is_valid_vitest_call(members: &[Cow<str>]) -> bool {
VALID_VITEST_FN_CALL_CHAINS.contains(&members.join("."))
}

fn resolve_to_jest_fn<'a>(
call_expr: &'a CallExpression<'a>,
original: Option<&'a str>,
Expand Down Expand Up @@ -333,6 +345,7 @@ impl<'a> ParsedJestFnCall<'a> {
}
}

#[derive(Debug)]
pub struct ParsedGeneralJestFnCall<'a> {
pub kind: JestFnKind,
pub members: Vec<KnownMemberExpressionProperty<'a>>,
Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_linter/src/utils/vitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use super::{
PossibleJestNode,
};

mod valid_vitest_fn;
pub use crate::utils::vitest::valid_vitest_fn::VALID_VITEST_FN_CALL_CHAINS;

pub fn parse_expect_and_typeof_vitest_fn_call<'a>(
call_expr: &'a CallExpression<'a>,
possible_jest_node: &PossibleJestNode<'a, '_>,
Expand Down
Loading
Loading