From 65817ae87fb0c6accaffd6c0b4942e60458f475d Mon Sep 17 00:00:00 2001 From: shulaoda Date: Sun, 25 Aug 2024 20:00:18 +0800 Subject: [PATCH 1/3] chore: add test cases --- .../src/rules/jest/prefer_to_have_length.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs b/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs index 81605191aa1f2..d2ae5e11f4592 100644 --- a/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs +++ b/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs @@ -217,6 +217,10 @@ fn tests() { "expect((meta.get('pages') as YArray).length).toBe((originalMeta.get('pages') as YArray).length);", None ), + ( + "expect(assetTypeContainer.getElementsByTagName('time').length).toEqual( + 0, + );", None) ]; let fix = vec![ @@ -247,6 +251,15 @@ fn tests() { "expect((meta.get('pages') as YArray)).toHaveLength((originalMeta.get('pages') as YArray).length);", None ), + ( + "expect(assetTypeContainer.getElementsByTagName('time').length).toEqual( + 0, + );", + "expect(assetTypeContainer.getElementsByTagName('time')).toHaveLength( + 0, + );", + None + ) ]; Tester::new(PreferToHaveLength::NAME, pass, fail) From 9c80bb6711997b525385b870d0825755fc8e9cf2 Mon Sep 17 00:00:00 2001 From: shulaoda Date: Sun, 25 Aug 2024 21:09:38 +0800 Subject: [PATCH 2/3] finish --- .../src/rules/jest/prefer_to_have_length.rs | 11 +++++------ .../src/snapshots/prefer_to_have_length.snap | 8 ++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs b/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs index d2ae5e11f4592..1d93f32f2f9e1 100644 --- a/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs +++ b/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs @@ -144,12 +144,11 @@ impl PreferToHaveLength { ctx.diagnostic_with_fix(use_to_have_length(matcher.span), |fixer| { let code = Self::build_code(fixer, static_mem_expr, kind, property_name); - let end = if call_expr.arguments.len() > 0 { - call_expr.arguments.first().unwrap().span().start - } else { - matcher.span.end - }; - fixer.replace(Span::new(call_expr.span.start, end - 1), code) + let offset = fixer + .source_range(Span::new(matcher.span.end, call_expr.span().end)) + .find('(') + .unwrap() as u32; + fixer.replace(Span::new(call_expr.span.start, matcher.span.end + offset), code) }); } diff --git a/crates/oxc_linter/src/snapshots/prefer_to_have_length.snap b/crates/oxc_linter/src/snapshots/prefer_to_have_length.snap index 72d2052315d05..2091519888c5b 100644 --- a/crates/oxc_linter/src/snapshots/prefer_to_have_length.snap +++ b/crates/oxc_linter/src/snapshots/prefer_to_have_length.snap @@ -77,3 +77,11 @@ source: crates/oxc_linter/src/tester.rs · ──── ╰──── help: Replace `expect((meta.get('pages') as YArray).length).toBe` with `expect((meta.get('pages') as YArray)).toHaveLength`. + + ⚠ eslint-plugin-jest(prefer-to-have-length): Suggest using `toHaveLength()`. + ╭─[prefer_to_have_length.tsx:1:64] + 1 │ expect(assetTypeContainer.getElementsByTagName('time').length).toEqual( + · ─────── + 2 │ 0, + ╰──── + help: Replace `expect(assetTypeContainer.getElementsByTagName('time').length).toEqual` with `expect(assetTypeContainer.getElementsByTagName('time')).toHaveLength`. From 5e849681aa307d426eba7f873c023c6e85bd5e57 Mon Sep 17 00:00:00 2001 From: shulaoda Date: Sun, 25 Aug 2024 21:14:19 +0800 Subject: [PATCH 3/3] lint --- .../src/rules/jest/prefer_to_have_length.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs b/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs index 1d93f32f2f9e1..896c1d2941899 100644 --- a/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs +++ b/crates/oxc_linter/src/rules/jest/prefer_to_have_length.rs @@ -144,10 +144,13 @@ impl PreferToHaveLength { ctx.diagnostic_with_fix(use_to_have_length(matcher.span), |fixer| { let code = Self::build_code(fixer, static_mem_expr, kind, property_name); - let offset = fixer - .source_range(Span::new(matcher.span.end, call_expr.span().end)) - .find('(') - .unwrap() as u32; + let offset = u32::try_from( + fixer + .source_range(Span::new(matcher.span.end, call_expr.span().end)) + .find('(') + .unwrap(), + ) + .unwrap(); fixer.replace(Span::new(call_expr.span.start, matcher.span.end + offset), code) }); }