Skip to content

Commit c49dc68

Browse files
committed
fix(linter): correct object assignment target logic in func-names rule
- Fix has_object_assignment_target_name to properly check if the specific function matches - Reorganize imports to follow conventional order (std first, then external crates) - Rename verbose function name for better readability
1 parent 2c63af4 commit c49dc68

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

crates/oxc_linter/src/rules/eslint/func_names.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
use std::borrow::Cow;
2+
13
use oxc_ast::{
24
AstKind,
3-
ast::ObjectAssignmentTarget,
45
ast::{
56
AssignmentTarget, AssignmentTargetProperty, BindingPatternKind, Expression, Function,
6-
FunctionType, PropertyKind,
7+
FunctionType, ObjectAssignmentTarget, PropertyKind,
78
},
89
};
910
use oxc_diagnostics::OxcDiagnostic;
1011
use oxc_macros::declare_oxc_lint;
1112
use oxc_semantic::NodeId;
1213
use oxc_span::{Atom, GetSpan, Span};
1314
use oxc_syntax::identifier::is_identifier_name;
14-
use std::borrow::Cow;
1515

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+
};
1823

1924
fn named_diagnostic(function_name: &str, span: Span) -> OxcDiagnostic {
2025
OxcDiagnostic::warn(format!("Unexpected named {function_name}."))
@@ -242,12 +247,7 @@ impl FuncNames {
242247
// check if the calling function is inside its own body
243248
// then, remove it from invalid_functions because recursion are always named
244249
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);
251251
}
252252
_ => {}
253253
}
@@ -280,7 +280,7 @@ impl Rule for FuncNames {
280280
}
281281
}
282282

283-
fn retain_recursive_function_from_invalid_functions(
283+
fn remove_recursive_functions(
284284
invalid_functions: &mut Vec<(&Function, &AstNode, &AstNode)>,
285285
expression: &oxc_ast::ast::CallExpression,
286286
node: &AstNode,
@@ -363,16 +363,17 @@ fn is_object_or_class_method(parent_node: &AstNode) -> bool {
363363
}
364364
}
365365

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 {
367370
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
376377
})
377378
}
378379

@@ -407,7 +408,9 @@ fn has_inferred_name<'a>(function: &Function<'a>, parent_node: &AstNode<'a>) ->
407408
AstKind::AssignmentTargetPropertyIdentifier(ident) => {
408409
ident.init.as_ref().is_some_and(|expr| is_same_function(expr, function))
409410
}
410-
AstKind::ObjectAssignmentTarget(target) => does_object_assignment_target_have_name(target),
411+
AstKind::ObjectAssignmentTarget(target) => {
412+
has_object_assignment_target_name(target, function)
413+
}
411414
_ => false,
412415
}
413416
}

0 commit comments

Comments
 (0)