Skip to content

Commit 6efe457

Browse files
committed
fix(linter/no-empty-function): respect allow options for functions and arrow functions (#12814)
fixes #12161
1 parent 9b35600 commit 6efe457

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ impl TryFrom<&str> for Allowed {
8585
"getters" | "getter" => Ok(Self::Getters),
8686
"setters" | "setter" => Ok(Self::Setters),
8787
"constructors" | "constructor" => Ok(Self::Constructors),
88+
"asyncFunctions" | "async-functions" => Ok(Self::AsyncFunctions),
89+
"asyncMethods" | "async-methods" => Ok(Self::AsyncMethods),
8890
"private-constructors" | "privateConstructors" => Ok(Self::PrivateConstructor),
8991
"protected-constructors" | "protectedConstructors" => Ok(Self::ProtectedConstructor),
9092
"decoratedFunctions" | "decorated-functions" => Ok(Self::DecoratedFunction),
@@ -229,11 +231,37 @@ impl NoEmptyFunction {
229231
match parent {
230232
AstKind::Function(f) => {
231233
if let Some(name) = f.name() {
232-
let kind = if f.generator { "generator function" } else { "function" };
234+
let is_generator = f.generator;
235+
let is_async = f.r#async;
236+
237+
if is_generator && self.allow.contains(Allowed::GeneratorFunctions) {
238+
return ViolationInfo::default();
239+
}
240+
if is_async && self.allow.contains(Allowed::AsyncFunctions) {
241+
return ViolationInfo::default();
242+
}
243+
if !is_generator && !is_async && self.allow.contains(Allowed::Function) {
244+
return ViolationInfo::default();
245+
}
246+
247+
let kind = if is_async {
248+
"async function"
249+
} else if is_generator {
250+
"generator function"
251+
} else {
252+
"function"
253+
};
233254
return (kind, Some(name.into())).into();
234255
}
235256
}
236-
AstKind::ArrowFunctionExpression(_) => {}
257+
AstKind::ArrowFunctionExpression(arrow) => {
258+
if self.allow.contains(Allowed::ArrowFunction) {
259+
return ViolationInfo::default();
260+
}
261+
if arrow.r#async && self.allow.contains(Allowed::AsyncFunctions) {
262+
return ViolationInfo::default();
263+
}
264+
}
237265
AstKind::IdentifierName(IdentifierName { name, .. })
238266
| AstKind::IdentifierReference(IdentifierReference { name, .. }) => {
239267
return ("function", Some(Cow::Borrowed(name.as_str()))).into();
@@ -301,6 +329,11 @@ impl NoEmptyFunction {
301329
MethodDefinitionKind::Get => self.allow.contains(Allowed::Getters),
302330
MethodDefinitionKind::Set => self.allow.contains(Allowed::Setters),
303331
MethodDefinitionKind::Method => {
332+
if method.value.r#async && self.allow.contains(Allowed::AsyncMethods)
333+
|| method.value.generator && self.allow.contains(Allowed::GeneratorMethods)
334+
{
335+
return true;
336+
}
304337
self.allow.contains(Allowed::Methods)
305338
|| (method.r#override && self.allow.contains(Allowed::OverrideMethod))
306339
}
@@ -434,6 +467,18 @@ fn test() {
434467
"class Foo extends Base { override foo() {} }",
435468
Some(serde_json::json!([{ "allow": ["overrideMethods"] }])),
436469
),
470+
// Test allow option for functions and arrow functions
471+
("function foo() {}", Some(serde_json::json!([{ "allow": ["functions"] }]))),
472+
("const bar = () => {};", Some(serde_json::json!([{ "allow": ["arrowFunctions"] }]))),
473+
(
474+
"const foo = () => {}; function bar() {}",
475+
Some(serde_json::json!([{ "allow": ["arrowFunctions", "functions"] }])),
476+
),
477+
("function* gen() {}", Some(serde_json::json!([{ "allow": ["generatorFunctions"] }]))),
478+
("async function foo() {}", Some(serde_json::json!([{ "allow": ["asyncFunctions"] }]))),
479+
("const foo = async () => {};", Some(serde_json::json!([{ "allow": ["asyncFunctions"] }]))),
480+
("class Foo { async bar() {} }", Some(serde_json::json!([{ "allow": ["asyncMethods"] }]))),
481+
("class Foo { *gen() {} }", Some(serde_json::json!([{ "allow": ["generatorMethods"] }]))),
437482
// extras added by oxc team
438483
("declare function foo(x: number): void;", None),
439484
];

0 commit comments

Comments
 (0)