Skip to content

Commit 0d49e24

Browse files
feat(lint/js): add noReturnInFinally (#9022)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 810ea56 commit 0d49e24

18 files changed

Lines changed: 900 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Added the nursery rule [`noReturnInFinally`](https://biomejs.dev/linter/rules/no-return-in-finally/). This rule disallows return statements in `Promise.prototype.finally()` callbacks, including inside nested blocks and conditional branches. Returns in nested functions are ignored by the rule.
6+
7+
```js
8+
// Invalid: return in finally callback
9+
Promise.resolve(1).finally(() => { return 2 })
10+
11+
// Valid: no return in finally callback
12+
Promise.resolve(1).finally(() => { console.log(2) })
13+
```
14+
15+
Returning a value from a `Promise.prototype.finally()` callback does not replace the original promise's fulfillment value, which can be confusing. Returned promises and thenables are awaited, and their rejection rejects the resulting promise.

‎crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/biome_configuration/src/analyzer/linter/rules.rs‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/biome_configuration/src/generated/linter_options_check.rs‎

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/biome_diagnostics_categories/src/categories.rs‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
use biome_analyze::{
2+
Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule,
3+
};
4+
use biome_console::markup;
5+
use biome_js_syntax::{AnyJsExpression, AnyJsFunctionBody, JsCallExpression, JsReturnStatement};
6+
use biome_rowan::{AstNode, WalkEvent};
7+
use biome_rule_options::no_return_in_finally::NoReturnInFinallyOptions;
8+
9+
use crate::services::control_flow::AnyJsControlFlowRoot;
10+
11+
declare_lint_rule! {
12+
/// Disallow return statements in `Promise.prototype.finally()` callbacks.
13+
///
14+
/// Returning a value from a `Promise.prototype.finally()` callback is ignored, which can be confusing.
15+
/// Returned promises and thenables are awaited, and their rejection rejects
16+
/// the resulting promise.
17+
///
18+
/// Returns inside nested blocks, including conditional branches and loops,
19+
/// are also disallowed. Returns inside nested functions are ignored by the rule.
20+
///
21+
/// ## Examples
22+
///
23+
/// ### Invalid
24+
///
25+
/// ```js,expect_diagnostic
26+
/// Promise.resolve(1).finally(() => { return 2 });
27+
/// ```
28+
///
29+
/// ```js,expect_diagnostic
30+
/// myPromise.finally(() => { return 2 });
31+
/// ```
32+
///
33+
/// ```js,expect_diagnostic
34+
/// myPromise.finally(() => {
35+
/// if (condition) {
36+
/// return 2;
37+
/// }
38+
/// });
39+
/// ```
40+
///
41+
/// ### Valid
42+
///
43+
/// ```js
44+
/// Promise.resolve(1).finally(() => { console.log(2) });
45+
/// myPromise.finally(() => {});
46+
///
47+
/// myPromise.finally(function () {
48+
/// function nested() {
49+
/// return 2;
50+
/// }
51+
/// console.log(nested());
52+
/// });
53+
/// ```
54+
///
55+
/// ## References
56+
///
57+
/// - MDN: [`Promise.prototype.finally` documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally)
58+
///
59+
pub NoReturnInFinally {
60+
version: "next",
61+
name: "noReturnInFinally",
62+
language: "js",
63+
recommended: true,
64+
sources: &[RuleSource::EslintPromise("no-return-in-finally").inspired()],
65+
}
66+
}
67+
68+
impl Rule for NoReturnInFinally {
69+
type Query = Ast<JsCallExpression>;
70+
type State = JsReturnStatement;
71+
type Signals = Option<Self::State>;
72+
type Options = NoReturnInFinallyOptions;
73+
74+
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
75+
let call_expr = ctx.query();
76+
77+
// Check if this is a .finally() call
78+
let member_name = call_expr
79+
.callee()
80+
.ok()?
81+
.as_js_static_member_expression()?
82+
.member()
83+
.ok()?
84+
.as_js_name()?
85+
.value_token()
86+
.ok()?;
87+
88+
if member_name.text_trimmed() != "finally" {
89+
return None;
90+
}
91+
92+
// Get the first argument (callback function)
93+
let args = call_expr.arguments().ok()?.args();
94+
let first_arg = args.into_iter().next()?.ok()?;
95+
96+
let body = match first_arg.as_any_js_expression()? {
97+
AnyJsExpression::JsArrowFunctionExpression(arrow) => match arrow.body().ok()? {
98+
AnyJsFunctionBody::JsFunctionBody(body) => body,
99+
AnyJsFunctionBody::AnyJsExpression(_) => return None,
100+
},
101+
AnyJsExpression::JsFunctionExpression(func) => func.body().ok()?,
102+
_ => return None,
103+
};
104+
105+
let mut preorder = body.syntax().preorder();
106+
while let Some(event) = preorder.next() {
107+
let WalkEvent::Enter(node) = event else {
108+
continue;
109+
};
110+
if AnyJsControlFlowRoot::can_cast(node.kind()) {
111+
preorder.skip_subtree();
112+
} else if let Some(ret) = JsReturnStatement::cast(node) {
113+
return Some(ret);
114+
}
115+
}
116+
None
117+
}
118+
119+
fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
120+
Some(
121+
RuleDiagnostic::new(
122+
rule_category!(),
123+
state.range(),
124+
markup! {
125+
"Returning a value from a "<Emphasis>"finally"</Emphasis>" callback is not allowed."
126+
},
127+
)
128+
.note(markup! {
129+
"The return value in a "<Emphasis>"finally"</Emphasis>" callback is ignored, making any return statement potentially confusing."
130+
}).note(markup! {
131+
"Remove the return statement from the "<Emphasis>"finally"</Emphasis>" callback to resolve this issue."
132+
}),
133+
)
134+
}
135+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* should generate diagnostics */
2+
3+
Promise.resolve(1).finally(() => { return 2 })
4+
5+
Promise.reject(0).finally(() => { return 2 })
6+
7+
myPromise.finally(() => { return 2 });
8+
9+
Promise.resolve(1).finally(function () { return 2 })
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: invalid.js
4+
---
5+
# Input
6+
```js
7+
/* should generate diagnostics */
8+
9+
Promise.resolve(1).finally(() => { return 2 })
10+
11+
Promise.reject(0).finally(() => { return 2 })
12+
13+
myPromise.finally(() => { return 2 });
14+
15+
Promise.resolve(1).finally(function () { return 2 })
16+
```
17+
18+
# Diagnostics
19+
```
20+
invalid.js:3:36 lint/nursery/noReturnInFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
21+
22+
i Returning a value from a finally callback is not allowed.
23+
24+
1 │ /* should generate diagnostics */
25+
2 │
26+
> 3 │ Promise.resolve(1).finally(() => { return 2 })
27+
│ ^^^^^^^^
28+
4 │
29+
5 │ Promise.reject(0).finally(() => { return 2 })
30+
31+
i The return value in a finally callback is ignored, making any return statement potentially confusing.
32+
33+
i Remove the return statement from the finally callback to resolve this issue.
34+
35+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
36+
37+
38+
```
39+
40+
```
41+
invalid.js:5:35 lint/nursery/noReturnInFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
42+
43+
i Returning a value from a finally callback is not allowed.
44+
45+
3 │ Promise.resolve(1).finally(() => { return 2 })
46+
4 │
47+
> 5 │ Promise.reject(0).finally(() => { return 2 })
48+
│ ^^^^^^^^
49+
6 │
50+
7 │ myPromise.finally(() => { return 2 });
51+
52+
i The return value in a finally callback is ignored, making any return statement potentially confusing.
53+
54+
i Remove the return statement from the finally callback to resolve this issue.
55+
56+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
57+
58+
59+
```
60+
61+
```
62+
invalid.js:7:27 lint/nursery/noReturnInFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
63+
64+
i Returning a value from a finally callback is not allowed.
65+
66+
5 │ Promise.reject(0).finally(() => { return 2 })
67+
6 │
68+
> 7 │ myPromise.finally(() => { return 2 });
69+
│ ^^^^^^^^
70+
8 │
71+
9 │ Promise.resolve(1).finally(function () { return 2 })
72+
73+
i The return value in a finally callback is ignored, making any return statement potentially confusing.
74+
75+
i Remove the return statement from the finally callback to resolve this issue.
76+
77+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
78+
79+
80+
```
81+
82+
```
83+
invalid.js:9:42 lint/nursery/noReturnInFinally ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
84+
85+
i Returning a value from a finally callback is not allowed.
86+
87+
7 │ myPromise.finally(() => { return 2 });
88+
8 │
89+
> 9 │ Promise.resolve(1).finally(function () { return 2 })
90+
│ ^^^^^^^^
91+
92+
i The return value in a finally callback is ignored, making any return statement potentially confusing.
93+
94+
i Remove the return statement from the finally callback to resolve this issue.
95+
96+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
97+
98+
99+
```

0 commit comments

Comments
 (0)
⌂ 换网址