Skip to content
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 promise expression in noFloatingPromises #4977

Open
wants to merge 6 commits into
base: next
Choose a base branch
from

Conversation

kaykdm
Copy link
Contributor

@kaykdm kaykdm commented Jan 27, 2025

Summary

related: #3187 and #4956
This pull request introduces the support for promise expressions in noFloatingPromises

Example of invalid code:

const promise = new Promise((resolve) => resolve('value'));
promise.then(() => { }).finally(() => { });

Promise.resolve('value').then(() => { })
Promise.all([p1, p2, p3])

Example of valid code:

const promise = new Promise((resolve) => resolve('value'));
promise.then(() => { }, () => { })

Promise.resolve('value').catch(() => {})
await Promise.all([p1, p2, p3])

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Jan 27, 2025
@kaykdm kaykdm marked this pull request as ready for review January 27, 2025 01:26
Copy link

codspeed-hq bot commented Jan 27, 2025

CodSpeed Performance Report

Merging #4977 will not alter performance

Comparing kaykdm:support-promise-expression (71696d7) with next (d76e8d3)

Summary

✅ 95 untouched benchmarks

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! There are other cases we need to cover regarding the Promise global

Comment on lines 253 to 254
is_variable_initializer_a_promise(&js_var_decl).unwrap_or(false)
|| is_variable_annotation_a_promise(&js_var_decl).unwrap_or(false),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
is_variable_initializer_a_promise(&js_var_decl).unwrap_or(false)
|| is_variable_annotation_a_promise(&js_var_decl).unwrap_or(false),
is_variable_initializer_a_promise(&js_var_decl).unwrap_or_default()
|| is_variable_annotation_a_promise(&js_var_decl).unwrap_or_default(),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

return None;
}

if is_handled_promise(&js_call_expression).unwrap_or(false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if is_handled_promise(&js_call_expression).unwrap_or(false) {
if is_handled_promise(&js_call_expression)? {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually want to return only when the value is true and continue when the value is false or None so using ? does not work here

Comment on lines 398 to 399
is_expression_an_promise(&js_ident_expr).unwrap_or(false)
|| is_binding_a_promise(&js_ident_expr, model).unwrap_or(false),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
is_expression_an_promise(&js_ident_expr).unwrap_or(false)
|| is_binding_a_promise(&js_ident_expr, model).unwrap_or(false),
is_expression_an_promise(&js_ident_expr).unwrap_or_default()
|| is_binding_a_promise(&js_ident_expr, model).unwrap_or_default(),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated!

Comment on lines 515 to 517
let js_ident_expr = any_js_expr.as_js_identifier_expression()?;
let reference = js_ident_expr.name().ok()?;
Some(reference.has_name("Promise"))
Copy link
Member

@ematipico ematipico Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, there are few things that we need to handle here (not just here, but in all cases where we check "Promise"):

  1. The parenthesis. Unfortunately these are valid cases e.g. return (new Pormise.resolve()). You can use the method .omit_parentheses that can be used from AnyJsExpression
  2. Global identifiers. A promise can be used from window.Promise or globalThis.Promise. You can use the utility global_identifier
  3. Making sure that Promise isn't a binding. You need to use the semantic model to check that.
var Promise = { resolve(): {} };
return Promise.resolve()

This should NOT trigger the rule.
Here's an example of rust code to check:

fn is_global_object(semantic: &SemanticModel) -> bool {
semantic
.scopes()
.find(|s| s.get_binding("Object").is_some())
.map_or(true, |s| s.is_global_scope())
}

Copy link
Contributor Author

@kaykdm kaykdm Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback!
I have updated the code to handle 1 and 2. I will probably work on 3 tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The parenthesis. Unfortunately these are valid cases e.g. return (new Pormise.resolve()). You can use the method .omit_parentheses that can be used from AnyJsExpression
  2. Global identifiers. A promise can be used from window.Promise or globalThis.Promise. You can use the utility global_identifier
  3. Making sure that Promise isn't a binding. You need to use the semantic model to check that.

I have updated the code to cover all the cases.
For 3 (making sure that Promise isn't a binding), creating a function like is_global_object for Promise does not work for globally defined variable. Therefore, I decided to use model.binding instead.

@kaykdm kaykdm force-pushed the support-promise-expression branch from 85ab50c to 20720d7 Compare January 28, 2025 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants