feat(ast): add Function::name() (#5361)

This commit is contained in:
DonIsaac 2024-08-31 03:45:45 +00:00
parent afb038e93e
commit 180b1a17fb
4 changed files with 23 additions and 29 deletions

View file

@ -1050,6 +1050,12 @@ impl<'a> Function<'a> {
} }
} }
/// Returns this [`Function`]'s name, if it has one.
#[inline]
pub fn name(&self) -> Option<Atom<'a>> {
self.id.as_ref().map(|id| id.name.clone())
}
pub fn is_typescript_syntax(&self) -> bool { pub fn is_typescript_syntax(&self) -> bool {
matches!( matches!(
self.r#type, self.r#type,

View file

@ -426,11 +426,11 @@ impl<'a> IsolatedDeclarations<'a> {
} }
} }
Some(Declaration::FunctionDeclaration(func)) => { Some(Declaration::FunctionDeclaration(func)) => {
if let Some(id) = func.id.as_ref() { if let Some(name) = func.name() {
assignable_properties_for_namespace assignable_properties_for_namespace
.entry(&ident.name) .entry(&ident.name)
.or_default() .or_default()
.insert(id.name.clone()); .insert(name);
} }
} }
Some(Declaration::ClassDeclaration(cls)) => { Some(Declaration::ClassDeclaration(cls)) => {
@ -487,17 +487,17 @@ impl<'a> IsolatedDeclarations<'a> {
&decl.declaration &decl.declaration
{ {
if func.body.is_some() { if func.body.is_some() {
if let Some(id) = func.id.as_ref() { if let Some(name) = func.name() {
can_expando_function_names.insert(id.name.clone()); can_expando_function_names.insert(name);
} }
} }
} }
} }
Statement::FunctionDeclaration(func) => { Statement::FunctionDeclaration(func) => {
if func.body.is_some() { if func.body.is_some() {
if let Some(id) = func.id.as_ref() { if let Some(name) = func.name() {
if self.scope.has_reference(&id.name) { if self.scope.has_reference(&name) {
can_expando_function_names.insert(id.name.clone()); can_expando_function_names.insert(name);
} }
} }
} }

View file

@ -42,7 +42,7 @@ enum FuncNamesConfig {
impl FuncNamesConfig { impl FuncNamesConfig {
fn is_invalid_function(self, func: &Function, parent_node: &AstNode<'_>) -> bool { 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 { match self {
Self::Never => func_name.is_some() && func.r#type != FunctionType::FunctionDeclaration, 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) 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<Cow<'a, str>> { fn get_property_key_name<'a>(key: &PropertyKey<'a>) -> Option<Cow<'a, str>> {
if matches!(key, PropertyKey::NullLiteral(_)) { if matches!(key, PropertyKey::NullLiteral(_)) {
return Some("null".into()); 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) { } else if let Some(static_name) = get_static_property_name(parent_node) {
tokens.push(static_name); 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())); tokens.push(Cow::Borrowed(name.as_str()));
} }
} }
_ => { _ => {
if let Some(static_name) = get_static_property_name(parent_node) { if let Some(static_name) = get_static_property_name(parent_node) {
tokens.push(static_name); 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())); 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 // check at first if the callee calls an invalid function
if !invalid_funcs if !invalid_funcs
.iter() .iter()
.filter_map(|(func, _)| get_function_name(func)) .filter_map(|(func, _)| func.name())
.any(|func_name| func_name == &identifier.name) .any(|func_name| func_name == identifier.name)
{ {
continue; continue;
} }
@ -409,11 +402,9 @@ impl Rule for FuncNames {
let ast_span = ctx.nodes().iter_parents(node.id()).skip(1).find_map(|p| { let ast_span = ctx.nodes().iter_parents(node.id()).skip(1).find_map(|p| {
match p.kind() { match p.kind() {
AstKind::Function(func) => { AstKind::Function(func) => {
let func_name = get_function_name(func); let func_name = func.name()?;
func_name?; if func_name == identifier.name {
if *func_name.unwrap() == identifier.name {
return Some(func.span); return Some(func.span);
} }
@ -434,7 +425,6 @@ impl Rule for FuncNames {
} }
for (func, parent_node) in &invalid_funcs { 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 func_name_complete = get_function_name_with_kind(func, parent_node);
let report_span = Span::new(func.span.start, func.params.span.start); let report_span = Span::new(func.span.start, func.params.span.start);
@ -444,10 +434,10 @@ impl Rule for FuncNames {
.as_ref() .as_ref()
.map_or_else(|| func.params.span.start, |tp| tp.span.start), .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( ctx.diagnostic_with_suggestion(
named_diagnostic(&func_name_complete, report_span), 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 { } else {
ctx.diagnostic_with_fix( ctx.diagnostic_with_fix(

View file

@ -353,9 +353,7 @@ fn is_somewhere_inside_component_or_hook(nodes: &AstNodes, node_id: AstNodeId) -
( (
node.id(), node.id(),
match node.kind() { match node.kind() {
AstKind::Function(func) => { AstKind::Function(func) => func.name().map(Cow::from),
func.id.as_ref().map(|it| Cow::Borrowed(it.name.as_str()))
}
AstKind::ArrowFunctionExpression(_) => { AstKind::ArrowFunctionExpression(_) => {
get_declaration_identifier(nodes, node.id()) get_declaration_identifier(nodes, node.id())
} }