Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions crates/oxc_linter/src/rules/vitest/prefer_expect_type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc_span::{GetSpan, Span};
use crate::{
context::LintContext,
rule::Rule,
utils::{PossibleJestNode, parse_expect_jest_fn_call},
utils::{MemberExpressionElement, PossibleJestNode, parse_expect_jest_fn_call},
};

fn prefer_expect_type_of_diagnostic(span: Span, help: &str) -> OxcDiagnostic {
Expand Down Expand Up @@ -129,7 +129,16 @@ impl PreferExpectTypeOf {
let modifier_text =
expect_call.modifiers().iter().fold(String::new(), |mut acc, modifier| {
use std::fmt::Write;
write!(&mut acc, ".{}", ctx.source_range(modifier.span)).unwrap();
match modifier.element {
// `.not`
MemberExpressionElement::IdentName(_) => {
write!(&mut acc, ".{}", ctx.source_range(modifier.span)).unwrap();
}
// `["not"]`, `[not]`, `[`not`]`, etc.
MemberExpressionElement::Expression(_) => {
write!(&mut acc, "[{}]", ctx.source_range(modifier.span)).unwrap();
}
}
acc
});

Expand Down Expand Up @@ -171,6 +180,7 @@ fn test() {
r#"expect(typeof fn).toBe("function")"#,
r#"expect(typeof value).toEqual("string")"#,
r#"expect(typeof value).not.toBe("string")"#,
r#"expect(typeof value)["not"].toBe("string")"#,
];

let fix = vec![
Expand All @@ -181,6 +191,10 @@ fn test() {
(r#"expect(typeof fn).toBe("function")"#, "expectTypeOf(fn).toBeFunction()"),
(r#"expect(typeof value).toEqual("string")"#, "expectTypeOf(value).toBeString()"),
(r#"expect(typeof value).not.toBe("string")"#, "expectTypeOf(value).not.toBeString()"),
(
r#"expect(typeof value)["not"].toBe("string")"#,
r#"expectTypeOf(value)["not"].toBeString()"#,
),
];

Tester::new(PreferExpectTypeOf::NAME, PreferExpectTypeOf::PLUGIN, pass, fail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ source: crates/oxc_linter/src/tester.rs
· ───────────────────────────────────────
╰────
help: Substitute the assertion with `expectTypeOf(value).not.toBeString()`.

⚠ eslint-plugin-vitest(prefer-expect-type-of): Type assertions should be done using `expectTypeOf`.
╭─[prefer_expect_type_of.tsx:1:1]
1 │ expect(typeof value)["not"].toBe("string")
· ──────────────────────────────────────────
╰────
help: Substitute the assertion with `expectTypeOf(value)["not"].toBeString()`.
Loading