diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index 3ffb6e937256a..e08c99fffc87b 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -1050,6 +1050,12 @@ impl<'a> Function<'a> { } } + /// Returns this [`Function`]'s name, if it has one. + #[inline] + pub fn name(&self) -> Option> { + self.id.as_ref().map(|id| id.name.clone()) + } + pub fn is_typescript_syntax(&self) -> bool { matches!( self.r#type, diff --git a/crates/oxc_isolated_declarations/src/lib.rs b/crates/oxc_isolated_declarations/src/lib.rs index 492b1de932021..129c4cb46bef4 100644 --- a/crates/oxc_isolated_declarations/src/lib.rs +++ b/crates/oxc_isolated_declarations/src/lib.rs @@ -426,11 +426,11 @@ impl<'a> IsolatedDeclarations<'a> { } } Some(Declaration::FunctionDeclaration(func)) => { - if let Some(id) = func.id.as_ref() { + if let Some(name) = func.name() { assignable_properties_for_namespace .entry(&ident.name) .or_default() - .insert(id.name.clone()); + .insert(name); } } Some(Declaration::ClassDeclaration(cls)) => { @@ -487,17 +487,17 @@ impl<'a> IsolatedDeclarations<'a> { &decl.declaration { if func.body.is_some() { - if let Some(id) = func.id.as_ref() { - can_expando_function_names.insert(id.name.clone()); + if let Some(name) = func.name() { + can_expando_function_names.insert(name); } } } } Statement::FunctionDeclaration(func) => { if func.body.is_some() { - if let Some(id) = func.id.as_ref() { - if self.scope.has_reference(&id.name) { - can_expando_function_names.insert(id.name.clone()); + if let Some(name) = func.name() { + if self.scope.has_reference(&name) { + can_expando_function_names.insert(name); } } } diff --git a/crates/oxc_linter/src/rules/eslint/func_names.rs b/crates/oxc_linter/src/rules/eslint/func_names.rs index 7bc9de67816e9..1e805c0123aad 100644 --- a/crates/oxc_linter/src/rules/eslint/func_names.rs +++ b/crates/oxc_linter/src/rules/eslint/func_names.rs @@ -42,7 +42,7 @@ enum FuncNamesConfig { impl FuncNamesConfig { fn is_invalid_function(self, func: &Function, parent_node: &AstNode<'_>) -> bool { - let func_name = get_function_name(func); + let func_name = func.name(); match self { Self::Never => func_name.is_some() && func.r#type != FunctionType::FunctionDeclaration, @@ -230,13 +230,6 @@ fn get_function_identifier<'a>(func: &'a Function<'a>) -> Option<&'a Span> { func.id.as_ref().map(|id| &id.span) } -/** - * Gets the identifier name of the function - */ -fn get_function_name<'f, 'a>(func: &'f Function<'a>) -> Option<&'f Atom<'a>> { - func.id.as_ref().map(|id| &id.name) -} - fn get_property_key_name<'a>(key: &PropertyKey<'a>) -> Option> { if matches!(key, PropertyKey::NullLiteral(_)) { return Some("null".into()); @@ -342,14 +335,14 @@ fn get_function_name_with_kind<'a>(func: &Function<'a>, parent_node: &AstNode<'a } } else if let Some(static_name) = get_static_property_name(parent_node) { tokens.push(static_name); - } else if let Some(name) = get_function_name(func) { + } else if let Some(name) = func.name() { tokens.push(Cow::Borrowed(name.as_str())); } } _ => { if let Some(static_name) = get_static_property_name(parent_node) { tokens.push(static_name); - } else if let Some(name) = get_function_name(func) { + } else if let Some(name) = func.name() { tokens.push(Cow::Borrowed(name.as_str())); } } @@ -399,8 +392,8 @@ impl Rule for FuncNames { // check at first if the callee calls an invalid function if !invalid_funcs .iter() - .filter_map(|(func, _)| get_function_name(func)) - .any(|func_name| func_name == &identifier.name) + .filter_map(|(func, _)| func.name()) + .any(|func_name| func_name == identifier.name) { continue; } @@ -409,11 +402,9 @@ impl Rule for FuncNames { let ast_span = ctx.nodes().iter_parents(node.id()).skip(1).find_map(|p| { match p.kind() { AstKind::Function(func) => { - let func_name = get_function_name(func); - - func_name?; + let func_name = func.name()?; - if *func_name.unwrap() == identifier.name { + if func_name == identifier.name { return Some(func.span); } @@ -434,7 +425,6 @@ impl Rule for FuncNames { } for (func, parent_node) in &invalid_funcs { - let func_name = get_function_name(func); let func_name_complete = get_function_name_with_kind(func, parent_node); let report_span = Span::new(func.span.start, func.params.span.start); @@ -444,10 +434,10 @@ impl Rule for FuncNames { .as_ref() .map_or_else(|| func.params.span.start, |tp| tp.span.start), ); - if func_name.is_some() { + if let Some(id) = func.id.as_ref() { ctx.diagnostic_with_suggestion( named_diagnostic(&func_name_complete, report_span), - |fixer| func.id.as_ref().map_or(fixer.noop(), |id| fixer.delete(id)), + |fixer| fixer.delete(id), ); } else { ctx.diagnostic_with_fix( diff --git a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs index 873709c9a67f1..aa57d721cf580 100644 --- a/crates/oxc_linter/src/rules/react/rules_of_hooks.rs +++ b/crates/oxc_linter/src/rules/react/rules_of_hooks.rs @@ -353,9 +353,7 @@ fn is_somewhere_inside_component_or_hook(nodes: &AstNodes, node_id: AstNodeId) - ( node.id(), match node.kind() { - AstKind::Function(func) => { - func.id.as_ref().map(|it| Cow::Borrowed(it.name.as_str())) - } + AstKind::Function(func) => func.name().map(Cow::from), AstKind::ArrowFunctionExpression(_) => { get_declaration_identifier(nodes, node.id()) }