Skip to content

Commit 5e014cb

Browse files
committed
perf(linter): refactor jest/require-hook to use top-level match (#14693)
Single `match` is simpler and enables node type analysis.
1 parent c8d775c commit 5e014cb

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,8 @@ impl RuleRunner for crate::rules::jest::prefer_todo::PreferTodo {
14271427
}
14281428

14291429
impl RuleRunner for crate::rules::jest::require_hook::RequireHook {
1430-
const NODE_TYPES: Option<&AstTypesBitset> = None;
1430+
const NODE_TYPES: Option<&AstTypesBitset> =
1431+
Some(&AstTypesBitset::from_types(&[AstType::CallExpression, AstType::Program]));
14311432
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
14321433
}
14331434

crates/oxc_linter/src/rules/jest/require_hook.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,34 +173,35 @@ impl Rule for RequireHook {
173173
}
174174

175175
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
176-
let kind = node.kind();
177-
178-
if let AstKind::Program(program) = kind {
179-
self.check_block_body(node, &program.body, ctx);
180-
} else if let AstKind::CallExpression(call_expr) = kind {
181-
if !is_type_of_jest_fn_call(
182-
call_expr,
183-
&PossibleJestNode { node, original: None },
184-
ctx,
185-
&[JestFnKind::General(JestGeneralFnKind::Describe)],
186-
) || call_expr.arguments.len() < 2
187-
{
188-
return;
176+
match node.kind() {
177+
AstKind::Program(program) => {
178+
self.check_block_body(node, &program.body, ctx);
189179
}
190-
191-
match &call_expr.arguments[1] {
192-
Argument::FunctionExpression(func_expr) => {
193-
if let Some(func_body) = &func_expr.body {
194-
self.check_block_body(node, &func_body.statements, ctx);
195-
}
180+
AstKind::CallExpression(call_expr) => {
181+
if !is_type_of_jest_fn_call(
182+
call_expr,
183+
&PossibleJestNode { node, original: None },
184+
ctx,
185+
&[JestFnKind::General(JestGeneralFnKind::Describe)],
186+
) || call_expr.arguments.len() < 2
187+
{
188+
return;
196189
}
197-
Argument::ArrowFunctionExpression(arrow_func_expr) => {
198-
if !arrow_func_expr.expression {
199-
self.check_block_body(node, &arrow_func_expr.body.statements, ctx);
190+
match &call_expr.arguments[1] {
191+
Argument::FunctionExpression(func_expr) => {
192+
if let Some(func_body) = &func_expr.body {
193+
self.check_block_body(node, &func_body.statements, ctx);
194+
}
195+
}
196+
Argument::ArrowFunctionExpression(arrow_func_expr) => {
197+
if !arrow_func_expr.expression {
198+
self.check_block_body(node, &arrow_func_expr.body.statements, ctx);
199+
}
200200
}
201+
_ => (),
201202
}
202-
_ => (),
203203
}
204+
_ => {}
204205
}
205206
}
206207
}

0 commit comments

Comments
 (0)