-
-
Notifications
You must be signed in to change notification settings - Fork 519
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(lint): support assigned functions in noFloatingPromises
#4958
feat(lint): support assigned functions in noFloatingPromises
#4958
Conversation
CodSpeed Performance ReportMerging #4958 will improve performances by 6.88%Comparing Summary
Benchmarks breakdown
|
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.
Love it!
match any_js_binding_decl { | ||
AnyJsBindingDeclaration::JsFunctionDeclaration(func_decl) => { | ||
return is_function_a_promise(&func_decl); | ||
} | ||
AnyJsBindingDeclaration::JsVariableDeclarator(js_var_decl) => { | ||
return is_variable_initializer_a_promise(&js_var_decl) | ||
|| is_variable_annotation_a_promise(&js_var_decl); | ||
} | ||
_ => {} | ||
} |
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.
Nit: if you remove the false
at the end, you can just use this entire match
as an expression that defines the return value, so you don't need the repeated return
statements. This is generally considered a bit more idiomatic in Rust:
match any_js_binding_decl { | |
AnyJsBindingDeclaration::JsFunctionDeclaration(func_decl) => { | |
return is_function_a_promise(&func_decl); | |
} | |
AnyJsBindingDeclaration::JsVariableDeclarator(js_var_decl) => { | |
return is_variable_initializer_a_promise(&js_var_decl) | |
|| is_variable_annotation_a_promise(&js_var_decl); | |
} | |
_ => {} | |
} | |
match any_js_binding_decl { | |
AnyJsBindingDeclaration::JsFunctionDeclaration(func_decl) => { | |
is_function_a_promise(&func_decl) | |
} | |
AnyJsBindingDeclaration::JsVariableDeclarator(js_var_decl) => { | |
is_variable_initializer_a_promise(&js_var_decl) | |
|| is_variable_annotation_a_promise(&js_var_decl) | |
} | |
_ => 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.
Thanks for pointing that out!
I've updated the code to remove the repeated return statements. It looks cleaner and more idiomatic now.
match expr { | ||
AnyJsExpression::JsArrowFunctionExpression(arrow_func) => { | ||
if arrow_func.async_token().is_some() { | ||
return true; | ||
} | ||
|
||
return is_return_type_a_promise(arrow_func.return_type_annotation()); | ||
} | ||
AnyJsExpression::JsFunctionExpression(func_expr) => { | ||
if func_expr.async_token().is_some() { | ||
return true; | ||
} | ||
return is_return_type_a_promise(func_expr.return_type_annotation()); | ||
} | ||
_ => {} | ||
} | ||
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.
Similar to feedback above:
match expr { | |
AnyJsExpression::JsArrowFunctionExpression(arrow_func) => { | |
if arrow_func.async_token().is_some() { | |
return true; | |
} | |
return is_return_type_a_promise(arrow_func.return_type_annotation()); | |
} | |
AnyJsExpression::JsFunctionExpression(func_expr) => { | |
if func_expr.async_token().is_some() { | |
return true; | |
} | |
return is_return_type_a_promise(func_expr.return_type_annotation()); | |
} | |
_ => {} | |
} | |
false | |
match expr { | |
AnyJsExpression::JsArrowFunctionExpression(arrow_func) => { | |
arrow_func.async_token().is_some() | |
|| is_return_type_a_promise(arrow_func.return_type_annotation()) | |
} | |
AnyJsExpression::JsFunctionExpression(func_expr) => { | |
func_expr.async_token().is_some() | |
|| is_return_type_a_promise(func_expr.return_type_annotation()) | |
} | |
_ => 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.
Thanks! updated!
a4ee1e3
to
70e298c
Compare
/// return 'value' | ||
/// } | ||
/// ``` | ||
fn is_variable_initializer_a_promise(js_variable_declarator: &JsVariableDeclarator) -> bool { |
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's been a while that you're contributing to the code base, so you might have noticed that you have to deal with many Result
coming from the syntax.
I strongly suggest changing all these functions to return a Option
instead, and start doing .ok()?
for all those syntax APIs that return SyntaxResult
.
This is an accepted approach inside Biome, especially inside the lint rules. Doing so, you low down the cognitive complexity of the code, and the code style it's in line with the existing rules.
In this case:
fn is_variable_initializer_a_promise(js_variable_declarator: &JsVariableDeclarator) -> Option<bool> {}
is_variable_initializer_a_promise(node)?
You're free to apply this suggestion to the whole file in the next PR, or in this PR, but we should do it eventually.
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.
Thanks for the review!
That makes sense. I'll apply that suggestion to the whole file in the next PR.
/// return Promise.resolve("value") | ||
/// } | ||
/// ``` | ||
fn is_variable_annotation_a_promise(js_variable_declarator: &JsVariableDeclarator) -> bool { |
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.
Same here. Change it to Option<bool>
, use .ok()?
every time you extract a type from a node, and the caller will use ?
Summary
related: #3187 and #4956
This pull request introduces the support for assigned functions in
noFloatingPromises
Example of invalid code:
Example of valid code: