Skip to content

Commit 0168ab2

Browse files
authored
Check return code paths on getters (#10102)
* Check return paths on getters * Remove TODO comment
1 parent 7f6e36c commit 0168ab2

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14126,12 +14126,7 @@ namespace ts {
1412614126
checkSignatureDeclaration(node);
1412714127
if (node.kind === SyntaxKind.GetAccessor) {
1412814128
if (!isInAmbientContext(node) && nodeIsPresent(node.body) && (node.flags & NodeFlags.HasImplicitReturn)) {
14129-
if (node.flags & NodeFlags.HasExplicitReturn) {
14130-
if (compilerOptions.noImplicitReturns) {
14131-
error(node.name, Diagnostics.Not_all_code_paths_return_a_value);
14132-
}
14133-
}
14134-
else {
14129+
if (!(node.flags & NodeFlags.HasExplicitReturn)) {
1413514130
error(node.name, Diagnostics.A_get_accessor_must_return_a_value);
1413614131
}
1413714132
}
@@ -14161,7 +14156,10 @@ namespace ts {
1416114156
checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, Diagnostics.get_and_set_accessor_must_have_the_same_this_type);
1416214157
}
1416314158
}
14164-
getTypeOfAccessors(getSymbolOfNode(node));
14159+
const returnType = getTypeOfAccessors(getSymbolOfNode(node));
14160+
if (node.kind === SyntaxKind.GetAccessor) {
14161+
checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType);
14162+
}
1416514163
}
1416614164
if (node.parent.kind !== SyntaxKind.ObjectLiteralExpression) {
1416714165
checkSourceElement(node.body);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
tests/cases/compiler/getterControlFlowStrictNull.ts(2,9): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
2+
tests/cases/compiler/getterControlFlowStrictNull.ts(11,14): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
3+
4+
5+
==== tests/cases/compiler/getterControlFlowStrictNull.ts (2 errors) ====
6+
class A {
7+
a(): string | null {
8+
~~~~~~~~~~~~~
9+
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
10+
if (Math.random() > 0.5) {
11+
return '';
12+
}
13+
14+
// it does error here as expected
15+
}
16+
}
17+
class B {
18+
get a(): string | null {
19+
~~~~~~~~~~~~~
20+
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
21+
if (Math.random() > 0.5) {
22+
return '';
23+
}
24+
25+
// it should error here because it returns undefined
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [getterControlFlowStrictNull.ts]
2+
class A {
3+
a(): string | null {
4+
if (Math.random() > 0.5) {
5+
return '';
6+
}
7+
8+
// it does error here as expected
9+
}
10+
}
11+
class B {
12+
get a(): string | null {
13+
if (Math.random() > 0.5) {
14+
return '';
15+
}
16+
17+
// it should error here because it returns undefined
18+
}
19+
}
20+
21+
//// [getterControlFlowStrictNull.js]
22+
var A = (function () {
23+
function A() {
24+
}
25+
A.prototype.a = function () {
26+
if (Math.random() > 0.5) {
27+
return '';
28+
}
29+
// it does error here as expected
30+
};
31+
return A;
32+
}());
33+
var B = (function () {
34+
function B() {
35+
}
36+
Object.defineProperty(B.prototype, "a", {
37+
get: function () {
38+
if (Math.random() > 0.5) {
39+
return '';
40+
}
41+
// it should error here because it returns undefined
42+
},
43+
enumerable: true,
44+
configurable: true
45+
});
46+
return B;
47+
}());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@strictNullChecks: true
2+
//@target: ES5
3+
class A {
4+
a(): string | null {
5+
if (Math.random() > 0.5) {
6+
return '';
7+
}
8+
9+
// it does error here as expected
10+
}
11+
}
12+
class B {
13+
get a(): string | null {
14+
if (Math.random() > 0.5) {
15+
return '';
16+
}
17+
18+
// it should error here because it returns undefined
19+
}
20+
}

0 commit comments

Comments
 (0)