Skip to content

#32642 - Getter (with setter) that doesn't return on all paths doesn't have a correct error span #35309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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) {
}
}