Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 35 additions & 1 deletion crates/oxc_linter/src/rules/vitest/prefer_called_once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,14 @@ impl PreferCalledOnce {
ctx.diagnostic_with_fix(
prefer_called_once_diagnostic(matcher_and_args_span, new_matcher_name.as_ref()),
|fixer| {
let argument_without_parenthesis_span =
get_inside_parenthesis_span(called_times_value.span, ctx);

let multi_fix = fixer.for_multifix();
let mut fixes = multi_fix.new_fix_with_capacity(2);

fixes.push(fixer.replace(matcher_to_be_fixed.span, new_matcher_name));
fixes.push(fixer.delete(&called_times_value.span));
fixes.push(fixer.delete(&argument_without_parenthesis_span));

fixes.with_message("Replace API with preferOnce instead of Times")
},
Expand All @@ -125,6 +128,28 @@ impl PreferCalledOnce {
}
}

fn is_open_parenthesis(source_text: &str) -> bool {
source_text.starts_with('(')
}

fn is_close_parenthesis(source_text: &str) -> bool {
source_text.ends_with(')')
}

fn get_inside_parenthesis_span(argument_span: Span, ctx: &LintContext<'_>) -> Span {
let mut inside_parentehsis_span = Span::new(argument_span.start, argument_span.end);
Comment thread
connorshea marked this conversation as resolved.
Outdated

while !is_open_parenthesis(ctx.source_range(inside_parentehsis_span.expand_left(1))) {
inside_parentehsis_span = inside_parentehsis_span.expand_left(1);
}

while !is_close_parenthesis(ctx.source_range(inside_parentehsis_span.expand_right(1))) {
inside_parentehsis_span = inside_parentehsis_span.expand_right(1);
}

inside_parentehsis_span
Comment thread
connorshea marked this conversation as resolved.
Outdated
}

#[test]
fn test() {
use crate::tester::Tester;
Expand Down Expand Up @@ -153,6 +178,9 @@ fn test() {
"expect(fn).resolves.toBeCalledTimes(1);",
"expect(fn).resolves.toHaveBeenCalledTimes(1);",
"expect(fn).resolves.toHaveBeenCalledTimes(/*comment*/1);",
"expect(window.HTMLElement.prototype.scrollIntoView).toHaveBeenCalledTimes(
1,
);",
];

let fix = vec![
Expand All @@ -165,6 +193,12 @@ fn test() {
"expect(fn).resolves.toHaveBeenCalledTimes(1);",
"expect(fn).resolves.toHaveBeenCalledOnce();",
),
(
"expect(window.HTMLElement.prototype.scrollIntoView).toHaveBeenCalledTimes(
1,
);",
"expect(window.HTMLElement.prototype.scrollIntoView).toHaveBeenCalledOnce();",
),
];
Tester::new(PreferCalledOnce::NAME, PreferCalledOnce::PLUGIN, pass, fail)
.expect_fix(fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ source: crates/oxc_linter/src/tester.rs
· ───────────────────────────────────
╰────
help: Prefer `toHaveBeenCalledOnce()`.

⚠ eslint-plugin-vitest(prefer-called-once): The use of `toBeCalledTimes(1)` and `toHaveBeenCalledTimes(1)` is discouraged.
╭─[prefer_called_once.tsx:1:53]
1 │ ╭─▶ expect(window.HTMLElement.prototype.scrollIntoView).toHaveBeenCalledTimes(
2 │ │ 1,
3 │ ╰─▶ );
╰────
help: Prefer `toHaveBeenCalledOnce()`.
Loading