diff --git a/crates/oxc_linter/src/rules/jest/expect_expect.rs b/crates/oxc_linter/src/rules/jest/expect_expect.rs index a4127b74d9b90c..3721630b1b7e5a 100644 --- a/crates/oxc_linter/src/rules/jest/expect_expect.rs +++ b/crates/oxc_linter/src/rules/jest/expect_expect.rs @@ -127,7 +127,7 @@ fn run<'a>( possible_jest_node, ctx, &[JestFnKind::General(JestGeneralFnKind::Test)], - ) || rule.additional_test_block_functions.contains(&name.into()) + ) || rule.additional_test_block_functions.contains(&name) { if let Some(member_expr) = call_expr.callee.as_member_expression() { let Some(property_name) = member_expr.static_property_name() else { diff --git a/crates/oxc_linter/src/rules/jest/no_standalone_expect.rs b/crates/oxc_linter/src/rules/jest/no_standalone_expect.rs index 01b682b744f544..b9eebaf72a7208 100644 --- a/crates/oxc_linter/src/rules/jest/no_standalone_expect.rs +++ b/crates/oxc_linter/src/rules/jest/no_standalone_expect.rs @@ -2,7 +2,7 @@ use oxc_ast::AstKind; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_semantic::NodeId; -use oxc_span::Span; +use oxc_span::{CompactStr, Span}; use rustc_hash::FxHashMap; use crate::{ @@ -28,7 +28,7 @@ pub struct NoStandaloneExpect(Box); #[derive(Debug, Default, Clone)] pub struct NoStandaloneExpectConfig { - additional_test_block_functions: Vec, + additional_test_block_functions: Vec, } impl std::ops::Deref for NoStandaloneExpect { @@ -65,9 +65,7 @@ impl Rule for NoStandaloneExpect { .get(0) .and_then(|v| v.get("additionalTestBlockFunctions")) .and_then(serde_json::Value::as_array) - .map(|v| { - v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect() - }) + .map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect()) .unwrap_or_default(); Self(Box::new(NoStandaloneExpectConfig { additional_test_block_functions })) @@ -128,7 +126,7 @@ impl NoStandaloneExpect { fn is_correct_place_to_call_expect<'a>( node: &AstNode<'a>, - additional_test_block_functions: &[String], + additional_test_block_functions: &[CompactStr], id_nodes_mapping: &FxHashMap>, ctx: &LintContext<'a>, ) -> Option<()> { @@ -196,7 +194,7 @@ fn is_correct_place_to_call_expect<'a>( fn is_var_declarator_or_test_block<'a>( node: &AstNode<'a>, - additional_test_block_functions: &[String], + additional_test_block_functions: &[CompactStr], id_nodes_mapping: &FxHashMap>, ctx: &LintContext<'a>, ) -> bool { @@ -213,7 +211,7 @@ fn is_var_declarator_or_test_block<'a>( } let node_name = get_node_name(&call_expr.callee); - if additional_test_block_functions.iter().any(|fn_name| &node_name == fn_name) { + if additional_test_block_functions.iter().any(|fn_name| node_name == fn_name) { return true; } } diff --git a/crates/oxc_linter/src/rules/jest/require_hook.rs b/crates/oxc_linter/src/rules/jest/require_hook.rs index 5ca53d8a0dd706..c98acd9c208917 100644 --- a/crates/oxc_linter/src/rules/jest/require_hook.rs +++ b/crates/oxc_linter/src/rules/jest/require_hook.rs @@ -6,7 +6,7 @@ use oxc_ast::{ use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_semantic::AstNode; -use oxc_span::Span; +use oxc_span::{CompactStr, Span}; use crate::{ context::LintContext, @@ -25,7 +25,7 @@ fn use_hook(span: Span) -> OxcDiagnostic { #[derive(Debug, Default, Clone)] pub struct RequireHookConfig { - allowed_function_calls: Vec, + allowed_function_calls: Vec, } #[derive(Debug, Default, Clone)] @@ -153,9 +153,7 @@ impl Rule for RequireHook { .get(0) .and_then(|config| config.get("allowedFunctionCalls")) .and_then(serde_json::Value::as_array) - .map(|v| { - v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect() - }) + .map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect()) .unwrap_or_default(); Self(Box::new(RequireHookConfig { allowed_function_calls })) @@ -230,7 +228,7 @@ impl RequireHook { ctx: &LintContext<'a>, ) { if let Expression::CallExpression(call_expr) = expr { - let name: String = get_node_name(&call_expr.callee); + let name = get_node_name(&call_expr.callee); if !(parse_jest_fn_call(call_expr, &PossibleJestNode { node, original: None }, ctx) .is_some() diff --git a/crates/oxc_linter/src/utils/jest.rs b/crates/oxc_linter/src/utils/jest.rs index 21d6c8c5d1e560..2479167e509889 100644 --- a/crates/oxc_linter/src/utils/jest.rs +++ b/crates/oxc_linter/src/utils/jest.rs @@ -8,6 +8,7 @@ use oxc_ast::{ AstKind, }; use oxc_semantic::{AstNode, ReferenceId}; +use oxc_span::CompactStr; use phf::phf_set; use crate::LintContext; @@ -254,9 +255,9 @@ fn collect_ids_referenced_to_global<'c>( /// join name of the expression. e.g. /// `expect(foo).toBe(bar)` -> "expect.toBe" /// `new Foo().bar` -> "Foo.bar" -pub fn get_node_name<'a>(expr: &'a Expression<'a>) -> String { +pub fn get_node_name<'a>(expr: &'a Expression<'a>) -> CompactStr { let chain = get_node_name_vec(expr); - chain.join(".") + chain.join(".").into() } pub fn get_node_name_vec<'a>(expr: &'a Expression<'a>) -> Vec> {