-
-
Notifications
You must be signed in to change notification settings - Fork 481
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(useExplicitFunctionReturnType): support higher-order function #4117
Conversation
…ed statements) in useExplicitFunctionReturnType
456e464
to
dd41388
Compare
CodSpeed Performance ReportMerging #4117 will improve performances by 7.61%Comparing Summary
Benchmarks breakdown
|
Isolated Declarations and the ESLint rule seem to recognize these patterns. I think the reason d'être for allowing higher order function is to simplify the writing of functional code with curried functions. It is fulfilled by checking only body with a single return statement We can still reevaluate this decision once users make the case for it. |
I agree with you, adding this could introduce a lot of complexity. Since we can always reevaluate later based on user feedback, I think this PR is ready for review. |
@@ -110,6 +121,15 @@ declare_lint_rule! { | |||
/// (() => {})(); | |||
/// ``` | |||
/// | |||
/// ```ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is good to add examples to demonstrate the exceptions to the rule. However, I think we should also describe the exceptions to the rule in the description (before ## Examples
). We should also document the previous exceptions to the rule introduced in your previous PRs.
statements.into_iter().any(|statement| { | ||
if let AnyJsStatement::JsReturnStatement(return_stmt) = statement { | ||
if let Some(args) = return_stmt.argument() { | ||
return matches!( | ||
args, | ||
AnyJsExpression::JsFunctionExpression(_) | ||
| AnyJsExpression::JsArrowFunctionExpression(_) | ||
); | ||
} | ||
} | ||
false | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should check only the first statement because we have to report code like:
function f() {
if (x) {
return 0;
}
return () => {}
}
Don't forget to add a test to avoid any regression here.
…statement_function_return
Summary
related: #2017
This PR introduces the ability to detect higher-order functions in the
useExplicitFunctionReturnType
rule, specifically functions that return other functions.Limitations:
Nested statement handling is not implemented: The current implementation does not cover cases where functions are returned inside conditional or branching logic (e.g., if statements or switch statements).
Example of unsupported case:
Question:
Should biome support nested statements like if or switch when checking for higher-order functions?
This would involve inspecting return statements within those control structures, adding complexity but potentially broadening the detection capability.