Skip to content

Commit

Permalink
perf(linter): use CompactStr for get_node_name in Jest rules
Browse files Browse the repository at this point in the history
  • Loading branch information
camchenry authored and DonIsaac committed Oct 10, 2024
1 parent f5f9eae commit 114b8c3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jest/expect_expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions crates/oxc_linter/src/rules/jest/no_standalone_expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -28,7 +28,7 @@ pub struct NoStandaloneExpect(Box<NoStandaloneExpectConfig>);

#[derive(Debug, Default, Clone)]
pub struct NoStandaloneExpectConfig {
additional_test_block_functions: Vec<String>,
additional_test_block_functions: Vec<CompactStr>,
}

impl std::ops::Deref for NoStandaloneExpect {
Expand Down Expand Up @@ -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 }))
Expand Down Expand Up @@ -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<NodeId, &PossibleJestNode<'a, '_>>,
ctx: &LintContext<'a>,
) -> Option<()> {
Expand Down Expand Up @@ -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<NodeId, &PossibleJestNode<'a, '_>>,
ctx: &LintContext<'a>,
) -> bool {
Expand All @@ -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;
}
}
Expand Down
10 changes: 4 additions & 6 deletions crates/oxc_linter/src/rules/jest/require_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,7 +25,7 @@ fn use_hook(span: Span) -> OxcDiagnostic {

#[derive(Debug, Default, Clone)]
pub struct RequireHookConfig {
allowed_function_calls: Vec<String>,
allowed_function_calls: Vec<CompactStr>,
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -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 }))
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use oxc_ast::{
AstKind,
};
use oxc_semantic::{AstNode, ReferenceId};
use oxc_span::CompactStr;
use phf::phf_set;

use crate::LintContext;
Expand Down Expand Up @@ -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<Cow<'a, str>> {
Expand Down

0 comments on commit 114b8c3

Please sign in to comment.