fix noImplicitReturns check when strictNullChecks is false (#20326)
This commit is contained in:
parent
93dca009f9
commit
78250ec58f
|
@ -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);
|
||||
|
|
17
tests/baselines/reference/generatorNoImplicitReturns.js
Normal file
17
tests/baselines/reference/generatorNoImplicitReturns.js
Normal 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';
|
||||
}
|
15
tests/baselines/reference/generatorNoImplicitReturns.symbols
Normal file
15
tests/baselines/reference/generatorNoImplicitReturns.symbols
Normal 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';
|
||||
}
|
||||
|
20
tests/baselines/reference/generatorNoImplicitReturns.types
Normal file
20
tests/baselines/reference/generatorNoImplicitReturns.types
Normal 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"
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
// @target: esnext
|
||||
// @noImplicitReturns: true
|
||||
// @strictNullChecks: false
|
||||
|
||||
function* testGenerator () {
|
||||
if (Math.random() > 0.5) {
|
||||
return;
|
||||
}
|
||||
yield 'hello';
|
||||
}
|
Loading…
Reference in a new issue