From bf839c1dfa1dcf6a48ec1fcadf49a1fe839e6f06 Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Mon, 18 Nov 2024 23:17:36 +0800 Subject: [PATCH] fix(linter): false positive in `jest/expect-expect` (#7341) closes #7237 --- .../src/rules/jest/expect_expect.rs | 75 ++++++++++--------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/crates/oxc_linter/src/rules/jest/expect_expect.rs b/crates/oxc_linter/src/rules/jest/expect_expect.rs index c95e72c1fffd7..2d283329b314e 100644 --- a/crates/oxc_linter/src/rules/jest/expect_expect.rs +++ b/crates/oxc_linter/src/rules/jest/expect_expect.rs @@ -248,16 +248,21 @@ fn check_statements<'a>( visited: &mut FxHashSet, ctx: &LintContext<'a>, ) -> bool { - statements.iter().any(|statement| { - if let Statement::ExpressionStatement(expr_stmt) = statement { - return check_assert_function_used( - &expr_stmt.expression, - assert_function_names, - visited, - ctx, - ); + statements.iter().any(|statement| match statement { + Statement::ExpressionStatement(expr_stmt) => { + check_assert_function_used(&expr_stmt.expression, assert_function_names, visited, ctx) } - false + Statement::BlockStatement(block_stmt) => { + check_statements(&block_stmt.body, assert_function_names, visited, ctx) + } + Statement::IfStatement(if_stmt) => { + if let Statement::BlockStatement(block_stmt) = &if_stmt.consequent { + check_statements(&block_stmt.body, assert_function_names, visited, ctx) + } else { + false + } + } + _ => false, }) } @@ -287,32 +292,6 @@ fn convert_pattern(pattern: &str) -> CompactStr { format!("(?ui)^{pattern}(\\.|$)").into() } -#[test] -fn debug() { - use crate::tester::Tester; - - let mut pass: Vec<(&str, Option)> = vec![]; - - let mut fail = vec![]; - - let pass_vitest = vec![( - " - import { test } from 'vitest'; - test.skip(\"skipped test\", () => {}) - ", - None, - )]; - - let fail_vitest = vec![]; - - pass.extend(pass_vitest); - fail.extend(fail_vitest); - - Tester::new(ExpectExpect::NAME, pass, fail) - .with_jest_plugin(true) - .with_vitest_plugin(true) - .test(); -} #[test] fn test() { use crate::tester::Tester; @@ -438,6 +417,32 @@ fn test() { "#, None, ), + ( + r#" + it("should not warn on await expect", async () => { + if(true) { + const asyncFunction = async () => { + throw new Error('nope') + }; + await expect(asyncFunction()).rejects.toThrow(); + } + }); + "#, + None, + ), + ( + r#" + it("should not warn on await expect", async () => { + { + const asyncFunction = async () => { + throw new Error('nope') + }; + await expect(asyncFunction()).rejects.toThrow(); + } + }); + "#, + None, + ), ]; let mut fail = vec![