|
| 1 | +use std::borrow::Cow; |
| 2 | + |
1 | 3 | use oxc_ast::{ |
2 | 4 | AstKind, |
3 | | - ast::ObjectAssignmentTarget, |
4 | 5 | ast::{ |
5 | 6 | AssignmentTarget, AssignmentTargetProperty, BindingPatternKind, Expression, Function, |
6 | | - FunctionType, PropertyKind, |
| 7 | + FunctionType, ObjectAssignmentTarget, PropertyKind, |
7 | 8 | }, |
8 | 9 | }; |
9 | 10 | use oxc_diagnostics::OxcDiagnostic; |
10 | 11 | use oxc_macros::declare_oxc_lint; |
11 | 12 | use oxc_semantic::NodeId; |
12 | 13 | use oxc_span::{Atom, GetSpan, Span}; |
13 | 14 | use oxc_syntax::identifier::is_identifier_name; |
14 | | -use std::borrow::Cow; |
15 | 15 |
|
16 | | -use crate::fixer::{RuleFix, RuleFixer}; |
17 | | -use crate::{AstNode, ast_util::get_function_name_with_kind, context::LintContext, rule::Rule}; |
| 16 | +use crate::{ |
| 17 | + AstNode, |
| 18 | + ast_util::get_function_name_with_kind, |
| 19 | + context::LintContext, |
| 20 | + fixer::{RuleFix, RuleFixer}, |
| 21 | + rule::Rule, |
| 22 | +}; |
18 | 23 |
|
19 | 24 | fn named_diagnostic(function_name: &str, span: Span) -> OxcDiagnostic { |
20 | 25 | OxcDiagnostic::warn(format!("Unexpected named {function_name}.")) |
@@ -242,12 +247,7 @@ impl FuncNames { |
242 | 247 | // check if the calling function is inside its own body |
243 | 248 | // then, remove it from invalid_functions because recursion are always named |
244 | 249 | AstKind::CallExpression(expression) => { |
245 | | - retain_recursive_function_from_invalid_functions( |
246 | | - &mut invalid_functions, |
247 | | - expression, |
248 | | - node, |
249 | | - ctx, |
250 | | - ); |
| 250 | + remove_recursive_functions(&mut invalid_functions, expression, node, ctx); |
251 | 251 | } |
252 | 252 | _ => {} |
253 | 253 | } |
@@ -280,7 +280,7 @@ impl Rule for FuncNames { |
280 | 280 | } |
281 | 281 | } |
282 | 282 |
|
283 | | -fn retain_recursive_function_from_invalid_functions( |
| 283 | +fn remove_recursive_functions( |
284 | 284 | invalid_functions: &mut Vec<(&Function, &AstNode, &AstNode)>, |
285 | 285 | expression: &oxc_ast::ast::CallExpression, |
286 | 286 | node: &AstNode, |
@@ -363,16 +363,17 @@ fn is_object_or_class_method(parent_node: &AstNode) -> bool { |
363 | 363 | } |
364 | 364 | } |
365 | 365 |
|
366 | | -fn does_object_assignment_target_have_name(target: &ObjectAssignmentTarget) -> bool { |
| 366 | +fn has_object_assignment_target_name<'a>( |
| 367 | + target: &ObjectAssignmentTarget<'a>, |
| 368 | + function: &Function<'a>, |
| 369 | +) -> bool { |
367 | 370 | target.properties.iter().any(|property| { |
368 | | - matches!( |
369 | | - property, |
370 | | - AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(identifier) |
371 | | - if matches!( |
372 | | - identifier.init, |
373 | | - Some(Expression::FunctionExpression(_)) |
374 | | - ) |
375 | | - ) |
| 371 | + if let AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(identifier) = property { |
| 372 | + if let Some(Expression::FunctionExpression(func_expr)) = &identifier.init { |
| 373 | + return get_function_identifier(func_expr) == get_function_identifier(function); |
| 374 | + } |
| 375 | + } |
| 376 | + false |
376 | 377 | }) |
377 | 378 | } |
378 | 379 |
|
@@ -407,7 +408,9 @@ fn has_inferred_name<'a>(function: &Function<'a>, parent_node: &AstNode<'a>) -> |
407 | 408 | AstKind::AssignmentTargetPropertyIdentifier(ident) => { |
408 | 409 | ident.init.as_ref().is_some_and(|expr| is_same_function(expr, function)) |
409 | 410 | } |
410 | | - AstKind::ObjectAssignmentTarget(target) => does_object_assignment_target_have_name(target), |
| 411 | + AstKind::ObjectAssignmentTarget(target) => { |
| 412 | + has_object_assignment_target_name(target, function) |
| 413 | + } |
411 | 414 | _ => false, |
412 | 415 | } |
413 | 416 | } |
|
0 commit comments