Skip to content

Commit 7fd0915

Browse files
committed
fix check order of strictNullChecks and noImplicitReturns
1 parent d79a474 commit 7fd0915

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22054,6 +22054,10 @@ namespace ts {
2205422054
if (func) {
2205522055
const signature = getSignatureFromDeclaration(func);
2205622056
const returnType = getReturnTypeOfSignature(signature);
22057+
if (func.kind !== SyntaxKind.Constructor && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func, returnType)) {
22058+
// The function has a return type, but the return statement doesn't have an expression.
22059+
error(node, Diagnostics.Not_all_code_paths_return_a_value);
22060+
}
2205722061
if (strictNullChecks || node.expression || returnType.flags & TypeFlags.Never) {
2205822062
const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
2205922063
const functionFlags = getFunctionFlags(func);
@@ -22091,10 +22095,6 @@ namespace ts {
2209122095
}
2209222096
}
2209322097
}
22094-
else if (func.kind !== SyntaxKind.Constructor && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func, returnType)) {
22095-
// The function has a return type, but the return statement doesn't have an expression.
22096-
error(node, Diagnostics.Not_all_code_paths_return_a_value);
22097-
}
2209822098
}
2209922099
}
2210022100

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/conformance/es6/yieldExpressions/generatorStrictNullChecks.ts(3,9): error TS7030: Not all code paths return a value.
2+
3+
4+
==== tests/cases/conformance/es6/yieldExpressions/generatorStrictNullChecks.ts (1 errors) ====
5+
function* testGenerator () {
6+
if (Math.random() > 0.5) {
7+
return;
8+
~~~~~~~
9+
!!! error TS7030: Not all code paths return a value.
10+
}
11+
yield 'hello';
12+
}
13+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [generatorStrictNullChecks.ts]
2+
function* testGenerator () {
3+
if (Math.random() > 0.5) {
4+
return;
5+
}
6+
yield 'hello';
7+
}
8+
9+
10+
//// [generatorStrictNullChecks.js]
11+
function* testGenerator() {
12+
if (Math.random() > 0.5) {
13+
return;
14+
}
15+
yield 'hello';
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorStrictNullChecks.ts ===
2+
function* testGenerator () {
3+
>testGenerator : Symbol(testGenerator, Decl(generatorStrictNullChecks.ts, 0, 0))
4+
5+
if (Math.random() > 0.5) {
6+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
7+
>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, --, --))
8+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
9+
10+
return;
11+
}
12+
yield 'hello';
13+
}
14+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorStrictNullChecks.ts ===
2+
function* testGenerator () {
3+
>testGenerator : () => IterableIterator<string>
4+
5+
if (Math.random() > 0.5) {
6+
>Math.random() > 0.5 : boolean
7+
>Math.random() : number
8+
>Math.random : () => number
9+
>Math : Math
10+
>random : () => number
11+
>0.5 : 0.5
12+
13+
return;
14+
}
15+
yield 'hello';
16+
>yield 'hello' : any
17+
>'hello' : "hello"
18+
}
19+
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: true
4+
5+
function* testGenerator () {
6+
if (Math.random() > 0.5) {
7+
return;
8+
}
9+
yield 'hello';
10+
}

0 commit comments

Comments
 (0)