Skip to content

Commit 6ad9d4f

Browse files
committed
refactor(linter): tidy eslint/func-names (#10923)
1 parent 6e47699 commit 6ad9d4f

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ fn is_object_or_class_method(parent_node: &AstNode) -> bool {
151151
match parent_node.kind() {
152152
AstKind::MethodDefinition(_) => true,
153153
AstKind::ObjectProperty(property) => {
154-
property.method
155-
|| property.kind == PropertyKind::Get
156-
|| property.kind == PropertyKind::Set
154+
property.method || matches!(property.kind, PropertyKind::Get | PropertyKind::Set)
157155
}
158156
_ => false,
159157
}
@@ -169,37 +167,23 @@ fn has_inferred_name<'a>(function: &Function<'a>, parent_node: &AstNode<'a>) ->
169167
match parent_node.kind() {
170168
AstKind::VariableDeclarator(declarator) => {
171169
matches!(declarator.id.kind, BindingPatternKind::BindingIdentifier(_))
172-
&& matches!(declarator.init.as_ref().unwrap(), Expression::FunctionExpression(function_expression)
173-
if get_function_identifier(function_expression) == get_function_identifier(function)
174-
)
175-
}
176-
AstKind::ObjectProperty(property) => {
177-
matches!(&property.value, Expression::FunctionExpression(function_expression)
178-
if get_function_identifier(function_expression) == get_function_identifier(function)
179-
)
170+
&& declarator.init.as_ref().is_some_and(|init| is_same_function(init, function))
180171
}
172+
AstKind::ObjectProperty(property) => is_same_function(&property.value, function),
181173
AstKind::PropertyDefinition(definition) => {
182-
matches!(&definition.value.as_ref().unwrap(), Expression::FunctionExpression(function_expression)
183-
if get_function_identifier(function_expression) == get_function_identifier(function)
184-
)
174+
definition.value.as_ref().is_some_and(|value| is_same_function(value, function))
185175
}
186176
AstKind::AssignmentExpression(expression) => {
187177
matches!(expression.left, AssignmentTarget::AssignmentTargetIdentifier(_))
188-
&& matches!(&expression.right, Expression::FunctionExpression(function_expression)
189-
if get_function_identifier(function_expression) == get_function_identifier(function)
190-
)
178+
&& is_same_function(&expression.right, function)
191179
}
192180
AstKind::AssignmentTargetWithDefault(target) => {
193181
matches!(target.binding, AssignmentTarget::AssignmentTargetIdentifier(_))
194-
&& matches!(&target.init, Expression::FunctionExpression(function_expression)
195-
if get_function_identifier(function_expression) == get_function_identifier(function)
196-
)
182+
&& is_same_function(&target.init, function)
197183
}
198184
AstKind::AssignmentPattern(pattern) => {
199185
matches!(pattern.left.kind, BindingPatternKind::BindingIdentifier(_))
200-
&& matches!(&pattern.right, Expression::FunctionExpression(function_expression)
201-
if get_function_identifier(function_expression) == get_function_identifier(function)
202-
)
186+
&& is_same_function(&pattern.right, function)
203187
}
204188
AstKind::ObjectAssignmentTarget(target) => {
205189
for property in &target.properties {
@@ -225,6 +209,12 @@ fn has_inferred_name<'a>(function: &Function<'a>, parent_node: &AstNode<'a>) ->
225209
}
226210
}
227211

212+
fn is_same_function<'a>(fn1: &Expression<'a>, fn2: &Function<'a>) -> bool {
213+
matches!(fn1, Expression::FunctionExpression(function_expression)
214+
if get_function_identifier(function_expression) == get_function_identifier(fn2)
215+
)
216+
}
217+
228218
/**
229219
* Gets the identifier for the function
230220
*/
@@ -369,15 +359,12 @@ fn guess_function_name<'a>(ctx: &LintContext<'a>, parent_id: NodeId) -> Option<C
369359
return decl.id.get_identifier_name().as_ref().map(Atom::as_str).map(Cow::Borrowed);
370360
}
371361
AstKind::ObjectProperty(prop) => {
372-
return prop.key.static_name().and_then(|name| {
373-
if is_valid_identifier_name(&name) { Some(name) } else { None }
374-
});
362+
return prop.key.static_name().filter(|name| is_valid_identifier_name(name));
375363
}
376364
AstKind::PropertyDefinition(prop) => {
377-
return prop.key.static_name().and_then(|name| {
378-
if is_valid_identifier_name(&name) { Some(name) } else { None }
379-
});
365+
return prop.key.static_name().filter(|name| is_valid_identifier_name(name));
380366
}
367+
381368
_ => return None,
382369
}
383370
}

0 commit comments

Comments
 (0)