Merge pull request #3352 from Microsoft/checkFunctionReturnExpression

Always check return expressions when type checking a file
This commit is contained in:
Jason Freeman
2015-06-03 10:54:22 -07:00
8 changed files with 41 additions and 1 deletions

View File

@@ -7657,6 +7657,15 @@ module ts {
}
if (node.body) {
if (!node.type) {
// There are some checks that are only performed in getReturnTypeFromBody, that may produce errors
// we need. An example is the noImplicitAny errors resulting from widening the return expression
// of a function. Because checking of function expression bodies is deferred, there was never an
// appropriate time to do this during the main walk of the file (see the comment at the top of
// checkFunctionExpressionBodies). So it must be done now.
getReturnTypeOfSignature(getSignatureFromDeclaration(node));
}
if (node.body.kind === SyntaxKind.Block) {
checkSourceElement(node.body);
}

View File

@@ -1,12 +1,15 @@
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(1,1): error TS1108: A 'return' statement can only be used within a function body.
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(1,18): error TS2304: Cannot find name 'role'.
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(2,18): error TS2304: Cannot find name 'Role'.
tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts(4,26): error TS2503: Cannot find namespace 'ng'.
==== tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts (3 errors) ====
==== tests/cases/compiler/multiLinePropertyAccessAndArrowFunctionIndent1.ts (4 errors) ====
return this.edit(role)
~~~~~~
!!! error TS1108: A 'return' statement can only be used within a function body.
~~~~
!!! error TS2304: Cannot find name 'role'.
.then((role: Role) =>
~~~~
!!! error TS2304: Cannot find name 'Role'.

View File

@@ -0,0 +1,7 @@
tests/cases/compiler/typeCheckObjectLiteralMethodBody.ts(1,13): error TS7010: 'bar', which lacks return-type annotation, implicitly has an 'any' return type.
==== tests/cases/compiler/typeCheckObjectLiteralMethodBody.ts (1 errors) ====
var foo = { bar() { return undefined } };
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS7010: 'bar', which lacks return-type annotation, implicitly has an 'any' return type.

View File

@@ -0,0 +1,5 @@
//// [typeCheckObjectLiteralMethodBody.ts]
var foo = { bar() { return undefined } };
//// [typeCheckObjectLiteralMethodBody.js]
var foo = { bar: function () { return undefined; } };

View File

@@ -0,0 +1,7 @@
tests/cases/compiler/typeCheckReturnExpression.ts(1,11): error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type.
==== tests/cases/compiler/typeCheckReturnExpression.ts (1 errors) ====
var foo = () => undefined;
~~~~~~~~~~~~~~~
!!! error TS7011: Function expression, which lacks return-type annotation, implicitly has an 'any' return type.

View File

@@ -0,0 +1,5 @@
//// [typeCheckReturnExpression.ts]
var foo = () => undefined;
//// [typeCheckReturnExpression.js]
var foo = function () { return undefined; };

View File

@@ -0,0 +1,2 @@
//@noImplicitAny: true
var foo = { bar() { return undefined } };

View File

@@ -0,0 +1,2 @@
//@noImplicitAny: true
var foo = () => undefined;