From 746b3188b67eb9836982d94e38678b231c223eb0 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Mon, 28 Apr 2025 15:23:21 +0000 Subject: [PATCH] fix(linter): false positive in typescript/explicit-function-return-type with `satisfies` (#10668) when working on #10667, i noticed these tests were missing from our suite. this PR ports over the tests and fixes them typescript/eslint PR https://github.com/typescript-eslint/typescript-eslint/pull/10315 --- .../explicit_function_return_type.rs | 66 +++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs b/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs index 021e157787bc7..fcd53b3e72f97 100644 --- a/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs +++ b/crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs @@ -420,6 +420,14 @@ impl ExplicitFunctionReturnType { let AstKind::ArrowFunctionExpression(func) = node.kind() else { return false }; let Some(expr) = func.get_expression() else { return false }; + let mut expr = expr; + loop { + expr = match expr { + Expression::TSSatisfiesExpression(e) => &e.expression, + _ => break, + }; + } + match expr { Expression::TSAsExpression(ts_expr) => { let TSType::TSTypeReference(ts_type) = &ts_expr.type_annotation else { @@ -606,7 +614,10 @@ fn is_typed_jsx(node: &AstNode) -> bool { } fn is_function(expr: &Expression) -> bool { - matches!(expr, Expression::ArrowFunctionExpression(_) | Expression::FunctionExpression(_)) + matches!( + expr.get_inner_expression(), + Expression::ArrowFunctionExpression(_) | Expression::FunctionExpression(_) + ) } fn ancestor_has_return_type<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) -> bool { @@ -1145,9 +1156,56 @@ fn test() { ), ( " - new Promise(resolve => {}); - new Foo(1, () => {}); - ", + interface R { + type: string; + value: number; + } + + const func = (value: number) => ({ type: 'X', value }) as const satisfies R; + ", + Some( + serde_json::json!([ { "allowDirectConstAssertionInArrowFunctions": true, }, ]), + ), + None, + None, + ), + ( + " + interface R { + type: string; + value: number; + } + + const func = (value: number) => + ({ type: 'X', value }) as const satisfies R satisfies R; + ", + Some( + serde_json::json!([ { "allowDirectConstAssertionInArrowFunctions": true, }, ]), + ), + None, + None, + ), + ( + " + interface R { + type: string; + value: number; + } + + const func = (value: number) => + ({ type: 'X', value }) as const satisfies R satisfies R satisfies R; + ", + Some( + serde_json::json!([ { "allowDirectConstAssertionInArrowFunctions": true, }, ]), + ), + None, + None, + ), + ( + " + new Promise(resolve => {}); + new Foo(1, () => {}); + ", Some(serde_json::json!([ { "allowTypedFunctionExpressions": true, }, ])), None, None,