From 74f7833eaf6d896ac6756751616e0b60cd243cdc Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Thu, 19 Feb 2026 20:35:05 +0000 Subject: [PATCH] fix(linter/jest/prefer-mock-return-shorthand): preserve typed arrow returns (#19556) --- .../rules/jest/prefer_mock_return_shorthand.rs | 16 ++++++++-------- .../jest_prefer_mock_return_shorthand.snap | 7 +++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/oxc_linter/src/rules/jest/prefer_mock_return_shorthand.rs b/crates/oxc_linter/src/rules/jest/prefer_mock_return_shorthand.rs index fec0971368dda..0a8215bbec672 100644 --- a/crates/oxc_linter/src/rules/jest/prefer_mock_return_shorthand.rs +++ b/crates/oxc_linter/src/rules/jest/prefer_mock_return_shorthand.rs @@ -143,14 +143,9 @@ impl Rule for PreferMockReturnShorthand { new_property_name, ), |fixer| { - let return_text = { - let source = ctx.source_range(GetSpan::span(return_expression)); - - source - .strip_prefix("(") - .and_then(|source| source.strip_suffix(")")) - .map_or(source.to_owned(), std::borrow::ToOwned::to_owned) - }; + let return_text = ctx + .source_range(GetSpan::span(return_expression.without_parentheses())) + .to_owned(); let argument_span = GetSpan::span(expr); let mut multifixer = fixer.for_multifix().new_fix_with_capacity(2); @@ -580,6 +575,7 @@ fn test() { .mockImplementationOnce(() => Promise.reject(42)) .mockImplementation(() => "hello sunshine") .mockReturnValueOnce(Promise.reject(42))"#, + "jest.fn().mockImplementation(() => (input: number | Record) => typeof input === 'number' ? input.toFixed(2) : JSON.stringify(input))", "jest.fn().mockImplementation(() => [], xyz)", r#"jest.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!"))"#, "aVariable.mockImplementation(() => { @@ -845,6 +841,10 @@ fn test() { .mockReturnValue("hello sunshine") .mockReturnValueOnce(Promise.reject(42))"#, ), + ( + "jest.fn().mockImplementation(() => (input: number | Record) => typeof input === 'number' ? input.toFixed(2) : JSON.stringify(input))", + "jest.fn().mockReturnValue((input: number | Record) => typeof input === 'number' ? input.toFixed(2) : JSON.stringify(input))", + ), ("jest.fn().mockImplementation(() => [], xyz)", "jest.fn().mockReturnValue([], xyz)"), ( r#"jest.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!"))"#, diff --git a/crates/oxc_linter/src/snapshots/jest_prefer_mock_return_shorthand.snap b/crates/oxc_linter/src/snapshots/jest_prefer_mock_return_shorthand.snap index ce804c3d266c1..dfe80639f9141 100644 --- a/crates/oxc_linter/src/snapshots/jest_prefer_mock_return_shorthand.snap +++ b/crates/oxc_linter/src/snapshots/jest_prefer_mock_return_shorthand.snap @@ -126,6 +126,13 @@ source: crates/oxc_linter/src/tester.rs ╰──── help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => (input: number | Record) => typeof input === 'number' ? input.toFixed(2) : JSON.stringify(input)) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. ╭─[prefer_mock_return_shorthand.tsx:1:11] 1 │ jest.fn().mockImplementation(() => [], xyz)