Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/oxc_isolated_declarations/src/return_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::{
ast::{
BindingIdentifier, Expression, Function, FunctionBody, ReturnStatement, TSType,
TSTypeAliasDeclaration, TSTypeName, TSTypeQueryExprName,
ArrowFunctionExpression, BindingIdentifier, Expression, Function, FunctionBody,
ReturnStatement, TSType, TSTypeAliasDeclaration, TSTypeName, TSTypeQueryExprName,
},
AstBuilder, Visit,
};
Expand Down Expand Up @@ -138,6 +138,10 @@ impl<'a> Visit<'a> for FunctionReturnType<'a> {
// We don't care about nested functions
}

fn visit_arrow_expression(&mut self, _expr: &ArrowFunctionExpression<'a>) {
// We don't care about nested functions
}

fn visit_return_statement(&mut self, stmt: &ReturnStatement<'a>) {
self.return_statement_count += 1;
if self.return_statement_count > 1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ function foo() {
// inferred type is number

function bar() {
if (true) {
if (a) {
return;
}
return 1;
}
// inferred type is number | undefined

function baz() {
if (true) {
if (a) {
return null;
}
return 1;
}
// We can't infer return type if there are multiple return statements with different types
// We can't infer return type if there are multiple return statements with different types

function qux() {
const a = (() => {
return 1;
})();
return `Hello, world!`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/infer-return-type.ts
declare function foo(): number;
declare function bar(): ((number) | (undefined));
declare function baz();
declare function qux(): string;


==================== Errors ====================
Expand All @@ -17,5 +18,5 @@ declare function baz();
13 |
14 | function baz() {
: ^^^
15 | if (true) {
15 | if (a) {
`----