-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
Merged
+388
−3
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ use biome_analyze::{ | |
use biome_console::markup; | ||
use biome_js_semantic::HasClosureAstNode; | ||
use biome_js_syntax::{ | ||
AnyJsBinding, AnyJsExpression, AnyJsFunctionBody, AnyTsType, JsFileSource, JsSyntaxKind, | ||
AnyJsBinding, AnyJsExpression, AnyJsFunctionBody, AnyJsStatement, AnyTsType, JsFileSource, | ||
JsStatementList, JsSyntaxKind, | ||
}; | ||
use biome_js_syntax::{ | ||
AnyJsFunction, JsGetterClassMember, JsGetterObjectMember, JsMethodClassMember, | ||
|
@@ -68,6 +69,16 @@ declare_lint_rule! { | |
/// const func = (value: number) => ({ type: 'X', value }) as any; | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// const arrowFn = () => () => {}; | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// const arrowFn = () => { | ||
/// return () => { }; | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// ```ts | ||
/// // No return value should be expected (void) | ||
|
@@ -110,6 +121,15 @@ declare_lint_rule! { | |
/// (() => {})(); | ||
/// ``` | ||
/// | ||
/// ```ts | ||
/// const arrowFn = () => (): void => {}; | ||
/// ``` | ||
/// | ||
/// ```ts | ||
/// const arrowFn = () => { | ||
/// return (): void => { }; | ||
/// } | ||
/// | ||
pub UseExplicitFunctionReturnType { | ||
version: "next", | ||
name: "useExplicitFunctionReturnType", | ||
|
@@ -150,6 +170,10 @@ impl Rule for UseExplicitFunctionReturnType { | |
return None; | ||
} | ||
|
||
if is_higher_order_function(func) { | ||
return None; | ||
} | ||
|
||
let func_range = func.syntax().text_range(); | ||
if let Ok(Some(AnyJsBinding::JsIdentifierBinding(id))) = func.id() { | ||
return Some(TextRange::new( | ||
|
@@ -266,3 +290,65 @@ fn is_function_used_in_argument_or_expression_list(func: &AnyJsFunction) -> bool | |
) | ||
) | ||
} | ||
|
||
/// Checks whether the given function is a higher-order function, i.e., a function | ||
/// that returns another function either directly in its body or as an expression. | ||
/// | ||
/// A higher-order function is one that returns either a regular function or an arrow | ||
/// function from within its body. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `func` - A reference to an `AnyJsFunction` that represents the JavaScript function to inspect. | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `true` if the function returns another function (either a regular function or an arrow function). | ||
/// * `false` if it does not return a function or if the body is not a valid returnable function expression. | ||
/// | ||
/// # Note | ||
/// | ||
/// This function currently **does not support** detecting a return of a function | ||
/// inside other statements like `if` statements or `switch` statements. It only detects | ||
/// direct returns of functions or function returns in a straightforward function body. | ||
fn is_higher_order_function(func: &AnyJsFunction) -> bool { | ||
match func.body().ok() { | ||
Some(AnyJsFunctionBody::AnyJsExpression(expr)) => { | ||
matches!( | ||
expr, | ||
AnyJsExpression::JsArrowFunctionExpression(_) | ||
| AnyJsExpression::JsFunctionExpression(_) | ||
) | ||
} | ||
Some(AnyJsFunctionBody::JsFunctionBody(func_body)) => { | ||
check_statements_for_function_return(func_body.statements()) | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
||
/// Checks whether the given list of JavaScript statements contains a return statement | ||
/// that returns a function expression (either a regular function or an arrow function). | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `statements` - A list of JavaScript statements (`JsStatementList`) to inspect. | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `true` if the list contains a return statement with a function expression as its argument. | ||
/// * `false` if no such return statement is found or if the list is empty. | ||
fn check_statements_for_function_return(statements: JsStatementList) -> bool { | ||
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 commentThe 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. |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.