mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
perf(linter): use CompactStr for get_node_name in Jest rules (#6403)
This commit is contained in:
parent
95892ecc86
commit
889400c77d
4 changed files with 14 additions and 17 deletions
|
|
@ -127,7 +127,7 @@ fn run<'a>(
|
||||||
possible_jest_node,
|
possible_jest_node,
|
||||||
ctx,
|
ctx,
|
||||||
&[JestFnKind::General(JestGeneralFnKind::Test)],
|
&[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() {
|
if let Some(member_expr) = call_expr.callee.as_member_expression() {
|
||||||
let Some(property_name) = member_expr.static_property_name() else {
|
let Some(property_name) = member_expr.static_property_name() else {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use oxc_ast::AstKind;
|
||||||
use oxc_diagnostics::OxcDiagnostic;
|
use oxc_diagnostics::OxcDiagnostic;
|
||||||
use oxc_macros::declare_oxc_lint;
|
use oxc_macros::declare_oxc_lint;
|
||||||
use oxc_semantic::NodeId;
|
use oxc_semantic::NodeId;
|
||||||
use oxc_span::Span;
|
use oxc_span::{CompactStr, Span};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
@ -28,7 +28,7 @@ pub struct NoStandaloneExpect(Box<NoStandaloneExpectConfig>);
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct NoStandaloneExpectConfig {
|
pub struct NoStandaloneExpectConfig {
|
||||||
additional_test_block_functions: Vec<String>,
|
additional_test_block_functions: Vec<CompactStr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Deref for NoStandaloneExpect {
|
impl std::ops::Deref for NoStandaloneExpect {
|
||||||
|
|
@ -65,9 +65,7 @@ impl Rule for NoStandaloneExpect {
|
||||||
.get(0)
|
.get(0)
|
||||||
.and_then(|v| v.get("additionalTestBlockFunctions"))
|
.and_then(|v| v.get("additionalTestBlockFunctions"))
|
||||||
.and_then(serde_json::Value::as_array)
|
.and_then(serde_json::Value::as_array)
|
||||||
.map(|v| {
|
.map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect())
|
||||||
v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect()
|
|
||||||
})
|
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
Self(Box::new(NoStandaloneExpectConfig { additional_test_block_functions }))
|
Self(Box::new(NoStandaloneExpectConfig { additional_test_block_functions }))
|
||||||
|
|
@ -128,7 +126,7 @@ impl NoStandaloneExpect {
|
||||||
|
|
||||||
fn is_correct_place_to_call_expect<'a>(
|
fn is_correct_place_to_call_expect<'a>(
|
||||||
node: &AstNode<'a>,
|
node: &AstNode<'a>,
|
||||||
additional_test_block_functions: &[String],
|
additional_test_block_functions: &[CompactStr],
|
||||||
id_nodes_mapping: &FxHashMap<NodeId, &PossibleJestNode<'a, '_>>,
|
id_nodes_mapping: &FxHashMap<NodeId, &PossibleJestNode<'a, '_>>,
|
||||||
ctx: &LintContext<'a>,
|
ctx: &LintContext<'a>,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
|
|
@ -196,7 +194,7 @@ fn is_correct_place_to_call_expect<'a>(
|
||||||
|
|
||||||
fn is_var_declarator_or_test_block<'a>(
|
fn is_var_declarator_or_test_block<'a>(
|
||||||
node: &AstNode<'a>,
|
node: &AstNode<'a>,
|
||||||
additional_test_block_functions: &[String],
|
additional_test_block_functions: &[CompactStr],
|
||||||
id_nodes_mapping: &FxHashMap<NodeId, &PossibleJestNode<'a, '_>>,
|
id_nodes_mapping: &FxHashMap<NodeId, &PossibleJestNode<'a, '_>>,
|
||||||
ctx: &LintContext<'a>,
|
ctx: &LintContext<'a>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
|
@ -213,7 +211,7 @@ fn is_var_declarator_or_test_block<'a>(
|
||||||
}
|
}
|
||||||
|
|
||||||
let node_name = get_node_name(&call_expr.callee);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use oxc_ast::{
|
||||||
use oxc_diagnostics::OxcDiagnostic;
|
use oxc_diagnostics::OxcDiagnostic;
|
||||||
use oxc_macros::declare_oxc_lint;
|
use oxc_macros::declare_oxc_lint;
|
||||||
use oxc_semantic::AstNode;
|
use oxc_semantic::AstNode;
|
||||||
use oxc_span::Span;
|
use oxc_span::{CompactStr, Span};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::LintContext,
|
context::LintContext,
|
||||||
|
|
@ -25,7 +25,7 @@ fn use_hook(span: Span) -> OxcDiagnostic {
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct RequireHookConfig {
|
pub struct RequireHookConfig {
|
||||||
allowed_function_calls: Vec<String>,
|
allowed_function_calls: Vec<CompactStr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
|
|
@ -153,9 +153,7 @@ impl Rule for RequireHook {
|
||||||
.get(0)
|
.get(0)
|
||||||
.and_then(|config| config.get("allowedFunctionCalls"))
|
.and_then(|config| config.get("allowedFunctionCalls"))
|
||||||
.and_then(serde_json::Value::as_array)
|
.and_then(serde_json::Value::as_array)
|
||||||
.map(|v| {
|
.map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect())
|
||||||
v.iter().filter_map(serde_json::Value::as_str).map(ToString::to_string).collect()
|
|
||||||
})
|
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
Self(Box::new(RequireHookConfig { allowed_function_calls }))
|
Self(Box::new(RequireHookConfig { allowed_function_calls }))
|
||||||
|
|
@ -230,7 +228,7 @@ impl RequireHook {
|
||||||
ctx: &LintContext<'a>,
|
ctx: &LintContext<'a>,
|
||||||
) {
|
) {
|
||||||
if let Expression::CallExpression(call_expr) = expr {
|
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)
|
if !(parse_jest_fn_call(call_expr, &PossibleJestNode { node, original: None }, ctx)
|
||||||
.is_some()
|
.is_some()
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use oxc_ast::{
|
||||||
AstKind,
|
AstKind,
|
||||||
};
|
};
|
||||||
use oxc_semantic::{AstNode, ReferenceId};
|
use oxc_semantic::{AstNode, ReferenceId};
|
||||||
|
use oxc_span::CompactStr;
|
||||||
use phf::phf_set;
|
use phf::phf_set;
|
||||||
|
|
||||||
use crate::LintContext;
|
use crate::LintContext;
|
||||||
|
|
@ -254,9 +255,9 @@ fn collect_ids_referenced_to_global<'c>(
|
||||||
/// join name of the expression. e.g.
|
/// join name of the expression. e.g.
|
||||||
/// `expect(foo).toBe(bar)` -> "expect.toBe"
|
/// `expect(foo).toBe(bar)` -> "expect.toBe"
|
||||||
/// `new Foo().bar` -> "Foo.bar"
|
/// `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);
|
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>> {
|
pub fn get_node_name_vec<'a>(expr: &'a Expression<'a>) -> Vec<Cow<'a, str>> {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue