Skip to content

Commit

Permalink
fix some docs and enable the rule for JavaScript files
Browse files Browse the repository at this point in the history
  • Loading branch information
kaykdm committed Jan 20, 2025
1 parent 1d2ef70 commit 93549da
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
19 changes: 5 additions & 14 deletions crates/biome_js_analyze/src/lint/nursery/no_floating_promises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use biome_js_factory::make;
use biome_js_semantic::SemanticModel;
use biome_js_syntax::{
binding_ext::AnyJsBindingDeclaration, AnyJsExpression, AnyJsName, AnyTsReturnType, AnyTsType,
JsCallExpression, JsExpressionStatement, JsFileSource, JsFunctionDeclaration,
JsStaticMemberExpression, JsSyntaxKind, TsReturnTypeAnnotation,
JsCallExpression, JsExpressionStatement, JsFunctionDeclaration, JsStaticMemberExpression,
JsSyntaxKind, TsReturnTypeAnnotation,
};
use biome_rowan::{AstNode, AstSeparatedList, BatchMutationExt, TriviaPieceKind};

Expand All @@ -16,12 +16,12 @@ use crate::{services::semantic::Semantic, JsRuleAction};
declare_lint_rule! {
/// Require Promise-like statements to be handled appropriately.
///
/// "floating" Promise is one that is created without any code set up to handle any errors it might throw.
/// A "floating" `Promise` is one that is created without any code set up to handle any errors it might throw.
/// Floating Promises can lead to several issues, including improperly sequenced operations, unhandled Promise rejections, and other unintended consequences.
///
/// This rule will report Promise-valued statements that are not treated in one of the following ways:
/// - Calling its `.then()` with two arguments
/// - Calling its `.catch()` with one argument
/// - Calling its `.then()` method with two arguments
/// - Calling its `.catch()` method with one argument
/// - `await`ing it
/// - `return`ing it
/// - `void`ing it
Expand Down Expand Up @@ -82,11 +82,6 @@ impl Rule for NoFloatingPromises {
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let source_type = ctx.source_type::<JsFileSource>().language();
if !source_type.is_typescript() || source_type.is_definition_file() {
return None;
}

let node = ctx.query();
let model = ctx.model();
let expression = node.expression().ok()?;
Expand All @@ -109,10 +104,6 @@ impl Rule for NoFloatingPromises {
}

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
//
// Read our guidelines to write great diagnostics:
// https://docs.rs/biome_analyze/latest/biome_analyze/#what-a-rule-should-say-to-the-user
//
let node = ctx.query();
Some(
RuleDiagnostic::new(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
async function returnsPromise() {
return 'value';
}
returnsPromise();
returnsPromise().then(() => { }).finally(() => { });
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.js
---
# Input
```jsx
async function returnsPromise() {
return 'value';
}
returnsPromise();
returnsPromise().then(() => { }).finally(() => { });

```

# Diagnostics
```
invalid.js:4:1 lint/nursery/noFloatingPromises FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! A "floating" Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.
2 │ return 'value';
3 │ }
> 4 │ returnsPromise();
│ ^^^^^^^^^^^^^^^^^
5 │ returnsPromise().then(() => { }).finally(() => { });
6 │
i This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.
i Safe fix: Add await operator.
4 │ await·returnsPromise();
│ ++++++
```

```
invalid.js:5:1 lint/nursery/noFloatingPromises FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! A "floating" Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.
3 │ }
4 │ returnsPromise();
> 5 │ returnsPromise().then(() => { }).finally(() => { });
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │
i This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.
i Safe fix: Add await operator.
5 │ await·returnsPromise().then(()·=>·{·}).finally(()·=>·{·});
│ ++++++
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
async function returnsPromise() {
return 'value';
}

await returnsPromise();
void returnsPromise();
return returnsPromise();

returnsPromise().then(
() => { },
() => { },
);

returnsPromise().catch(() => { });
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.js
---
# Input
```jsx
async function returnsPromise() {
return 'value';
}

await returnsPromise();
void returnsPromise();
return returnsPromise();

returnsPromise().then(
() => { },
() => { },
);

returnsPromise().catch(() => { });
```

0 comments on commit 93549da

Please sign in to comment.