Flag non-nullable values with call expressions in if
statements as … · microsoft/TypeScript@130615a (original) (raw)
``
1
`+
=== tests/cases/compiler/truthinessCallExpressionCoercion.ts ===
`
``
2
`+
function func() { return Math.random() > 0.5; }
`
``
3
`+
func : Symbol(func, Decl(truthinessCallExpressionCoercion.ts, 0, 0))
`
``
4
`+
Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
`
``
5
`+
Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
`
``
6
`+
random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
`
``
7
+
``
8
`+
if (func) { // error
`
``
9
`+
func : Symbol(func, Decl(truthinessCallExpressionCoercion.ts, 0, 0))
`
``
10
`+
}
`
``
11
+
``
12
`+
function onlyErrorsWhenNonNullable(required: () => boolean, optional?: () => boolean) {
`
``
13
`+
onlyErrorsWhenNonNullable : Symbol(onlyErrorsWhenNonNullable, Decl(truthinessCallExpressionCoercion.ts, 3, 1))
`
``
14
`+
required : Symbol(required, Decl(truthinessCallExpressionCoercion.ts, 5, 35))
`
``
15
`+
optional : Symbol(optional, Decl(truthinessCallExpressionCoercion.ts, 5, 59))
`
``
16
+
``
17
`+
if (required) { // error
`
``
18
`+
required : Symbol(required, Decl(truthinessCallExpressionCoercion.ts, 5, 35))
`
``
19
`+
}
`
``
20
+
``
21
`+
if (required()) { // ok
`
``
22
`+
required : Symbol(required, Decl(truthinessCallExpressionCoercion.ts, 5, 35))
`
``
23
`+
}
`
``
24
+
``
25
`+
if (optional) { // ok
`
``
26
`+
optional : Symbol(optional, Decl(truthinessCallExpressionCoercion.ts, 5, 59))
`
``
27
`+
}
`
``
28
`+
}
`
``
29
+
``
30
`+
function checksPropertyAndElementAccess() {
`
``
31
`+
checksPropertyAndElementAccess : Symbol(checksPropertyAndElementAccess, Decl(truthinessCallExpressionCoercion.ts, 14, 1))
`
``
32
+
``
33
`+
const x = {
`
``
34
`+
x : Symbol(x, Decl(truthinessCallExpressionCoercion.ts, 17, 9))
`
``
35
+
``
36
`+
foo: {
`
``
37
`+
foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 17, 15))
`
``
38
+
``
39
`+
bar() { }
`
``
40
`+
bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 18, 14))
`
``
41
`+
}
`
``
42
`+
}
`
``
43
+
``
44
`+
if (x.foo.bar) { // error
`
``
45
`+
x.foo.bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 18, 14))
`
``
46
`+
x.foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 17, 15))
`
``
47
`+
x : Symbol(x, Decl(truthinessCallExpressionCoercion.ts, 17, 9))
`
``
48
`+
foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 17, 15))
`
``
49
`+
bar : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 18, 14))
`
``
50
`+
}
`
``
51
+
``
52
`+
if (x.foo['bar']) { // error
`
``
53
`+
x.foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 17, 15))
`
``
54
`+
x : Symbol(x, Decl(truthinessCallExpressionCoercion.ts, 17, 9))
`
``
55
`+
foo : Symbol(foo, Decl(truthinessCallExpressionCoercion.ts, 17, 15))
`
``
56
`+
'bar' : Symbol(bar, Decl(truthinessCallExpressionCoercion.ts, 18, 14))
`
``
57
`+
}
`
``
58
`+
}
`
``
59
+
``
60
`+
function maybeBoolean(param: boolean | (() => boolean)) {
`
``
61
`+
maybeBoolean : Symbol(maybeBoolean, Decl(truthinessCallExpressionCoercion.ts, 28, 1))
`
``
62
`+
param : Symbol(param, Decl(truthinessCallExpressionCoercion.ts, 30, 22))
`
``
63
+
``
64
`+
if (param) { // ok
`
``
65
`+
param : Symbol(param, Decl(truthinessCallExpressionCoercion.ts, 30, 22))
`
``
66
`+
}
`
``
67
`+
}
`
``
68
+
``
69
`+
class Foo {
`
``
70
`+
Foo : Symbol(Foo, Decl(truthinessCallExpressionCoercion.ts, 33, 1))
`
``
71
+
``
72
`+
maybeIsUser?: () => boolean;
`
``
73
`+
maybeIsUser : Symbol(Foo.maybeIsUser, Decl(truthinessCallExpressionCoercion.ts, 35, 11))
`
``
74
+
``
75
`+
isUser() {
`
``
76
`+
isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion.ts, 36, 32))
`
``
77
+
``
78
`+
return true;
`
``
79
`+
}
`
``
80
+
``
81
`+
test() {
`
``
82
`+
test : Symbol(Foo.test, Decl(truthinessCallExpressionCoercion.ts, 40, 5))
`
``
83
+
``
84
`+
if (this.isUser) { // error
`
``
85
`+
this.isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion.ts, 36, 32))
`
``
86
`+
this : Symbol(Foo, Decl(truthinessCallExpressionCoercion.ts, 33, 1))
`
``
87
`+
isUser : Symbol(Foo.isUser, Decl(truthinessCallExpressionCoercion.ts, 36, 32))
`
``
88
`+
}
`
``
89
+
``
90
`+
if (this.maybeIsUser) { // ok
`
``
91
`+
this.maybeIsUser : Symbol(Foo.maybeIsUser, Decl(truthinessCallExpressionCoercion.ts, 35, 11))
`
``
92
`+
this : Symbol(Foo, Decl(truthinessCallExpressionCoercion.ts, 33, 1))
`
``
93
`+
maybeIsUser : Symbol(Foo.maybeIsUser, Decl(truthinessCallExpressionCoercion.ts, 35, 11))
`
``
94
`+
}
`
``
95
`+
}
`
``
96
`+
}
`
``
97
+