Skip to content

Commit

Permalink
assign error to method definition node if a return type is empty (#35309
Browse files Browse the repository at this point in the history
)
  • Loading branch information
a-tarasyuk authored and DanielRosenwasser committed Nov 25, 2019
1 parent b968922 commit 0c17476
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Expand Up @@ -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) {
Expand Down
17 changes: 16 additions & 1 deletion tests/baselines/reference/getterControlFlowStrictNull.errors.txt
@@ -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 {
~~~~~~~~~~~~~
Expand All @@ -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) {
}
}
29 changes: 29 additions & 0 deletions tests/baselines/reference/getterControlFlowStrictNull.js
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
}());
22 changes: 22 additions & 0 deletions tests/baselines/reference/getterControlFlowStrictNull.symbols
Expand Up @@ -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))
}
}
26 changes: 26 additions & 0 deletions tests/baselines/reference/getterControlFlowStrictNull.types
Expand Up @@ -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
}
}
12 changes: 12 additions & 0 deletions tests/cases/compiler/getterControlFlowStrictNull.ts
Expand Up @@ -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) {
}
}

0 comments on commit 0c17476

Please sign in to comment.