Skip to content

Commit 0c17476

Browse files
a-tarasyukDanielRosenwasser
authored andcommitted
assign error to method definition node if a return type is empty (#35309)
1 parent b968922 commit 0c17476

6 files changed

+106
-2
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26102,7 +26102,7 @@ namespace ts {
2610226102
error(getEffectiveReturnTypeNode(func), Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
2610326103
}
2610426104
else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
26105-
error(getEffectiveReturnTypeNode(func), Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
26105+
error(getEffectiveReturnTypeNode(func) || func, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
2610626106
}
2610726107
else if (compilerOptions.noImplicitReturns) {
2610826108
if (!type) {

tests/baselines/reference/getterControlFlowStrictNull.errors.txt

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
tests/cases/compiler/getterControlFlowStrictNull.ts(2,9): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
22
tests/cases/compiler/getterControlFlowStrictNull.ts(11,14): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
3+
tests/cases/compiler/getterControlFlowStrictNull.ts(20,9): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
34

45

5-
==== tests/cases/compiler/getterControlFlowStrictNull.ts (2 errors) ====
6+
==== tests/cases/compiler/getterControlFlowStrictNull.ts (3 errors) ====
67
class A {
78
a(): string | null {
89
~~~~~~~~~~~~~
@@ -24,4 +25,18 @@ tests/cases/compiler/getterControlFlowStrictNull.ts(11,14): error TS2366: Functi
2425

2526
// it should error here because it returns undefined
2627
}
28+
}
29+
class C {
30+
get a() {
31+
~
32+
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
33+
if (Math.random() > 0.5) {
34+
return 0;
35+
}
36+
37+
// it should error here because it returns undefined
38+
}
39+
40+
set a(value: number) {
41+
}
2742
}

tests/baselines/reference/getterControlFlowStrictNull.js

+29
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ class B {
1616

1717
// it should error here because it returns undefined
1818
}
19+
}
20+
class C {
21+
get a() {
22+
if (Math.random() > 0.5) {
23+
return 0;
24+
}
25+
26+
// it should error here because it returns undefined
27+
}
28+
29+
set a(value: number) {
30+
}
1931
}
2032

2133
//// [getterControlFlowStrictNull.js]
@@ -45,3 +57,20 @@ var B = /** @class */ (function () {
4557
});
4658
return B;
4759
}());
60+
var C = /** @class */ (function () {
61+
function C() {
62+
}
63+
Object.defineProperty(C.prototype, "a", {
64+
get: function () {
65+
if (Math.random() > 0.5) {
66+
return 0;
67+
}
68+
// it should error here because it returns undefined
69+
},
70+
set: function (value) {
71+
},
72+
enumerable: true,
73+
configurable: true
74+
});
75+
return C;
76+
}());

tests/baselines/reference/getterControlFlowStrictNull.symbols

+22
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,25 @@ class B {
3333
// it should error here because it returns undefined
3434
}
3535
}
36+
class C {
37+
>C : Symbol(C, Decl(getterControlFlowStrictNull.ts, 17, 1))
38+
39+
get a() {
40+
>a : Symbol(C.a, Decl(getterControlFlowStrictNull.ts, 18, 9), Decl(getterControlFlowStrictNull.ts, 25, 5))
41+
42+
if (Math.random() > 0.5) {
43+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
44+
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
45+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
46+
47+
return 0;
48+
}
49+
50+
// it should error here because it returns undefined
51+
}
52+
53+
set a(value: number) {
54+
>a : Symbol(C.a, Decl(getterControlFlowStrictNull.ts, 18, 9), Decl(getterControlFlowStrictNull.ts, 25, 5))
55+
>value : Symbol(value, Decl(getterControlFlowStrictNull.ts, 27, 10))
56+
}
57+
}

tests/baselines/reference/getterControlFlowStrictNull.types

+26
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,29 @@ class B {
4343
// it should error here because it returns undefined
4444
}
4545
}
46+
class C {
47+
>C : C
48+
49+
get a() {
50+
>a : number
51+
52+
if (Math.random() > 0.5) {
53+
>Math.random() > 0.5 : boolean
54+
>Math.random() : number
55+
>Math.random : () => number
56+
>Math : Math
57+
>random : () => number
58+
>0.5 : 0.5
59+
60+
return 0;
61+
>0 : 0
62+
}
63+
64+
// it should error here because it returns undefined
65+
}
66+
67+
set a(value: number) {
68+
>a : number
69+
>value : number
70+
}
71+
}

tests/cases/compiler/getterControlFlowStrictNull.ts

+12
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,16 @@ class B {
1717

1818
// it should error here because it returns undefined
1919
}
20+
}
21+
class C {
22+
get a() {
23+
if (Math.random() > 0.5) {
24+
return 0;
25+
}
26+
27+
// it should error here because it returns undefined
28+
}
29+
30+
set a(value: number) {
31+
}
2032
}

0 commit comments

Comments
 (0)