Skip to content

Commit 78250ec

Browse files
Kingwlmhegazy
authored andcommitted
fix noImplicitReturns check when strictNullChecks is false (#20326)
1 parent 93dca00 commit 78250ec

File tree

5 files changed

+70
-9
lines changed

5 files changed

+70
-9
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22049,17 +22049,16 @@ namespace ts {
2204922049
if (func) {
2205022050
const signature = getSignatureFromDeclaration(func);
2205122051
const returnType = getReturnTypeOfSignature(signature);
22052+
const functionFlags = getFunctionFlags(func);
22053+
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
22054+
// A generator does not need its return expressions checked against its return type.
22055+
// Instead, the yield expressions are checked against the element type.
22056+
// TODO: Check return expressions of generators when return type tracking is added
22057+
// for generators.
22058+
return;
22059+
}
2205222060
if (strictNullChecks || node.expression || returnType.flags & TypeFlags.Never) {
2205322061
const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
22054-
const functionFlags = getFunctionFlags(func);
22055-
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
22056-
// A generator does not need its return expressions checked against its return type.
22057-
// Instead, the yield expressions are checked against the element type.
22058-
// TODO: Check return expressions of generators when return type tracking is added
22059-
// for generators.
22060-
return;
22061-
}
22062-
2206322062
if (func.kind === SyntaxKind.SetAccessor) {
2206422063
if (node.expression) {
2206522064
error(node, Diagnostics.Setters_cannot_return_a_value);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [generatorNoImplicitReturns.ts]
2+
3+
function* testGenerator () {
4+
if (Math.random() > 0.5) {
5+
return;
6+
}
7+
yield 'hello';
8+
}
9+
10+
11+
//// [generatorNoImplicitReturns.js]
12+
function* testGenerator() {
13+
if (Math.random() > 0.5) {
14+
return;
15+
}
16+
yield 'hello';
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorNoImplicitReturns.ts ===
2+
3+
function* testGenerator () {
4+
>testGenerator : Symbol(testGenerator, Decl(generatorNoImplicitReturns.ts, 0, 0))
5+
6+
if (Math.random() > 0.5) {
7+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
8+
>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, --, --))
9+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
10+
11+
return;
12+
}
13+
yield 'hello';
14+
}
15+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorNoImplicitReturns.ts ===
2+
3+
function* testGenerator () {
4+
>testGenerator : () => IterableIterator<string>
5+
6+
if (Math.random() > 0.5) {
7+
>Math.random() > 0.5 : boolean
8+
>Math.random() : number
9+
>Math.random : () => number
10+
>Math : Math
11+
>random : () => number
12+
>0.5 : 0.5
13+
14+
return;
15+
}
16+
yield 'hello';
17+
>yield 'hello' : any
18+
>'hello' : "hello"
19+
}
20+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: esnext
2+
// @noImplicitReturns: true
3+
// @strictNullChecks: false
4+
5+
function* testGenerator () {
6+
if (Math.random() > 0.5) {
7+
return;
8+
}
9+
yield 'hello';
10+
}

0 commit comments

Comments
 (0)