fix noImplicitReturns check when strictNullChecks is false (#20326)

This commit is contained in:
wenlu.wang 2017-11-30 07:27:32 +08:00 committed by Mohamed Hegazy
parent 93dca009f9
commit 78250ec58f
5 changed files with 70 additions and 9 deletions

View file

@ -22049,17 +22049,16 @@ namespace ts {
if (func) {
const signature = getSignatureFromDeclaration(func);
const returnType = getReturnTypeOfSignature(signature);
const functionFlags = getFunctionFlags(func);
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
// A generator does not need its return expressions checked against its return type.
// Instead, the yield expressions are checked against the element type.
// TODO: Check return expressions of generators when return type tracking is added
// for generators.
return;
}
if (strictNullChecks || node.expression || returnType.flags & TypeFlags.Never) {
const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
const functionFlags = getFunctionFlags(func);
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
// A generator does not need its return expressions checked against its return type.
// Instead, the yield expressions are checked against the element type.
// TODO: Check return expressions of generators when return type tracking is added
// for generators.
return;
}
if (func.kind === SyntaxKind.SetAccessor) {
if (node.expression) {
error(node, Diagnostics.Setters_cannot_return_a_value);

View file

@ -0,0 +1,17 @@
//// [generatorNoImplicitReturns.ts]
function* testGenerator () {
if (Math.random() > 0.5) {
return;
}
yield 'hello';
}
//// [generatorNoImplicitReturns.js]
function* testGenerator() {
if (Math.random() > 0.5) {
return;
}
yield 'hello';
}

View file

@ -0,0 +1,15 @@
=== tests/cases/conformance/es6/yieldExpressions/generatorNoImplicitReturns.ts ===
function* testGenerator () {
>testGenerator : Symbol(testGenerator, Decl(generatorNoImplicitReturns.ts, 0, 0))
if (Math.random() > 0.5) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
return;
}
yield 'hello';
}

View file

@ -0,0 +1,20 @@
=== tests/cases/conformance/es6/yieldExpressions/generatorNoImplicitReturns.ts ===
function* testGenerator () {
>testGenerator : () => IterableIterator<string>
if (Math.random() > 0.5) {
>Math.random() > 0.5 : boolean
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number
>0.5 : 0.5
return;
}
yield 'hello';
>yield 'hello' : any
>'hello' : "hello"
}

View file

@ -0,0 +1,10 @@
// @target: esnext
// @noImplicitReturns: true
// @strictNullChecks: false
function* testGenerator () {
if (Math.random() > 0.5) {
return;
}
yield 'hello';
}