Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
matches!(
self.r#type,
Expand Down
14 changes: 7 additions & 7 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
28 changes: 9 additions & 19 deletions crates/oxc_linter/src/rules/eslint/func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Cow<'a, str>> {
if matches!(key, PropertyKey::NullLiteral(_)) {
return Some("null".into());
Expand Down Expand Up @@ -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()));
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_linter/src/rules/react/rules_of_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down