assign error to method definition node if a return type is empty (#35309)

This commit is contained in:
Alexander T 2019-11-25 06:18:04 +02:00 committed by Daniel Rosenwasser
parent b9689228b5
commit 0c17476d09
6 changed files with 106 additions and 2 deletions

View file

@ -26102,7 +26102,7 @@ namespace ts {
error(getEffectiveReturnTypeNode(func), Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
}
else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
error(getEffectiveReturnTypeNode(func), Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
error(getEffectiveReturnTypeNode(func) || func, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
}
else if (compilerOptions.noImplicitReturns) {
if (!type) {

View file

@ -1,8 +1,9 @@
tests/cases/compiler/getterControlFlowStrictNull.ts(2,9): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
tests/cases/compiler/getterControlFlowStrictNull.ts(11,14): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
tests/cases/compiler/getterControlFlowStrictNull.ts(20,9): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
==== tests/cases/compiler/getterControlFlowStrictNull.ts (2 errors) ====
==== tests/cases/compiler/getterControlFlowStrictNull.ts (3 errors) ====
class A {
a(): string | null {
~~~~~~~~~~~~~
@ -24,4 +25,18 @@ tests/cases/compiler/getterControlFlowStrictNull.ts(11,14): error TS2366: Functi
// it should error here because it returns undefined
}
}
class C {
get a() {
~
!!! error TS2366: Function lacks ending return statement and return type does not include 'undefined'.
if (Math.random() > 0.5) {
return 0;
}
// it should error here because it returns undefined
}
set a(value: number) {
}
}

View file

@ -16,6 +16,18 @@ class B {
// it should error here because it returns undefined
}
}
class C {
get a() {
if (Math.random() > 0.5) {
return 0;
}
// it should error here because it returns undefined
}
set a(value: number) {
}
}
//// [getterControlFlowStrictNull.js]
@ -45,3 +57,20 @@ var B = /** @class */ (function () {
});
return B;
}());
var C = /** @class */ (function () {
function C() {
}
Object.defineProperty(C.prototype, "a", {
get: function () {
if (Math.random() > 0.5) {
return 0;
}
// it should error here because it returns undefined
},
set: function (value) {
},
enumerable: true,
configurable: true
});
return C;
}());

View file

@ -33,3 +33,25 @@ class B {
// it should error here because it returns undefined
}
}
class C {
>C : Symbol(C, Decl(getterControlFlowStrictNull.ts, 17, 1))
get a() {
>a : Symbol(C.a, Decl(getterControlFlowStrictNull.ts, 18, 9), Decl(getterControlFlowStrictNull.ts, 25, 5))
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, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
return 0;
}
// it should error here because it returns undefined
}
set a(value: number) {
>a : Symbol(C.a, Decl(getterControlFlowStrictNull.ts, 18, 9), Decl(getterControlFlowStrictNull.ts, 25, 5))
>value : Symbol(value, Decl(getterControlFlowStrictNull.ts, 27, 10))
}
}

View file

@ -43,3 +43,29 @@ class B {
// it should error here because it returns undefined
}
}
class C {
>C : C
get a() {
>a : number
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 0;
>0 : 0
}
// it should error here because it returns undefined
}
set a(value: number) {
>a : number
>value : number
}
}

View file

@ -17,4 +17,16 @@ class B {
// it should error here because it returns undefined
}
}
class C {
get a() {
if (Math.random() > 0.5) {
return 0;
}
// it should error here because it returns undefined
}
set a(value: number) {
}
}