Skip to content

Commit

Permalink
fix(isolated-declarations): inferring an incorrect return type when t…
Browse files Browse the repository at this point in the history
…here is an arrow function inside a function
  • Loading branch information
Dunqing committed Jun 19, 2024
1 parent 4c43113 commit 0f46dee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
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 @@ -7,14 +7,19 @@ function bar() {
if (true) {
return;
}
return 1;
}
// inferred type is number | undefined

function baz() {
if (true) {
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 @@ -5,17 +5,18 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/infer_return_type.ts
==================== .D.TS ====================

declare function foo(): number;
declare function bar(): ((number) | (undefined));
declare function baz();
declare function bar();
declare function baz(): null;
declare function qux(): string;


==================== Errors ====================

x TS9007: Function must have an explicit return type annotation with
| --isolatedDeclarations.
,-[14:10]
13 |
14 | function baz() {
: ^^^
15 | if (true) {
`----
,-[6:10]
5 |
6 | function bar() {
: ^^^
7 | if (true) {
`----

0 comments on commit 0f46dee

Please sign in to comment.