Skip to content

Commit a1703d7

Browse files
committed
Merge pull request microsoft#3352 from Microsoft/checkFunctionReturnExpression
Always check return expressions when type checking a file
2 parents e39a2e7 + f390133 commit a1703d7

8 files changed

+41
-1
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7657,6 +7657,15 @@ module ts {
76577657
}
76587658

76597659
if (node.body) {
7660+
if (!node.type) {
7661+
// There are some checks that are only performed in getReturnTypeFromBody, that may produce errors
7662+
// we need. An example is the noImplicitAny errors resulting from widening the return expression
7663+
// of a function. Because checking of function expression bodies is deferred, there was never an
7664+
// appropriate time to do this during the main walk of the file (see the comment at the top of
7665+
// checkFunctionExpressionBodies). So it must be done now.
7666+
getReturnTypeOfSignature(getSignatureFromDeclaration(node));
7667+
}
7668+
76607669
if (node.body.kind === SyntaxKind.Block) {
76617670
checkSourceElement(node.body);
76627671
}

tests/baselines/reference/multiLinePropertyAccessAndArrowFunctionIndent1.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
2+
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(1,18): error TS2304: Cannot find name 'role'.
23
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(2,18): error TS2304: Cannot find name 'Role'.
34
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(4,26): error TS2503: Cannot find namespace 'ng'.
45

56

6-
==== tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts (3 errors) ====
7+
==== tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts (4 errors) ====
78
return this.edit(role)
89
~~~~~~
910
!!! error TS1108: A 'return' statement can only be used within a function body.
11+
~~~~
12+
!!! error TS2304: Cannot find name 'role'.
1013
.then((role: Role) =>
1114
~~~~
1215
!!! error TS2304: Cannot find name 'Role'.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/compiler/typeCheckObjectLiteralMethodBody.ts(1,13): error TS7010: 'bar', which lacks return-type annotation, implicitly has an 'any' return type.
2+
3+
4+
==== tests/cases/compiler/typeCheckObjectLiteralMethodBody.ts (1 errors) ====
5+
var foo = { bar() { return undefined } };
6+
~~~~~~~~~~~~~~~~~~~~~~~~~~
7+
!!! error TS7010: 'bar', which lacks return-type annotation, implicitly has an 'any' return type.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [typeCheckObjectLiteralMethodBody.ts]
2+
var foo = { bar() { return undefined } };
3+
4+
//// [typeCheckObjectLiteralMethodBody.js]
5+
var foo = { bar: function () { return undefined; } };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests/cases/compiler/typeCheckReturnExpression.ts(1,11): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type.
2+
3+
4+
==== tests/cases/compiler/typeCheckReturnExpression.ts (1 errors) ====
5+
var foo = () => undefined;
6+
~~~~~~~~~~~~~~~
7+
!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//// [typeCheckReturnExpression.ts]
2+
var foo = () => undefined;
3+
4+
//// [typeCheckReturnExpression.js]
5+
var foo = function () { return undefined; };
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@noImplicitAny: true
2+
var foo = { bar() { return undefined } };
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@noImplicitAny: true
2+
var foo = () => undefined;

0 commit comments

Comments
 (0)