|
| 1 | +use oxc_allocator::Box as OBox; |
| 2 | +use oxc_ast::{ |
| 3 | + ast::{Expression, FunctionBody, Statement}, |
| 4 | + AstKind, |
| 5 | +}; |
| 6 | +use oxc_diagnostics::OxcDiagnostic; |
| 7 | +use oxc_macros::declare_oxc_lint; |
| 8 | +use oxc_span::Span; |
| 9 | + |
| 10 | +use crate::{context::LintContext, rule::Rule, utils::is_promise, AstNode}; |
| 11 | + |
| 12 | +fn no_return_in_finally_diagnostic(span0: Span) -> OxcDiagnostic { |
| 13 | + OxcDiagnostic::warn("Don't return in a finally callback") |
| 14 | + .with_help("Remove the return statement as nothing can consume the return value") |
| 15 | + .with_label(span0) |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Debug, Default, Clone)] |
| 19 | +pub struct NoReturnInFinally; |
| 20 | + |
| 21 | +declare_oxc_lint!( |
| 22 | + /// ### What it does |
| 23 | + /// |
| 24 | + /// Disallow return statements in a finally() callback of a promise. |
| 25 | + /// |
| 26 | + /// ### Why is this bad? |
| 27 | + /// |
| 28 | + /// Disallow return statements inside a callback passed to finally(), since nothing would |
| 29 | + /// consume what's returned. |
| 30 | + /// |
| 31 | + /// ### Example |
| 32 | + /// ```javascript |
| 33 | + /// myPromise.finally(function (val) { |
| 34 | + /// return val |
| 35 | + /// }) |
| 36 | + /// ``` |
| 37 | + NoReturnInFinally, |
| 38 | + nursery, |
| 39 | +); |
| 40 | + |
| 41 | +impl Rule for NoReturnInFinally { |
| 42 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 43 | + let AstKind::CallExpression(call_expr) = node.kind() else { |
| 44 | + return; |
| 45 | + }; |
| 46 | + |
| 47 | + let Some(prop_name) = is_promise(call_expr) else { |
| 48 | + return; |
| 49 | + }; |
| 50 | + |
| 51 | + if prop_name != "finally" { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + for argument in &call_expr.arguments { |
| 56 | + let Some(arg_expr) = argument.as_expression() else { |
| 57 | + continue; |
| 58 | + }; |
| 59 | + match arg_expr { |
| 60 | + Expression::ArrowFunctionExpression(arrow_expr) => { |
| 61 | + find_return_statement(&arrow_expr.body, ctx); |
| 62 | + } |
| 63 | + Expression::FunctionExpression(func_expr) => { |
| 64 | + let Some(func_body) = &func_expr.body else { |
| 65 | + continue; |
| 66 | + }; |
| 67 | + find_return_statement(func_body, ctx); |
| 68 | + } |
| 69 | + _ => continue, |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fn find_return_statement<'a>(func_body: &OBox<'_, FunctionBody<'a>>, ctx: &LintContext<'a>) { |
| 76 | + let Some(return_stmt) = |
| 77 | + func_body.statements.iter().find(|stmt| matches!(stmt, Statement::ReturnStatement(_))) |
| 78 | + else { |
| 79 | + return; |
| 80 | + }; |
| 81 | + |
| 82 | + let Statement::ReturnStatement(stmt) = return_stmt else { |
| 83 | + return; |
| 84 | + }; |
| 85 | + |
| 86 | + ctx.diagnostic(no_return_in_finally_diagnostic(stmt.span)); |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +fn test() { |
| 91 | + use crate::tester::Tester; |
| 92 | + |
| 93 | + let pass = vec![ |
| 94 | + "Promise.resolve(1).finally(() => { console.log(2) })", |
| 95 | + "Promise.reject(4).finally(() => { console.log(2) })", |
| 96 | + "Promise.reject(4).finally(() => {})", |
| 97 | + "myPromise.finally(() => {});", |
| 98 | + "Promise.resolve(1).finally(function () { })", |
| 99 | + ]; |
| 100 | + |
| 101 | + let fail = vec![ |
| 102 | + "Promise.resolve(1).finally(() => { return 2 })", |
| 103 | + "Promise.reject(0).finally(() => { return 2 })", |
| 104 | + "myPromise.finally(() => { return 2 });", |
| 105 | + "Promise.resolve(1).finally(function () { return 2 })", |
| 106 | + ]; |
| 107 | + |
| 108 | + Tester::new(NoReturnInFinally::NAME, pass, fail).test_and_snapshot(); |
| 109 | +} |
0 commit comments